Terminal, colour and Ruby

I use ruby for system administration tasks all the time and find a little colour always helps to highlight certain information. The only problem is I’ll forget which colour is represented by which number. The ruby script below prints out a nice box of all foreground and background colours. I’ve uploaded a screenshot of the output below so you can see how it looks.

Ruby colour box

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/env ruby -wdK
 
esc = "\033["
fgs = (30..37).to_a
bgs = (40..47).to_a
box_width = 4 + 4 * bgs.length
 
puts "", "Ruby colour box".center(box_width), ""
 
print " "*4
 
bgs.each_with_index do |bg, i|
  fg = fgs[i]
  print " #{bg} "
end
 
print "\n"
 
fgs.each do |fg|
  print " #{fg} "
  bgs.each do |bg|
    print "#{esc}#{fg};#{bg}m #{fg} #{esc}0m"
  end
  print "\n"
end
 
puts "", "http://www.jamesconroyfinn.com".center(box_width), ""

There are a couple of nice ruby features used in this script like defining an array using a range.

4
fgs = (30..37).to_a

And padding a string to center align it:

27
"http://www.jamesconroyfinn.com".center(box_width)

0 Responses to “Terminal, colour and Ruby”


  1. No Comments

Leave a Reply