Is there a way to clear the contents of a terminal?

J

John Maclean

Hi Chaps,

MATLAB uses cls, in a bash shell I'd use clear. Is there a way to clear the contents of a terminal -safely-?
 
J

John Maclean

I should've made myself clearer. Is there a way to do this via a ruby command?
 
R

Ross Bamford

Hi Chaps,

MATLAB uses cls, in a bash shell I'd use clear. Is there a way to clear
the contents of a terminal -safely-?

Safe, sure, reliable, probably not. But on most 'modern' systems I guess
this should work:

puts "\e[2J"
 
R

Ross Bamford

Hi Chaps,

MATLAB uses cls, in a bash shell I'd use clear. Is there a way to clear
the contents of a terminal -safely-?

Safe, sure, reliable, probably not. But on most 'modern' systems I guess
this should work:

puts "\e[2J"

or "\e[2J\e[0;0H" to home the cursor too.
 
S

Steve Litt

Hi Chaps,

MATLAB uses cls, in a bash shell I'd use clear. Is there a way to clear the
contents of a terminal -safely-?

In the UMENU program, I do this:

puts "\n" * 24

Crude, but effective, and has the advantage of being totally portable -- even
on an ancient teletype terminal. If you use different terminal resolutions,
then it would be

print "\n" * lines_per_screen

SteveT


Steve Litt
http://www.troubleshooters.com
(e-mail address removed)
 
R

Ross Bamford

Dooohhhhh. This would have printed "12". What I needed some sort of
equivalent
of Pascal chr().

IIRC ASCII 0x0C is form-feed, right. Doesn't Ruby support "\f"? (seems to).
But I don't think it'll clear the terminal, will it?
 
J

James Edward Gray II

IIRC ASCII 0x0C is form-feed, right. Doesn't Ruby support "\f"?
(seems to).
But I don't think it'll clear the terminal, will it?

Correct on all accounts. :)

James Edward Gray II
 
Y

Yohanes Santoso

John Maclean said:
Hi Chaps,

MATLAB uses cls, in a bash shell I'd use clear. Is there a way to clear the contents of a terminal -safely-?

--------------------------------------------------------------------------------
CLEAR_TERMINAL_CMD = `/usr/bin/clear`

def clear_screen
print CLEAR_TERMINAL_CMD
end

clear_screen
--------------------------------------------------------------------------------

The above is the simplest, most portable solution. Sadly, most
portable here extends only to within unix system. There is no single
solution that works with both win32 and unix.

Other solutions (for unix):

1. Print \n a number of times. You can't tell how many \n you need to
print. Some environments gives you the LINES env. variable, but not
all.

2. Hard code some terminal escape sequence. Definitely not portable
between different terminal types. /usr/bin/clear uses curses which
in turn consults the terminfo database for the correct escape
sequence on the current terminal.

3. Link to curses itself. Not simple.

4. Consult terminfo database yourself. Location is not standard and
most probably not as simple as the /usr/bin/clear solution.

YS.
 
E

Eric Hodel

MATLAB uses cls, in a bash shell I'd use clear. Is there a way to
clear the contents of a terminal -safely-?

require 'curses'

Curses.init_screen
Curses.clear
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top