From 74645bee373b3c7ed1792fb11962ac3f3d691d14 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Thu, 6 Nov 2014 03:09:00 +0900 Subject: [PATCH] Add preview.rb script --- README.md | 19 ++++++++++++++ tools/preview.rb | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100755 tools/preview.rb diff --git a/README.md b/README.md index 7dc3944..c11101f 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ - [Extra](#extra) - [X11 Installation](#x11-installation) - [Terminator color schemes](#terminator-color-schemes) + - [Previewing color schemes](#previewing-color-schemes) ##Intro## This is a set of color schemes for iTerm (aka iTerm2). Screenshots below and in the [screenshots](screenshots/) directory. @@ -716,6 +717,24 @@ An example config file that includes the code snippet for the Symfonic theme wou [plugins] ``` +###Previewing color schemes### + +[preview.rb](tools/preview.rb) is a simple script that allows you to preview +the color schemes without having to import them. It parses .itermcolors files +and applies the colors to the current session using [iTerm's proprietary +escape codes](https://iterm2.com/documentation-escape-codes.html). As noted in +the linked page, it doesn't run on tmux or screen. + +```sh +# Apply AdventureTime scheme to the current session +tools/preview.rb schemes/AdventureTime.itermcolors + +# Apply the schemes in turn. +# - Press any key to advance; hit CTRL-C or ESC to stop +tools/preview.rb schemes/* +``` + + ---- iTerm Color Schemes | iTerm2 Color Schemes | iTerm 2 Color Schemes | iTerm Themes | iTerm2 Themes | iTerm 2 Themes diff --git a/tools/preview.rb b/tools/preview.rb new file mode 100755 index 0000000..963b4be --- /dev/null +++ b/tools/preview.rb @@ -0,0 +1,68 @@ +#!/usr/bin/ruby +# +# Applies the colors defined in .itermcolors file to the current session using +# proprietary escape codes of iTerm2 +# +# Author: Junegunn Choi Reference: +# https://iterm2.com/documentation-escape-codes.html + +require 'rexml/document' +require 'io/console' + +files = ARGV.select { |f| File.exists? f } +if files.empty? + puts "usage: #$0 " + exit 1 +end + +if ENV.has_key? 'TMUX' + puts 'Does not work on tmux' + exit 1 +end + +preview = files.length > 1 +until files.empty? + file = files.shift + print "[#{File.basename file, '.*'}] " if preview + begin + colors = {} + root = REXML::Document.new(File.read file).root + root.elements['dict'].select { |e| e.is_a? REXML::Element }.each do |dict| + if dict.previous_element && !dict.previous_element.text.strip.empty? + type = dict.previous_element.text.downcase.gsub(/^ansi\s+|\s+color$/, '') + colors[type] = {} + end + next unless type + + dict.elements.each_slice(2) do |elems| + key = val = nil + elems.each do |elem| + case elem.name.downcase + when 'key' then key = elem.text + when 'real' then val = elem.text + end + end + colors[type][key.sub(/\s.+/, '').downcase.to_sym] = + '%02x' % [255, val.to_f.*(256).to_i].min if key && val + end + colors[type] &&= colors[type].values_at(:red, :green, :blue).join + end + + colors.each do |type, rgb| + print "\e]P" << { + 'foreground' => 'g', + 'background' => 'h', + 'bold' => 'i', + 'selection' => 'j', + 'selected text' => 'k', + 'cursor' => 'l', + 'cursor text' => 'm', + }.fetch(type, '%x' % type.to_i) << rgb << "\e\\" + end + break if files.empty? || [3.chr, "\e"].include?(IO.console.getch) + rescue Exception + print '(X) ' + end +end +puts if preview +