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.
508861510639b56d61eb30e6a28b01d9?d=identicon&s=25 John Maclean (Guest)
on 2006-01-04 07: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-?
9dfe8c734b0f9b37a4e218425c0a2138?d=identicon&s=25 Gene Tani (Guest)
on 2006-01-04 09:08
(Received via mailing list)
John Maclean 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 Maclean
> MSc (DIC)
> 07739 171 531

Control-L
508861510639b56d61eb30e6a28b01d9?d=identicon&s=25 John Maclean (Guest)
on 2006-01-04 10: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
82e62c756d89bc6fa0a0a2d7f2b1e617?d=identicon&s=25 Ross Bamford (Guest)
on 2006-01-04 11:15
(Received via mailing list)
On Wed, 04 Jan 2006 06:49:14 -0000, John Maclean <info@jayeola.org>
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"
82e62c756d89bc6fa0a0a2d7f2b1e617?d=identicon&s=25 Ross Bamford (Guest)
on 2006-01-04 11:18
(Received via mailing list)
On Wed, 04 Jan 2006 10:01:56 -0000, Ross Bamford
<rosco@roscopeco.remove.co.uk> wrote:

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

or "\e[2J\e[0;0H" to home the cursor too.
1b62a85b59ccab03b84ee5ec378f75b4?d=identicon&s=25 Steve Litt (Guest)
on 2006-01-04 18:12
(Received via mailing list)
On Wednesday 04 January 2006 01:49 am, John Maclean 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 Litt
http://www.troubleshooters.com
slitt@troubleshooters.com
1b62a85b59ccab03b84ee5ec378f75b4?d=identicon&s=25 Steve Litt (Guest)
on 2006-01-04 18:12
(Received via mailing list)
On Wednesday 04 January 2006 04:07 am, John Maclean 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
1b62a85b59ccab03b84ee5ec378f75b4?d=identicon&s=25 Steve Litt (Guest)
on 2006-01-04 18:14
(Received via mailing list)
On Wednesday 04 January 2006 09:10 am, Steve Litt wrote:
> On Wednesday 04 January 2006 04:07 am, John Maclean 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 Litt
http://www.troubleshooters.com
slitt@troubleshooters.com
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-01-04 18:14
(Received via mailing list)
On Jan 4, 2006, at 9:00 AM, Steve Litt wrote:

> of Pascal chr().
12.chr ?

James Edward Gray II
37a3c73ffbf864e4b28f7f2384ee12ce?d=identicon&s=25 Timothy Hunter (tim-hunter)
on 2006-01-04 18:14
(Received via mailing list)
Steve Litt 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"
82e62c756d89bc6fa0a0a2d7f2b1e617?d=identicon&s=25 Ross Bamford (Guest)
on 2006-01-04 18:50
(Received via mailing list)
On Wed, 04 Jan 2006 15:00:45 -0000, Steve Litt <slitt@earthlink.net>
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?
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-01-04 18:53
(Received via mailing list)
On Jan 4, 2006, at 11:47 AM, Ross Bamford 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 Gray II
B000982a23d5c6a34292902caf225dd7?d=identicon&s=25 Yohanes Santoso (Guest)
on 2006-01-04 19:21
(Received via mailing list)
John Maclean <info@jayeola.org> 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 Maclean
> 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.
82e62c756d89bc6fa0a0a2d7f2b1e617?d=identicon&s=25 Ross Bamford (Guest)
on 2006-01-04 19:33
(Received via mailing list)
On Wed, 04 Jan 2006 17:51:22 -0000, James Edward Gray II
<james@grayproductions.net> wrote:

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

Wow, can't believe I remembered that... Now if only I could retain the
_useful_ stuff :D
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2006-01-04 19:45
(Received via mailing list)
On Jan 3, 2006, at 10:49 PM, John Maclean 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 Hodel - drbrain@segment7.net - http://segment7.net
This implementation is HODEL-HASH-9600 compliant

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