Forum: Ruby Is there a way to clear the contents of a terminal?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
John M. (Guest)
on 2006-01-04 08:50
(Received via mailing list)
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-?
Gene T. (Guest)
on 2006-01-04 10:08
(Received via mailing list)
John M. wrote:
> 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-?
>
> --
> John M.
> MSc (DIC)
> 07739 171 531

Control-L
John M. (Guest)
on 2006-01-04 11:08
(Received via mailing list)
I should've made myself clearer. Is there a way to do this via a ruby
command?

On Wed, 4 Jan 2006 17:07:58 +0900
Ross B. (Guest)
on 2006-01-04 12:15
(Received via mailing list)
On Wed, 04 Jan 2006 06:49:14 -0000, John M. 
<removed_email_address@domain.invalid>
wrote:

> 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"
Ross B. (Guest)
on 2006-01-04 12:18
(Received via mailing list)
On Wed, 04 Jan 2006 10:01:56 -0000, Ross B.
<removed_email_address@domain.invalid> wrote:

> this should work:
>
> 	puts "\e[2J"
>

or "\e[2J\e[0;0H" to home the cursor too.
Steve L. (Guest)
on 2006-01-04 19:12
(Received via mailing list)
On Wednesday 04 January 2006 01:49 am, John M. wrote:
> 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 L.
http://www.troubleshooters.com
removed_email_address@domain.invalid
Steve L. (Guest)
on 2006-01-04 19:12
(Received via mailing list)
On Wednesday 04 January 2006 04:07 am, John M. wrote:
> I should've made myself clearer. Is there a way to do this via a ruby
> command?

print 12.to_s

I still like 25 newlines better.

SteveT
Steve L. (Guest)
on 2006-01-04 19:14
(Received via mailing list)
On Wednesday 04 January 2006 09:10 am, Steve L. wrote:
> On Wednesday 04 January 2006 04:07 am, John M. wrote:
> > I should've made myself clearer. Is there a way to do this via a ruby
> > command?
>
> print 12.to_s

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

SteveT

Steve L.
http://www.troubleshooters.com
removed_email_address@domain.invalid
James G. (Guest)
on 2006-01-04 19:14
(Received via mailing list)
On Jan 4, 2006, at 9:00 AM, Steve L. wrote:

> of Pascal chr().
12.chr ?

James Edward G. II
Timothy H. (Guest)
on 2006-01-04 19:14
(Received via mailing list)
Steve L. wrote:
>
> Dooohhhhh. This would have printed "12". What I needed some sort of equivalent
> of Pascal chr().
>
Something like this?

rb$ ri Integer.chr
------------------------------------------------------------ Integer#chr
      int.chr    => string
------------------------------------------------------------------------
      Returns a string containing the ASCII character represented by the
      receiver's value.

         65.chr    #=> "A"
         ?a.chr    #=> "a"
         230.chr   #=> "\346"
Ross B. (Guest)
on 2006-01-04 19:50
(Received via mailing list)
On Wed, 04 Jan 2006 15:00:45 -0000, Steve L. 
<removed_email_address@domain.invalid>
wrote:

>
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?
James G. (Guest)
on 2006-01-04 19:53
(Received via mailing list)
On Jan 4, 2006, at 11:47 AM, Ross B. wrote:

>>
>> 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?

Correct on all accounts.  :)

James Edward G. II
Yohanes S. (Guest)
on 2006-01-04 20:21
(Received via mailing list)
John M. <removed_email_address@domain.invalid> writes:

> 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-?
>
> --
> John M.
> MSc (DIC)
> 07739 171 531

--------------------------------------------------------------------------------
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.
Ross B. (Guest)
on 2006-01-04 20:33
(Received via mailing list)
On Wed, 04 Jan 2006 17:51:22 -0000, James Edward G. II
<removed_email_address@domain.invalid> wrote:

>>>>
>
> Correct on all accounts.  :)
>

Wow, can't believe I remembered that... Now if only I could retain the
_useful_ stuff :D
Eric H. (Guest)
on 2006-01-04 20:45
(Received via mailing list)
On Jan 3, 2006, at 10:49 PM, John M. wrote:

> 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

--
Eric H. - removed_email_address@domain.invalid - http://segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
This topic is locked and can not be replied to.