Skip to content

Instantly share code, notes, and snippets.

@markevans
Created December 12, 2011 16:17
Show Gist options
  • Save markevans/1468065 to your computer and use it in GitHub Desktop.
Save markevans/1468065 to your computer and use it in GitHub Desktop.
Command Line utility for setting the background colour of the OSX terminal
#!/usr/bin/env ruby
if ARGV.length != 1
puts "Usage:"
puts " colour red"
puts "or"
puts " colour '#00ffaa' # NOTE THE QUOTES"
puts "or"
puts " colour rgb(255,0,0)"
exit
end
STANDARD_COLOURS = {
'white' => '#FFFFFF',
'silver' => '#C0C0C0',
'gray' => '#808080',
'black' => '#000000',
'red' => '#FF0000',
'maroon' => '#800000',
'yellow' => '#FFFF00',
'olive' => '#808000',
'lime' => '#00FF00',
'green' => '#008000',
'aqua' => '#00FFFF',
'teal' => '#008080',
'blue' => '#0000FF',
'navy' => '#000080',
'fuchsia' => '#FF00FF',
'purple' => '#800080'
}
def hex_to_16_bit_rgb(hex)
_, r, g, b = hex.match(/^#(\w\w)(\w\w)(\w\w)$/).to_a
rgb_to_16_bit_rgb(r.hex, g.hex, b.hex)
end
def rgb_to_16_bit_rgb(r, g, b)
[r << 8, g << 8, b << 8].join(',')
end
colour_string = ARGV[0]
colour = case colour_string
when /^\#/ then hex_to_16_bit_rgb(colour_string)
when /rgb\((\d+),(\d+),(\d+)\)/ then rgb_to_16_bit_rgb($1, $2, $3)
when /^\w+$/ then hex_to_16_bit_rgb(STANDARD_COLOURS[colour_string]) if STANDARD_COLOURS[colour_string]
end
if !colour
puts "I don't recognize #{colour_string.inspect}"
exit
end
puts "Setting the background colour to #{colour} (16 bit rgb)"
command = """
osascript -e 'tell application \"Terminal\"
set targetWindow to window 1
set background color of targetWindow to {#{colour}}
# set cursor color of targetWindow to TODO
# set normal text color of targetWindow to TODO
# set bold text color of targetWindow to TODO
end tell'
"""
`#{command}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment