Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Why is there no platform independent way of clearing a terminal?

Reply
Thread Tools

Why is there no platform independent way of clearing a terminal?

 
 
Daniel Fetchinson
Guest
Posts: n/a
 
      07-27-2010
Hi folks,

If I'm only interested in linux and windows I know I can do

################################
import os
import platform

if platform.system( ) == 'Linux':
clear = 'clear'
else:
clear = 'cls'

os.system( clear )
################################

or something equivalent using os.name and friends, but was wondering
why there is no platform independent way (i.e. the platform dependence
is taken care of by the python stdlib) of clearing a terminal. Sure,
there are many different terminals and many different operating
systems but in many areas python managed to hide all these
complexities behind a well defined API.

Why was clearing a terminal left out?

Cheers,
Daniel


--
Psss, psss, put it down! - http://www.cafepress.com/putitdown
 
Reply With Quote
 
 
 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      07-27-2010
Daniel Fetchinson a écrit :
> Hi folks,
>
> If I'm only interested in linux and windows I know I can do
>
> ################################
> import os
> import platform
>
> if platform.system( ) == 'Linux':
> clear = 'clear'
> else:
> clear = 'cls'
>
> os.system( clear )
> ################################
>
> or something equivalent using os.name and friends, but was wondering
> why there is no platform independent way (i.e. the platform dependence
> is taken care of by the python stdlib) of clearing a terminal. Sure,
> there are many different terminals and many different operating
> systems but in many areas python managed to hide all these
> complexities behind a well defined API.
>
> Why was clearing a terminal left out?
>


What you're talking about is a shell, not a terminal (a terminal is a
physical device). And the shell is not necessarily part of the OS itself
(there's no shortage of shells for unices / linux systems), so it
doesn't belong to the os or platform modules.

FWIW, I can't tell for sure since I never used any other shell than
bash, but I'm not sure your above code is garanteed to work on each and
any possible unix shell.
 
Reply With Quote
 
 
 
 
Grant Edwards
Guest
Posts: n/a
 
      07-27-2010
On 2010-07-27, Bruno Desthuilliers <(E-Mail Removed)> wrote:
> Daniel Fetchinson a ?crit :
>> Hi folks,
>>
>> If I'm only interested in linux and windows I know I can do
>>
>> ################################
>> import os
>> import platform
>>
>> if platform.system( ) == 'Linux':
>> clear = 'clear'
>> else:
>> clear = 'cls'
>>
>> os.system( clear )
>> ################################
>>
>> or something equivalent using os.name and friends, but was wondering
>> why there is no platform independent way (i.e. the platform dependence
>> is taken care of by the python stdlib) of clearing a terminal. Sure,
>> there are many different terminals and many different operating
>> systems but in many areas python managed to hide all these
>> complexities behind a well defined API.
>>
>> Why was clearing a terminal left out?
>>

>
> What you're talking about is a shell, not a terminal (a terminal is a
> physical device).


No, what he's talking about is clearing a terminal (or a terminal
emulator). They both work the same, the only difference is whether
the terminal software is running on dedicated hardware or on
general-purpose hardware.

> And the shell is not necessarily part of the OS itself
> (there's no shortage of shells for unices / linux systems), so it
> doesn't belong to the os or platform modules.


True, but clearing a terminal or terminal emulator has nothing to do
with the shell. It's done using an in-band control/escape sequence
that's indepedent of the shell being used. His example accomplishes
this using an executable named 'clear' which knows how to use
terminfo/termcap (I forget which one) to send the proper escape
sequence to the terminal.

> FWIW, I can't tell for sure since I never used any other shell than
> bash, but I'm not sure your above code is garanteed to work on each
> and any possible unix shell.


Again, the shell is irrelevent.

--
Grant Edwards grant.b.edwards Yow! Zippy's brain cells
at are straining to bridge
gmail.com synapses ...
 
Reply With Quote
 
Phil Thompson
Guest
Posts: n/a
 
      07-27-2010
On Tue, 27 Jul 2010 16:02:23 +0200, Bruno Desthuilliers
<(E-Mail Removed)> wrote:
> Daniel Fetchinson a écrit :
>> Hi folks,
>>
>> If I'm only interested in linux and windows I know I can do
>>
>> ################################
>> import os
>> import platform
>>
>> if platform.system( ) == 'Linux':
>> clear = 'clear'
>> else:
>> clear = 'cls'
>>
>> os.system( clear )
>> ################################
>>
>> or something equivalent using os.name and friends, but was wondering
>> why there is no platform independent way (i.e. the platform dependence
>> is taken care of by the python stdlib) of clearing a terminal. Sure,
>> there are many different terminals and many different operating
>> systems but in many areas python managed to hide all these
>> complexities behind a well defined API.
>>
>> Why was clearing a terminal left out?
>>

>
> What you're talking about is a shell, not a terminal (a terminal is a
> physical device). And the shell is not necessarily part of the OS itself


> (there's no shortage of shells for unices / linux systems), so it
> doesn't belong to the os or platform modules.
>
> FWIW, I can't tell for sure since I never used any other shell than
> bash, but I'm not sure your above code is garanteed to work on each and
> any possible unix shell.


Sorry, but that is completely wrong - the shell is irrelevant.

"clear" is just a normal command line program that queries the
termcap/terminfo database (possibly via the curses library) for the
terminal specific sequence of characters that will clear the screen. It
then writes those characters to stdout. The terminal, or (more usually
these days) terminal emulator, then interprets those characters and takes
the appropriate action.

I'm not sure what the POSIX status of the clear command is, but I'd be
surprised if it wasn't present on a UNIX/Linux system of any vintage.

Phil

 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      07-27-2010
Grant Edwards a écrit :
> On 2010-07-27, Bruno Desthuilliers <(E-Mail Removed)> wrote:
>> Daniel Fetchinson a ?crit :

(snip)
>>> Why was clearing a terminal left out?
>>>

>> What you're talking about is a shell, not a terminal (a terminal is a
>> physical device).

>
> No, what he's talking about is clearing a terminal (or a terminal
> emulator). They both work the same, the only difference is whether
> the terminal software is running on dedicated hardware or on
> general-purpose hardware.


(snip)

I stand corrected.
 
Reply With Quote
 
Daniel Fetchinson
Guest
Posts: n/a
 
      07-27-2010
>>> Hi folks,
>>>
>>> If I'm only interested in linux and windows I know I can do
>>>
>>> ################################
>>> import os
>>> import platform
>>>
>>> if platform.system( ) == 'Linux':
>>> clear = 'clear'
>>> else:
>>> clear = 'cls'
>>>
>>> os.system( clear )
>>> ################################
>>>
>>> or something equivalent using os.name and friends, but was wondering
>>> why there is no platform independent way (i.e. the platform dependence
>>> is taken care of by the python stdlib) of clearing a terminal. Sure,
>>> there are many different terminals and many different operating
>>> systems but in many areas python managed to hide all these
>>> complexities behind a well defined API.
>>>
>>> Why was clearing a terminal left out?
>>>

>>
>> What you're talking about is a shell, not a terminal (a terminal is a
>> physical device). And the shell is not necessarily part of the OS itself

>
>> (there's no shortage of shells for unices / linux systems), so it
>> doesn't belong to the os or platform modules.
>>
>> FWIW, I can't tell for sure since I never used any other shell than
>> bash, but I'm not sure your above code is garanteed to work on each and
>> any possible unix shell.

>
> Sorry, but that is completely wrong - the shell is irrelevant.
>
> "clear" is just a normal command line program that queries the
> termcap/terminfo database (possibly via the curses library) for the
> terminal specific sequence of characters that will clear the screen. It
> then writes those characters to stdout. The terminal, or (more usually
> these days) terminal emulator, then interprets those characters and takes
> the appropriate action.
>
> I'm not sure what the POSIX status of the clear command is, but I'd be
> surprised if it wasn't present on a UNIX/Linux system of any vintage.



After getting the technicalities out of the way, maybe I should have asked:

Is it only me or others would find a platform independent python API
to clear the terminal useful?

Cheers,
Daniel


--
Psss, psss, put it down! - http://www.cafepress.com/putitdown
 
Reply With Quote
 
John Nagle
Guest
Posts: n/a
 
      07-27-2010
On 7/27/2010 7:44 AM, Bruno Desthuilliers wrote:
> Grant Edwards a écrit :
>> On 2010-07-27, Bruno Desthuilliers
>> <(E-Mail Removed)> wrote:
>>> Daniel Fetchinson a ?crit :

> (snip)
>>>> Why was clearing a terminal left out?
>>>>
>>> What you're talking about is a shell, not a terminal (a terminal is a
>>> physical device).

>>
>> No, what he's talking about is clearing a terminal (or a terminal
>> emulator). They both work the same, the only difference is whether
>> the terminal software is running on dedicated hardware or on
>> general-purpose hardware.

>
> (snip)
>
> I stand corrected.


I immediately thought of using the "curses" module, but that's
UNIX-only, or at least it's not in the ActiveState Python distro.

John Nagle
 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      07-27-2010
On 2010-07-27, Daniel Fetchinson <(E-Mail Removed)> wrote:

> After getting the technicalities out of the way, maybe I should have asked:
>
> Is it only me or others would find a platform independent python API
> to clear the terminal useful?


I write a lot of command-line programs, and I can't remember the last
time time I wanted to clear a terminal. But then again, pretty much
all of my programs are designed so that they can be used as filters.

About 10 years ago I did need to do a text-mode UI (menus, popups,
text-entry, etc.), and I used newt.

--
Grant Edwards grant.b.edwards Yow! Is a tattoo real, like
at a curb or a battleship?
gmail.com Or are we suffering in
Safeway?
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      07-27-2010
On 7/27/2010 12:58 PM, Daniel Fetchinson wrote:

> After getting the technicalities out of the way, maybe I should have asked:
>
> Is it only me or others would find a platform independent python API
> to clear the terminal useful?


One problem is, Where would you put it? The OS module is for system
calls, mostly based on posix. The system call involved in clearing a
terminal is a write string call. *nix puts terminal control in a
separate library.

Another is, what next? clear_line? Pretty soon, we are back to curses.

Still another problem is that most of us do not have terminals; we have
screens and use them as such. OS-independent full-screen graphics/game
libraries have clear screen commands. Similary, GUI systems have means
of clearing text and canvas widgets, but should not be able to clear the
whole screen. The turtle module has a clear command for its canvas,
which would be the same regardless of underlying gui. So we already have
several OS independent clear commands.

On Windows, the DOS clr command only works withing a text-mode command
window (once called a dos window). The same thing (os.system('clr')
within an IDLE shell uselessly flashes a blank command window, which
then disappears. Yeah, it is too bad windows did not use the obvious
'clear' like everyone? else. If command windows still imitate or can be
set to imitate ansi terminals, then I would think curses is your best bet.

--
Terry Jan Reedy

 
Reply With Quote
 
Tim Harig
Guest
Posts: n/a
 
      07-27-2010
On 2010-07-27, John Nagle <(E-Mail Removed)> wrote:
> On 7/27/2010 7:44 AM, Bruno Desthuilliers wrote:
>> Grant Edwards a écrit :
>>> On 2010-07-27, Bruno Desthuilliers
>>> <(E-Mail Removed)> wrote:
>>>> Daniel Fetchinson a ?crit :

>> (snip)
>>>>> Why was clearing a terminal left out?
>>>>>
>>>> What you're talking about is a shell, not a terminal (a terminal is a
>>>> physical device).
>>>
>>> No, what he's talking about is clearing a terminal (or a terminal
>>> emulator). They both work the same, the only difference is whether
>>> the terminal software is running on dedicated hardware or on
>>> general-purpose hardware.

>>
>> (snip)
>>
>> I stand corrected.

>
> I immediately thought of using the "curses" module, but that's
> UNIX-only, or at least it's not in the ActiveState Python distro.


pdcurses:

http://pdcurses.sourceforge.net/

is a cross platform curses implementation that is available for Windows.
I wonder how difficult it would be to embed into the Python curses module
as a backup for systems where curses is not natively available. This would
allow Python to provide cross platform charactor mode manipulation.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Platform-independent way for storing application data Jan Thomä Java 15 02-19-2009 04:19 PM
Launching an independent Python program in a cross-platform way (including mac) =?iso-8859-1?B?QW5kcuk=?= Python 8 05-01-2007 11:47 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Platform independent way to play an mp3 file Gandalf Python 1 08-18-2004 11:13 AM
Platform-independent way to refer to execute path MK Python 1 06-25-2003 05:43 PM



Advertisments