Created
August 21, 2012 06:17
-
-
Save pasela/3412590 to your computer and use it in GitHub Desktop.
Convert .minttyrc color settings to escape sequences.
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
# encoding: utf-8 | |
# Convert .minttyrc color settings to escape sequences. | |
# | |
# USAGE: | |
# ruby export-mintty-color.rb [minttyrc_file] | |
# | |
# minttyrc_file target rc file(default is ~/.minttyrc) | |
COLOR_SETTINGS = { | |
'ForegroundColour' => '\e]10;%s\a', | |
'BackgroundColour' => '\e]11;%s\a', | |
'CursorColour' => '\e]12;%s\a', | |
'IMECursorColour' => '\e]4;262;%s\a', | |
'Black' => '\e]4;0;%s\a', | |
'BoldBlack' => '\e]4;8;%s\a', | |
'Red' => '\e]4;1;%s\a', | |
'BoldRed' => '\e]4;9;%s\a', | |
'Green' => '\e]4;2;%s\a', | |
'BoldGreen' => '\e]4;10;%s\a', | |
'Yellow' => '\e]4;3;%s\a', | |
'BoldYellow' => '\e]4;11;%s\a', | |
'Blue' => '\e]4;4;%s\a', | |
'BoldBlue' => '\e]4;12;%s\a', | |
'Magenta' => '\e]4;5;%s\a', | |
'BoldMagenta' => '\e]4;13;%s\a', | |
'Cyan' => '\e]4;6;%s\a', | |
'BoldCyan' => '\e]4;14;%s\a', | |
'White' => '\e]4;7;%s\a', | |
'BoldWhite' => '\e]4;15;%s\a', | |
} | |
def parse_color(value) | |
case value | |
when /(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/ | |
{ red: $1.to_i, green: $2.to_i, blue: $3.to_i } | |
when /#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/i | |
{ red: $1.hex, green: $2.hex, blue: $3.hex } | |
when /rgb:([0-9a-f]{2})\/([0-9a-f]{2})\/([0-9a-f]{2})/i | |
{ red: $1.hex, green: $2.hex, blue: $3.hex } | |
end | |
end | |
def to_hex_color(color) | |
if color | |
'#%02x%02x%02x' % [color[:red], color[:green], color[:blue]] | |
else | |
'#ffffff' | |
end | |
end | |
mintty_rc = File.expand_path(ARGV[0] || '~/.minttyrc') | |
if !FileTest.exist?(mintty_rc) | |
$stderr.puts "#{mintty_rc} not exists." | |
exit 1 | |
end | |
IO.foreach(mintty_rc) {|line| | |
line.chomp! | |
key, value = line.split(/\s*=\s*/) | |
if COLOR_SETTINGS.key?(key) | |
color = parse_color(value) | |
if color | |
color_esc = COLOR_SETTINGS[key] % to_hex_color(color) | |
puts "echo -ne %20s # #{key}" % "'#{color_esc}'" | |
end | |
end | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment