Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Why is there no platform independent way of clearing a terminal? (http://www.velocityreviews.com/forums/t729305-why-is-there-no-platform-independent-way-of-clearing-a-terminal.html)

Daniel Fetchinson 07-27-2010 12:54 PM

Why is there no platform independent way of clearing a terminal?
 
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

Bruno Desthuilliers 07-27-2010 02:02 PM

Re: Why is there no platform independent way of clearing a terminal?
 
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.

Grant Edwards 07-27-2010 02:32 PM

Re: Why is there no platform independent way of clearing a terminal?
 
On 2010-07-27, Bruno Desthuilliers <bruno.42.desthuilliers@websiteburo.invalid> 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 ...

Phil Thompson 07-27-2010 02:36 PM

Re: Why is there no platform independent way of clearing aterminal?
 
On Tue, 27 Jul 2010 16:02:23 +0200, Bruno Desthuilliers
<bruno.42.desthuilliers@websiteburo.invalid> 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


Bruno Desthuilliers 07-27-2010 02:44 PM

Re: Why is there no platform independent way of clearing a terminal?
 
Grant Edwards a écrit :
> On 2010-07-27, Bruno Desthuilliers <bruno.42.desthuilliers@websiteburo.invalid> 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.

Daniel Fetchinson 07-27-2010 04:58 PM

Re: Why is there no platform independent way of clearing a terminal?
 
>>> 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

John Nagle 07-27-2010 05:02 PM

Re: Why is there no platform independent way of clearing a terminal?
 
On 7/27/2010 7:44 AM, Bruno Desthuilliers wrote:
> Grant Edwards a écrit :
>> On 2010-07-27, Bruno Desthuilliers
>> <bruno.42.desthuilliers@websiteburo.invalid> 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

Grant Edwards 07-27-2010 05:13 PM

Re: Why is there no platform independent way of clearing a terminal?
 
On 2010-07-27, Daniel Fetchinson <fetchinson@googlemail.com> 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?

Terry Reedy 07-27-2010 06:01 PM

Re: Why is there no platform independent way of clearing a terminal?
 
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


Tim Harig 07-27-2010 09:12 PM

Re: Why is there no platform independent way of clearing a terminal?
 
On 2010-07-27, John Nagle <nagle@animats.com> wrote:
> On 7/27/2010 7:44 AM, Bruno Desthuilliers wrote:
>> Grant Edwards a écrit :
>>> On 2010-07-27, Bruno Desthuilliers
>>> <bruno.42.desthuilliers@websiteburo.invalid> 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.


All times are GMT. The time now is 08:55 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.