Created
May 11, 2018 08:54
-
-
Save monorkin/f6721e4d3fc2650fda07bf6461188db7 to your computer and use it in GitHub Desktop.
Script that converts iTerm themes to Alacritty
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 ruby | |
# frozen_string_literal: true | |
# USAGE: | |
# curl https://gist.github.com/stankec/.../iterm_theme_to_alacritty.rb > iterm_theme_to_alacritty.rb | |
# chmod +x iterm_theme_to_alacritty.rb | |
# ./iterm_theme_to_alacritty.rb path/to/your/theme.itermcolors | |
# | |
# This will output the config to the terminal | |
# | |
# NOT RECOMENDED, but you can also write to the config by passing 'write' as the | |
# second argument. | |
# E.g: | |
# ./iterm_theme_to_alacritty.rb path/to/your/theme.itermcolors write | |
require 'rubygems' | |
require 'bundler' | |
require 'yaml' | |
# Install dependencies | |
begin | |
require 'plist' | |
rescue LoadError | |
# gemfile <<-G | |
# source "https://rubygems.org" | |
# gem "plist" | |
# G | |
# bundle 'install --deployment' | |
# retry | |
puts 'Run `gem install plist` first' | |
exit(1) | |
end | |
LOOKUP_PATHS = [ | |
"#{Dir.home}/.config/alacritty/alacritty.yml" | |
].freeze | |
def write? | |
ARGV[1] == 'write' | |
end | |
existing_configs = LOOKUP_PATHS.select { |path| File.exist?(path) } | |
config_path = existing_configs.first | |
if write? && config_path.nil? | |
puts 'No Alacritty config found!' | |
exit(1) | |
end | |
iterm2_theme_path = ARGV[0] || '' | |
unless File.exist?(iterm2_theme_path) | |
puts "iTerm2 theme not found! Searched '#{iterm2_theme_path}'" | |
exit(1) | |
end | |
begin | |
theme = Plist.parse_xml(iterm2_theme_path) | |
rescue | |
puts "'#{iterm2_theme_path}' is not a validiTerm2 theme!" | |
exit(1) | |
end | |
class Theme | |
class Color | |
attr_accessor :color | |
def initialize(color) | |
@color = color | |
end | |
def red | |
color['Red Component'].to_f | |
end | |
def green | |
color['Green Component'].to_f | |
end | |
def blue | |
color['Blue Component'].to_f | |
end | |
def formatted_hex | |
"0x#{hex}" | |
end | |
def hex | |
[ | |
hex_component(red), | |
hex_component(green), | |
hex_component(blue) | |
].join('') | |
end | |
def hex_component(float) | |
(float * 255).to_i.clamp(0, 255).to_s(16) | |
end | |
end | |
attr_accessor :theme | |
def initialize(theme) | |
@theme = theme | |
end | |
def ansi_color(i) | |
color("Ansi #{i} Color") | |
end | |
def background | |
color('Background Color') | |
end | |
def foreground | |
color('Foreground Color') | |
end | |
def cursor_text | |
color('Cursor Text Color') | |
end | |
def cursor | |
color('Cursor Color') | |
end | |
private | |
def color(key) | |
to_color(theme[key]) | |
end | |
def to_color(color) | |
return unless color | |
Color.new(color) | |
end | |
end | |
theme = Theme.new(theme) | |
begin | |
config = (write? && YAML.load_file(config_path)) || {} | |
rescue | |
puts 'Invalid Alacritty config format!' | |
exit(1) | |
end | |
colors = { | |
primary: { | |
background: theme.background.formatted_hex, | |
foreground: theme.foreground.formatted_hex | |
}, | |
cursor: { | |
text: theme.cursor_text.formatted_hex, | |
cursor: theme.cursor.formatted_hex | |
}, | |
normal: { | |
black: theme.ansi_color(0).formatted_hex, | |
red: theme.ansi_color(1).formatted_hex, | |
green: theme.ansi_color(2).formatted_hex, | |
yellow: theme.ansi_color(3).formatted_hex, | |
blue: theme.ansi_color(4).formatted_hex, | |
magenta: theme.ansi_color(5).formatted_hex, | |
cyan: theme.ansi_color(6).formatted_hex, | |
white: theme.ansi_color(7).formatted_hex | |
}, | |
bright: { | |
black: theme.ansi_color(8).formatted_hex, | |
red: theme.ansi_color(9).formatted_hex, | |
green: theme.ansi_color(10).formatted_hex, | |
yellow: theme.ansi_color(11).formatted_hex, | |
blue: theme.ansi_color(12).formatted_hex, | |
magenta: theme.ansi_color(13).formatted_hex, | |
cyan: theme.ansi_color(14).formatted_hex, | |
white: theme.ansi_color(15).formatted_hex | |
} | |
} | |
def stringify_keys(hash) | |
case hash | |
when Array then hash.map { |e| stringify_keys(e) } | |
when Hash then Hash[hash.map { |k, v| [k.to_s, stringify_keys(v)] }] | |
else hash | |
end | |
end | |
config['colors'] = stringify_keys(colors) | |
def write_config(cfg) | |
File.open(config_path, 'w') do |f| | |
f.write(cfg.to_yaml) | |
end | |
rescue | |
puts 'Unable to write Alacritty config!' | |
exit(1) | |
end | |
def print_config(cfg) | |
puts cfg.to_yaml | |
end | |
case ARGV[1] | |
when 'write' then write_config(config) | |
when nil then print_config(config) | |
end | |
puts 'Done!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment