Mouse.The.Lucky.Dog
Mouse.The.Lucky.Dog

Reputation: 755

terminal output in ruby without curses?

I'm working in Linux ( and konsole if that makes a sifference ) and would like to have some basic control over the screen. What I need is simple and does not need the full power of ncurses, I real need three simple commands, "clear screen", "go to x and y" and "use this color".

Can anyone make suggestions?

Upvotes: 0

Views: 320

Answers (1)

Tim Uruski
Tim Uruski

Reputation: 151

To control the screen you need to send (or print) ANSI control sequences. To clear the screen the sequence is \e[2J, which you can just puts or print to STDOUT, depending on your needs.

Some example methods in Ruby:

def clear_screen
  print "\e[2J"
end

def clear_line
  print "\e[2K"
end

def reset_cursor
  print "\e[H"
end

def position_cursor(y,x)
  print "\e[#{y};#{x}H"
end

def red
  print "\e[0;31m"
end

A table of the sequences is here: http://ascii-table.com/ansi-escape-sequences.php

You can see a table of color sequences here: http://www.pixelbeat.org/docs/terminal_colours/

Upvotes: 2

Related Questions