Clear screen

Is there a way to clear the terminal screen by using Ruby directly?
Instead of using system(“clear”).

Cheers
Christian

On Apr 26, 2007, at 8:25 AM, Christian wrote:

Is there a way to clear the terminal screen by using Ruby directly?
Instead of using system(“clear”).

Nothing builtin. A lighter approach is to print an ANSI escape
sequence if your terminal understands it:

module Screen
def self.clear
print “\e[2J\e[f”
end
end

– fxn

Thanks a lot. I will stick to the escape characters because I also
need to set the cursor position.

Cheers
Christian

Christian,

The following is a hack: I checked, and on my system, “clear” sends the
following control characters to the screen: ^[[H^[[2J

This will only work on unix, and I’m not sure whether it works on all
terminals, but

puts “\e[H\e[2J”

clears the screen. You might want to use “if $stdout.isatty” so that you
don’t get control characters in an output file…

Note that this is pretty much exactly the same thing as system(“clear”).
If that’s not good enough, you probably want to look at terminal
libraries–I think ruby-termios might be useful, but I have no
experience with it.

Dan

On Apr 26, 2007, at 10:00 AM, Christian wrote:

Thanks a lot. I will stick to the escape characters because I also
need to set the cursor position.

You may want to check out the Ruby ncurses project:
http://ncurses-ruby.berlios.de/

Gary W.