Created
August 31, 2016 10:29
-
-
Save walski/d04faffef66ac417a190c1a98c7cbe07 to your computer and use it in GitHub Desktop.
Outputs the full key of the current line in a Rails I18n YAML file and copies it to the clipboard. 🎉
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby18 -wKU | |
def key_part(line) | |
match = line.match(/^\s*([^:]+)/) | |
return unless match | |
match.captures.first | |
end | |
def leading_spaces(line) | |
line.match(/^(\s*)\S?/).captures.first.length | |
end | |
cursor_line = ENV['TM_LINE_NUMBER'].to_i | |
content = File.read(ENV['TM_FILEPATH']) | |
lines = content.split("\n") | |
i = cursor_line | |
spaces = leading_spaces(lines[i]) | |
key_parts = [key_part(lines[i])] | |
while spaces > 0 | |
new_spaces = spaces - 2 | |
while i >= 0 && leading_spaces(lines[i]) > new_spaces | |
i -= 1 | |
end | |
key_parts.unshift(key_part(lines[i])) | |
spaces = new_spaces | |
end | |
full_key = key_parts.compact.join('.') | |
`echo '#{full_key}' | pbcopy` | |
puts full_key |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment