Permalink
Browse files

Upstream colortest

  • Loading branch information...
1 parent 56aa57e commit 4d52d1e7bb3c6a0226f74a67076ab0c96617486d @rking rking committed with rking@sharpsaw.org Jan 7, 2013
Showing with 61 additions and 0 deletions.
  1. +61 −0 bin/colortest
View
@@ -0,0 +1,61 @@
+#!/usr/bin/env ruby
+# Print 8- and 256-color tables.
+
+class String
+ def visible_length
+ # only knows about newlines and "\e[m"-escapes
+ gsub(/(.*\n|\e.*?m)/, '').length
+ end
+end
+
+class LineBrokenOutput
+ COLUMNS = (ENV['COLUMNS'] || 80).to_i
+ def initialize
+ @buf = ''
+ end
+ def put atomic_str
+ flush if COLUMNS < (@buf + atomic_str).visible_length
+ @buf += atomic_str
+ end
+ def flush
+ puts @buf
+ @buf = ''
+ end
+end
+
+out = LineBrokenOutput.new
+
+puts '256-color mode ("\\e[38;5;___m"):'
+[
+ (0..15),
+ *(16..256).each_slice(6),
+].each do |row|
+ row.each do |c|
+ out.put "\e[38;5;%sm%3s\e[0m " % [c, c]
+ end
+ out.flush
+end
+
+out.put "\n16-color mode:"
+0.upto(16) do |c|
+ adjusted = 30+c
+ out.put "\e[#{adjusted}m#{adjusted}\e[m "
+end
+
+out.put "\n Extras: "
+extras = {
+ 'reset' => 0,
+ 'bold' => 1,
+ 'faint' => 2,
+ 'italic' => 3,
+ 'underline' => 4,
+ 'blink' => 5,
+ 'fastblink' => 6,
+ 'reverse' => 7,
+ 'concealed' => 8,
+ 'strikeout' => 9,
+}
+extras.each do |key, c|
+ out.put "\e[#{c}m#{key}(#{c})\e[0m "
+end
+out.flush

0 comments on commit 4d52d1e

Please sign in to comment.