Colors in your terminal
Par Mathieu Lecarme le mercredi, 13 octobre 2010, 13:15 - Lien permanent
In a world where everything has a GUI, the ugly and antediluvian terminal seems to be an abberation. MacOSX brings only one thing to MacOS : the Terminal.
Terminal doesn't have to be sad, you can use audacious colors with it!
Terminal can handle more than simple characters with special instructions.
Ruby
In ruby, you've the wonderful but simple rainbow
sudo gem install rainbow
It's easy to use :
puts "hello " + "colored".color(:red) + " world"
If you are an adventurer, you can do more. Wikipedia tells you everything about ANSI escape code. CSI in ruby is \e[.
With such tools, you can do the extraordinary spinning fan effect :
1000.times do |i|
print "\e[3G\e[1m"
print %w{ | / – \\ }[i % 4]
print " \e[0m \e[8G"
STDOUT.flush
sleep 0.5
end
It's cute as a regex.
- You put the cursor in an absolute position (third place)
- You wont red color
- You draw a fan
- No more color
- Go to hell, cursor, I wont to see my fan
In ruby, when you puts, flush is implicit, but not with print .
No rake user can live without this gem.
The rest of the world
Every language got a library for that. I have trying colors for nodejs and colorama for Python

