ANSI Codes with Character

written in ansi, ascii, ruby

This was a lightening talk given to the office about ANSI Escape Codes. Most of the time, all 5 minutes of it, was spent explaining the code snippets.

Wiki about ANSI Codes

What is an ANSI code?

ANSI Escape Codes are a nearly universal means of embedding display options in computer terminals.

\e[ is how to tell the terminal we’re giving it a command instead just outputting text.

What does that mean?!? Let the examples do the talking.

Color Examples

1
2
echo -e "\e[2J\e[32m It's not easy being green \e[0m"
echo -e "\e[2J\e[31m Apples are red  \e[0m"
1
(30..37).each{|i| puts "i: \e[#{i}m #{i} \e[0m"}

Position Examples

1
2
3
4
5
6
7
8
9
x=`tput cols`.to_i
y=`tput lines`.to_i
loop do
  print "\e[s" # Save current cursor position
  print "\e[#{rand(y)};#{rand(x)}H"  # move to row/column
  print "💩"    # print POOP!
  print "\e[u" # restore position
  sleep(rand)
end
1
2
# Ruby oneliner troll
ruby -e 'x=`tput cols`.to_i; y=`tput lines`.to_i; loop {print "\e[s\e[#{rand(y)};#{rand(x)}H💩\e[u"; sleep(rand)}'

ANSI-nine Examples

1
2
3
4
5
# Poop-field
curl -s https://raw.githubusercontent.com/ddrscott/ansinine/master/stars | ruby

# Smoke
curl -s https://raw.githubusercontent.com/ddrscott/ansinine/master/fire.rb | ruby