|
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +# imports an iTerm color profile to gnome terminal |
| 4 | +# syntax: import_from_iterm.rb <path_to_profile> <profile_name> |
| 5 | + |
| 6 | +require 'nokogiri' |
| 7 | +require 'nokogiri-plist' |
| 8 | + |
| 9 | +class Color |
| 10 | + attr_accessor :red |
| 11 | + attr_accessor :green |
| 12 | + attr_accessor :blue |
| 13 | + |
| 14 | + def initialize(args={}) |
| 15 | + args.each_pair do |k, v| |
| 16 | + self.send("#{k}=", v) |
| 17 | + end |
| 18 | + end |
| 19 | + |
| 20 | + def to_48bit_hex |
| 21 | + red_part = sprintf("%04x", (red.to_f * 65535).round) |
| 22 | + green_part = sprintf("%04x", (green.to_f * 65535).round) |
| 23 | + blue_part = sprintf("%04x", (blue.to_f * 65535).round) |
| 24 | + "##{red_part}#{green_part}#{blue_part}" |
| 25 | + end |
| 26 | +end |
| 27 | + |
| 28 | +file_path = ARGV[0] |
| 29 | +profile_name = ARGV[1] |
| 30 | + |
| 31 | +unless file_path && profile_name |
| 32 | + puts "Syntax: import_from_iterm.rb <path_to_profile> <profile_name>" |
| 33 | + exit(-1) |
| 34 | +end |
| 35 | + |
| 36 | +if Dir.exists?("colors/#{profile_name}") |
| 37 | + puts "Color #{profile_name} already exists, delete it or choose a different name" |
| 38 | + exit(-1) |
| 39 | +end |
| 40 | + |
| 41 | +input = File.open(file_path) |
| 42 | +plist = Nokogiri::PList(input) |
| 43 | +input.close |
| 44 | + |
| 45 | +# First of all, we need to collect values for each ansi color |
| 46 | +# Label inside plist file is like "Ansi 0 Color" |
| 47 | + |
| 48 | +colors = {} |
| 49 | +(0).upto(15) do |count| |
| 50 | + label = "Ansi #{count} Color" |
| 51 | + data = plist[label] |
| 52 | + colors[label] = Color.new( |
| 53 | + red: data['Red Component'], |
| 54 | + green: data['Green Component'], |
| 55 | + blue: data['Blue Component'], |
| 56 | + ) |
| 57 | +end |
| 58 | + |
| 59 | +# Now, we need to collect other data: background color, foreground color, and bold color |
| 60 | + |
| 61 | +['Background Color', 'Foreground Color', 'Bold Color'].each do |label| |
| 62 | + data = plist[label] |
| 63 | + colors[label] = Color.new( |
| 64 | + red: data['Red Component'], |
| 65 | + green: data['Green Component'], |
| 66 | + blue: data['Blue Component'], |
| 67 | + ) |
| 68 | +end |
| 69 | + |
| 70 | +# Very good. Now we have all we need to generate the Gnome Terminal color profile. |
| 71 | + |
| 72 | +# Let's start creating a directory |
| 73 | + |
| 74 | +Dir.mkdir("colors/#{profile_name}") |
| 75 | + |
| 76 | +# now, the palettes. Let's build it first |
| 77 | + |
| 78 | +palette = [] |
| 79 | +(0).upto(15) do |count| |
| 80 | + label = "Ansi #{count} Color" |
| 81 | + color_code_48_bit = colors[label].to_48bit_hex |
| 82 | + palette << color_code_48_bit |
| 83 | +end |
| 84 | + |
| 85 | +# now let's write the dconf palette |
| 86 | +File.open("colors/#{profile_name}/palette_dconf", "w") do |f| |
| 87 | + f.puts palette.map { |color| "'#{color}'"}.join(", ") |
| 88 | +end |
| 89 | + |
| 90 | +# and the gconf palette |
| 91 | +File.open("colors/#{profile_name}/palette_gconf", "w") do |f| |
| 92 | + f.puts palette.join(":") |
| 93 | +end |
| 94 | + |
| 95 | +# bold color |
| 96 | +File.open("colors/#{profile_name}/bd_color", "w") do |f| |
| 97 | + f.puts colors['Bold Color'].to_48bit_hex |
| 98 | +end |
| 99 | + |
| 100 | +# background color |
| 101 | +File.open("colors/#{profile_name}/bg_color", "w") do |f| |
| 102 | + f.puts colors['Background Color'].to_48bit_hex |
| 103 | +end |
| 104 | + |
| 105 | +# Foreground color |
| 106 | +File.open("colors/#{profile_name}/fg_color", "w") do |f| |
| 107 | + f.puts colors['Foreground Color'].to_48bit_hex |
| 108 | +end |
| 109 | + |
| 110 | +# A little README.md file |
| 111 | +File.open("colors/#{profile_name}/README.md", "w") do |f| |
| 112 | + f.puts "Colorscheme #{profile_name} - imported from iterm on #{Time.now}" |
| 113 | +end |
| 114 | + |
| 115 | +puts "Done!" |
0 commit comments