Created
March 14, 2012 19:05
-
-
Save skull-squadron/2038710 to your computer and use it in GitHub Desktop.
ansi 256 color swatch sample in ruby
This file contains hidden or 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 | |
# | |
# Usage: 256swatch [-b [foreground [test pattern]]] | |
# | |
# option description default | |
# ----------------------------------------------------------- | |
# | |
# -b print background swatches instead normal | |
# | |
# foreground force foreground (decimal) normal | |
# | |
# test pattern text to display 'test' | |
def swatch(s, width=16) | |
def foreground(x) ; "\033[38;5;#{x}m" end | |
def background(x) ; "\033[48;5;#{x}m" end | |
def reset ; "\033[0m" end | |
result = ' ' + (0...width).map{|x|sprintf "%*d",s.size-1+(x==0?0:2),x}.join + "\n" # header | |
color = 0 | |
begin # row header each cell color cell reset btw_cell eol | |
result += sprintf("%3d ",color)+(0...width).collect{yield(color+=1).to_s+s+reset}.join(' ')+"\n" | |
end until color == 256 | |
result | |
end | |
puts <<-eos | |
#{swatch(ARGV[2]||"test") { |x| (ARGV[0] ? background(x) : foreground(x)) + | |
(ARGV[1] ? foreground(ARGV[1].to_i) : '') }} | |
eos |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment