Skip to content

Instantly share code, notes, and snippets.

@rheinardkorf
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rheinardkorf/6834dc067c7890b9f2c3 to your computer and use it in GitHub Desktop.
Save rheinardkorf/6834dc067c7890b9f2c3 to your computer and use it in GitHub Desktop.
A simple ruby script to add to a TextMate bundle that aligns PHP key/value pairs.
#!/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -wKU
# Author: Rheinard Korf
# Usage:
# * Create a new bundle (or add to existing bundle).
# * Create a new command (e.g. KeyValue Alignment) and set a shortcut key (e.g. Ctrl+Cmd+Option+] )
# * Paste the code in the command editor and save the bundle (Cmd+S)
#
# Now select a set of key/value pairs (not the whole array) and try out the new command.
lines = ENV['TM_SELECTED_TEXT'].split("\n")
max_length = 0
lines.each do | line |
temp = line.split(/\:|\=\>/)
if temp[0].length > max_length then max_length = temp[0].length end
end
lines.each do | line |
temp = line.split(/\:|\=\>/)
print temp[0].ljust(max_length) + '=>'
puts temp[1]
end
@rheinardkorf
Copy link
Author

Turns this:

"key1" => "value1",  
"longerkey2" => "value2",  
"anevenlongerkey3" => "value3",  
"alongkeykey4" => "value4",  
"nottoolongkey5" => "value5",  

into this:

"key1"             => "value1",  
"longerkey2"       => "value2",  
"anevenlongerkey3" => "value3",  
"alongkeykey4"     => "value4",  
"nottoolongkey5"   => "value5",  

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment