Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-10-04 Thread eryk sun
On Tue, Oct 4, 2016 at 2:22 PM, Random832  wrote:
> On Wed, Sep 28, 2016, at 23:36, Chris Angelico wrote:
>> On Thu, Sep 29, 2016 at 12:04 PM, Steven D'Aprano 
>> wrote:
>> > (Also, it seems a shame that Ctrl-D is EOF in Linux and Mac, but Windows
>> > is Ctrl-Z + Return. Can that be standardized to Ctrl-D everywhere?)
>>
>> Sadly, I suspect not. If you're running in the default Windows
>> terminal emulator (the one a normal user will get by invoking
>> cmd.exe), you're running under a lot of restrictions, and I believe
>> one of them is that you can't get Ctrl-D without an enter.
>
> Well, we could read _everything_ in character-at-a-time mode, and
> implement our own line editing. In effect, that's what readline is
> doing.

3.6+ switched to calling ReadConsoleW, which allows using a 32-bit
control mask to indicate which ASCII control codes should terminate a
read. The control character is left in the input string, so it's
possible to define custom behavior for multiple control characters.
Here's a basic ctypes example of how this feature works. In each case,
after calling ReadConsoleW I enter "spam" and then type a control
character to terminate the read.

import sys
import msvcrt
import ctypes

kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
ReadConsoleW = kernel32.ReadConsoleW

CTRL_MASK = 2 ** 32 - 1 # all ctrl codes

hin = msvcrt.get_osfhandle(sys.stdin.fileno())
buf = (ctypes.c_wchar * 10)(*('-' * 10))
pn = (ctypes.c_ulong * 1)()
ctl = (ctypes.c_ulong * 4)(16, 0, CTRL_MASK, 0)

>>> # Ctrl+2 or Ctrl+@ (i.e. NUL)
... ret = ReadConsoleW(hin, buf, 10, pn, ctl); print()
spam
>>> buf[:]
'spam\x00-'

>>> # Ctrl+D
... ret = ReadConsoleW(hin, buf, 10, pn, ctl); print()
spam
>>> buf[:]
'spam\x04-'

>>> # Ctrl+[
... ret = ReadConsoleW(hin, buf, 10, pn, ctl); print()
spam
>>> buf[:]
'spam\x1b-'

This could be used to implement Ctrl+D and Ctrl+L support in
PyOS_Readline. Supporting Ctrl+L to work like GNU readline wouldn't be
a trivial one-liner, but it's doable. It has to clear the screen and
also write the input (except the Ctrl+L) back to the input buffer.

> The main consequence of reading everything in character-at-a-time mode
> is that we'd have to implement everything ourselves, and the line
> editing you get *without* doing it yourself is somewhat nicer on Windows
> than on Linux (it supports cursor movement, inserting characters, and
> history).

Line-input mode also supports F7 for a history popup window to select
a previous command; Ctrl+F to search the screen text; text selection
(e.g. shift+arrows or Ctrl+A); copy/paste via Ctrl+C and Ctrl+V (or
Ctrl+Insert and Shift+Insert); and parameterized input aliases ($1-$9
and $* for parameters).

https://technet.microsoft.com/en-us/library/mt427362
https://technet.microsoft.com/en-us/library/cc753867

>> "Bash on Ubuntu on windows" responds to CTRL+D just fine. I don't really
>> know how it works, but it looks like it is based on the Windows terminal
>> emulator.
>
> It runs inside it, but it's using the "Windows Subsystem for Linux",
> which (I assume) reads character-at-a-time and feeds it to a Unix-like
> terminal driver, (which Bash then has incidentally also put in
> character-at-a-time mode by using readline - to see what you get on WSL
> *without* doing this, try running "cat" under bash.exe)

Let's take a look at how WSL modifies the console's global state.
Here's a simple function to print the console's input and output modes
and codepages, which we can call in the background to monitor the
console state:

def report():
hin = msvcrt.get_osfhandle(0)
hout = msvcrt.get_osfhandle(1)
modeIn = (ctypes.c_ulong * 1)()
modeOut = (ctypes.c_ulong * 1)()
kernel32.GetConsoleMode(hin, modeIn)
kernel32.GetConsoleMode(hout, modeOut)
cpIn = kernel32.GetConsoleCP()
cpOut = kernel32.GetConsoleOutputCP()
print('\nmodeIn=%x, modeOut=%x, cpIn=%d, cpOut=%d' %
  (modeIn[0], modeOut[0], cpIn, cpOut))

def monitor():
report()
t = threading.Timer(10, monitor, ())
t.start()

>>> monitor(); subprocess.call('bash.exe')

modeIn=f7, modeOut=3, cpIn=437, cpOut=437
...
modeIn=2d8, modeOut=f, cpIn=65001, cpOut=65001

See the following page for a description of the mode flags:

https://msdn.microsoft.com/en-us/library/ms686033

The output mode changed from 0x3 to 0xf, enabling

DISABLE_NEWLINE_AUTO_RETURN (0x8)
ENABLE_VIRTUAL_TERMINAL_PROCESSING (0x4)

The input mode changed from 0xf7 to 0x2d8, enabling

ENABLE_VIRTUAL_TERMINAL_INPUT (0x200)
ENABLE_WINDOW_INPUT (0x8, probably for SIGWINCH)

and disabling

ENABLE_INSERT_MODE (0x20)
ENABLE_ECHO_INPUT (0x4)
ENABLE_LINE_INPUT (0x2)
ENABLE_PROCESSED_INPUT (0x1)

So you're correct that it's basically using a 

Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-10-04 Thread Random832
On Wed, Sep 28, 2016, at 23:36, Chris Angelico wrote:
> On Thu, Sep 29, 2016 at 12:04 PM, Steven D'Aprano 
> wrote:
> > (Also, it seems a shame that Ctrl-D is EOF in Linux and Mac, but Windows
> > is Ctrl-Z + Return. Can that be standardized to Ctrl-D everywhere?)
> 
> Sadly, I suspect not. If you're running in the default Windows
> terminal emulator (the one a normal user will get by invoking
> cmd.exe), you're running under a lot of restrictions, and I believe
> one of them is that you can't get Ctrl-D without an enter.

Well, we could read _everything_ in character-at-a-time mode, and
implement our own line editing. In effect, that's what readline is
doing.

The main consequence of reading everything in character-at-a-time mode
is that we'd have to implement everything ourselves, and the line
editing you get *without* doing it yourself is somewhat nicer on Windows
than on Linux (it supports cursor movement, inserting characters, and
history).

On Wed, Sep 28, 2016, at 23:41, אלעזר wrote:
> "Bash on Ubuntu on windows" responds to CTRL+D just fine. I don't really
> know how it works, but it looks like it is based on the Windows terminal
> emulator.

It runs inside it, but it's using the "Windows Subsystem for Linux",
which (I assume) reads character-at-a-time and feeds it to a Unix-like
terminal driver, (which Bash then has incidentally also put in
character-at-a-time mode by using readline - to see what you get on WSL
*without* doing this, try running "cat" under bash.exe)
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-29 Thread Stephan Houben
Hi JM,

Windows 7 Enterprise
"Microsoft Windows [Version 6.1.7601]"

I am running Python directly from the shortcut created on installation.
But starting it from cmd.exe has the same effect.

Codepage is 437 , this may be relevant?

I just tried it on a Windows 10 PC, there it has the same effect.

Stephan


2016-09-29 9:12 GMT+02:00 João Matos :

> Hello,
>
> I tried on Python 2.7.10 and Python 3.5.2 and Ctrl-L doesn't work on both.
> I tried on 2 PCs with Windows 7 and none of them worked.
>
> What is your Windows version? Are you trying on the cmd.exe console or PS?
>
>
> Best regards,
>
> JM
>
> quinta-feira, 29 de Setembro de 2016 às 08:09:13 UTC+1, Stephan Houben
> escreveu:
>
>> Hi all,
>>
>> I just tried with this official Python binary:
>> Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32
>> bit (Intel)] on win32
>>
>> and CTRL-L for sure does clear the window. It just doesn't then move the
>> prompt to the top, so you end up with a bunch of empty lines, followed by
>> the prompt.
>>
>> Stephan
>>
>> 2016-09-29 8:50 GMT+02:00 João Matos :
>>
>>> Hello,
>>>
>>> Yes, Ctrl-L doesn't clear the screen on Windows.
>>> Making Ctrl-L clear the screen would be a good solution (no need for a
>>> clear screen command).
>>>
>>>
>>> Best regards,
>>>
>>> JM
>>>
>>> quinta-feira, 29 de Setembro de 2016 às 03:06:26 UTC+1, Steven D'Aprano
>>> escreveu:
>>>
 On Tue, Sep 27, 2016 at 10:05:16AM -0700, João Matos wrote:
 > Hello,
 >
 >
 > It doesn't work in Windows.

 What is "it"? Are you talking about Ctrl-L to clear the screen?


 Perhaps we should start by adding Ctrl-L as a standard way to clear the
 Python REPL, in the same way that Ctrl-C is the standard way to
 interrupt the interpreter regardless of whether you are using Linux,
 Mac
 or Windows.

 (Also, it seems a shame that Ctrl-D is EOF in Linux and Mac, but
 Windows
 is Ctrl-Z + Return. Can that be standardized to Ctrl-D everywhere?)


 --
 Steve
 ___
 Python-ideas mailing list
 python...@python.org
 https://mail.python.org/mailman/listinfo/python-ideas
 Code of Conduct: http://python.org/psf/codeofconduct/

>>>
>>> ___
>>> Python-ideas mailing list
>>> python...@python.org
>>> https://mail.python.org/mailman/listinfo/python-ideas
>>> Code of Conduct: http://python.org/psf/codeofconduct/
>>>
>>
>>
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-29 Thread João Matos
Hello,

I tried on Python 2.7.10 and Python 3.5.2 and Ctrl-L doesn't work on both.
I tried on 2 PCs with Windows 7 and none of them worked.

What is your Windows version? Are you trying on the cmd.exe console or PS?


Best regards,

JM

quinta-feira, 29 de Setembro de 2016 às 08:09:13 UTC+1, Stephan Houben 
escreveu:

> Hi all,
>
> I just tried with this official Python binary:
> Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 
> bit (Intel)] on win32
>
> and CTRL-L for sure does clear the window. It just doesn't then move the 
> prompt to the top, so you end up with a bunch of empty lines, followed by 
> the prompt.
>
> Stephan
>
> 2016-09-29 8:50 GMT+02:00 João Matos :
>
>> Hello,
>>
>> Yes, Ctrl-L doesn't clear the screen on Windows.
>> Making Ctrl-L clear the screen would be a good solution (no need for a 
>> clear screen command).
>>
>>
>> Best regards,
>>
>> JM
>>
>> quinta-feira, 29 de Setembro de 2016 às 03:06:26 UTC+1, Steven D'Aprano 
>> escreveu:
>>
>>> On Tue, Sep 27, 2016 at 10:05:16AM -0700, João Matos wrote: 
>>> > Hello, 
>>> > 
>>> > 
>>> > It doesn't work in Windows. 
>>>
>>> What is "it"? Are you talking about Ctrl-L to clear the screen? 
>>>
>>>
>>> Perhaps we should start by adding Ctrl-L as a standard way to clear the 
>>> Python REPL, in the same way that Ctrl-C is the standard way to 
>>> interrupt the interpreter regardless of whether you are using Linux, Mac 
>>> or Windows. 
>>>
>>> (Also, it seems a shame that Ctrl-D is EOF in Linux and Mac, but Windows 
>>> is Ctrl-Z + Return. Can that be standardized to Ctrl-D everywhere?) 
>>>
>>>
>>> -- 
>>> Steve 
>>> ___ 
>>> Python-ideas mailing list 
>>> python...@python.org 
>>> https://mail.python.org/mailman/listinfo/python-ideas 
>>> Code of Conduct: http://python.org/psf/codeofconduct/ 
>>>
>>
>> ___
>> Python-ideas mailing list
>> python...@python.org 
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>
>___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-29 Thread eryk sun
On Thu, Sep 29, 2016 at 7:08 AM, Stephan Houben  wrote:
>
> I just tried with this official Python binary:
> Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit
> (Intel)] on win32
>
> and CTRL-L for sure does clear the window. It just doesn't then move the
> prompt to the top, so you end up with a bunch of empty lines, followed by
> the prompt.

You probably have pyreadline installed. It calls ReadConsoleInputW to
read low-level input records, bypassing the console's normal cooked
read. See the following file that defines the key binding:

https://github.com/pyreadline/pyreadline/blob/1.7/pyreadline/configuration/pyreadlineconfig.ini#L18

Unfortunately pyreadline is broken for non-ASCII input. It ignores the
Alt+Numpad record sequences used for non-ASCII characters.

Without having to implement readline module for Windows (personally, I
don't use it), support for Ctrl+L can be added relatively easily in
3.6+. ReadConsoleW takes a parameter to specify a mask of ASCII
control characters that terminate a read. The control character is
left in the buffer, so code just has to be written that looks for
various control characters to implement features such as a Ctrl+L
clear screen.
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-29 Thread Paul Moore
On 29 September 2016 at 08:08, Stephan Houben  wrote:
> I just tried with this official Python binary:
> Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit
> (Intel)] on win32
>
> and CTRL-L for sure does clear the window. It just doesn't then move the
> prompt to the top, so you end up with a bunch of empty lines, followed by
> the prompt.

Do you have pyreadline installed? I believe that intercepts things like CTRL-L.

Paul
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-29 Thread Stephan Houben
Hi all,

I just tried with this official Python binary:
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32
bit (Intel)] on win32

and CTRL-L for sure does clear the window. It just doesn't then move the
prompt to the top, so you end up with a bunch of empty lines, followed by
the prompt.

Stephan

2016-09-29 8:50 GMT+02:00 João Matos :

> Hello,
>
> Yes, Ctrl-L doesn't clear the screen on Windows.
> Making Ctrl-L clear the screen would be a good solution (no need for a
> clear screen command).
>
>
> Best regards,
>
> JM
>
> quinta-feira, 29 de Setembro de 2016 às 03:06:26 UTC+1, Steven D'Aprano
> escreveu:
>
>> On Tue, Sep 27, 2016 at 10:05:16AM -0700, João Matos wrote:
>> > Hello,
>> >
>> >
>> > It doesn't work in Windows.
>>
>> What is "it"? Are you talking about Ctrl-L to clear the screen?
>>
>>
>> Perhaps we should start by adding Ctrl-L as a standard way to clear the
>> Python REPL, in the same way that Ctrl-C is the standard way to
>> interrupt the interpreter regardless of whether you are using Linux, Mac
>> or Windows.
>>
>> (Also, it seems a shame that Ctrl-D is EOF in Linux and Mac, but Windows
>> is Ctrl-Z + Return. Can that be standardized to Ctrl-D everywhere?)
>>
>>
>> --
>> Steve
>> ___
>> Python-ideas mailing list
>> python...@python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>
> ___
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-29 Thread João Matos
Hello,

Yes, Ctrl-L doesn't clear the screen on Windows.
Making Ctrl-L clear the screen would be a good solution (no need for a 
clear screen command).


Best regards,

JM

quinta-feira, 29 de Setembro de 2016 às 03:06:26 UTC+1, Steven D'Aprano 
escreveu:

> On Tue, Sep 27, 2016 at 10:05:16AM -0700, João Matos wrote: 
> > Hello, 
> > 
> > 
> > It doesn't work in Windows. 
>
> What is "it"? Are you talking about Ctrl-L to clear the screen? 
>
>
> Perhaps we should start by adding Ctrl-L as a standard way to clear the 
> Python REPL, in the same way that Ctrl-C is the standard way to 
> interrupt the interpreter regardless of whether you are using Linux, Mac 
> or Windows. 
>
> (Also, it seems a shame that Ctrl-D is EOF in Linux and Mac, but Windows 
> is Ctrl-Z + Return. Can that be standardized to Ctrl-D everywhere?) 
>
>
> -- 
> Steve 
> ___ 
> Python-ideas mailing list 
> python...@python.org  
> https://mail.python.org/mailman/listinfo/python-ideas 
> Code of Conduct: http://python.org/psf/codeofconduct/ 
>
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-28 Thread אלעזר
"Bash on Ubuntu on windows" responds to CTRL+D just fine. I don't really
know how it works, but it looks like it is based on the Windows terminal
emulator.

Elazar

בתאריך יום ה׳, 29 בספט' 2016, 06:36, מאת Chris Angelico ‏:

> On Thu, Sep 29, 2016 at 12:04 PM, Steven D'Aprano 
> wrote:
> > (Also, it seems a shame that Ctrl-D is EOF in Linux and Mac, but Windows
> > is Ctrl-Z + Return. Can that be standardized to Ctrl-D everywhere?)
>
> Sadly, I suspect not. If you're running in the default Windows
> terminal emulator (the one a normal user will get by invoking
> cmd.exe), you're running under a lot of restrictions, and I believe
> one of them is that you can't get Ctrl-D without an enter.
>
> But it would be very nice to support both.
>
> ChrisA
> ___
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-28 Thread Oleg Broytman
On Thu, Sep 29, 2016 at 12:04:28PM +1000, Steven D'Aprano  
wrote:
> (Also, it seems a shame that Ctrl-D is EOF in Linux and Mac, but Windows 
> is Ctrl-Z + Return. Can that be standardized to Ctrl-D everywhere?)

   I don't think consistency should go that far. Consistency inside the
platform is more important than consistency between platforms, and other
w32 console programs understand [Ctrl]+[Z] as EOF and as far as I know
only [Ctrl]+[Z].

> -- 
> Steve

Oleg.
-- 
 Oleg Broytmanhttp://phdru.name/p...@phdru.name
   Programmers don't die, they just GOSUB without RETURN.
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-28 Thread Steven D'Aprano
On Tue, Sep 27, 2016 at 10:05:16AM -0700, João Matos wrote:
> Hello,
> 
> 
> It doesn't work in Windows.

What is "it"? Are you talking about Ctrl-L to clear the screen?


Perhaps we should start by adding Ctrl-L as a standard way to clear the 
Python REPL, in the same way that Ctrl-C is the standard way to 
interrupt the interpreter regardless of whether you are using Linux, Mac 
or Windows.

(Also, it seems a shame that Ctrl-D is EOF in Linux and Mac, but Windows 
is Ctrl-Z + Return. Can that be standardized to Ctrl-D everywhere?)


-- 
Steve
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-27 Thread João Matos
Hello,


It doesn't work in Windows.


Best regards,

JM


terça-feira, 27 de Setembro de 2016 às 16:40:42 UTC+1, Dennis Brakhane via 
Python-ideas escreveu:

> I don't know if it works on Windows, but at least in Linux pressing 
> Ctrl-L will do exactly what you describe (as long as the REPL uses 
> readline) 
>
> On 17.09.2016 12:51, João Matos wrote: 
> > Hello, 
> > 
> > I would like to suggest adding a clear command (not function) to Python. 
> > It's simple purpose would be to clear the REPL screen, leaving the >>> 
> > prompt at the top left of the screen. 
> > 
> > This is something very basic but also very useful for newbies learning 
> > Python from the REPL. 
> > After some trial and errors it is best to start with a clean screen. 
> > Clearing the screen helps clear your mind. 
> > 
> > Historically it is a common command in interpreted languages. 
> > 
> > Best regards, 
> > 
> > JM 
> > 
> > ___ 
> > Python-ideas mailing list 
> > python...@python.org  
> > https://mail.python.org/mailman/listinfo/python-ideas 
> > Code of Conduct: http://python.org/psf/codeofconduct/ 
>
>
>
>___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-20 Thread Greg Ewing

Terry Reedy wrote:
In the default 
mode with user code executed in a separate no-window process, there is 
currently no way for the child process to know the current size of 
Shell's tk text window in the parent process.


On unix it should be possible to let the child know if it's
connected through a pty rather than a pipe. I don't know
whether Windows has any equivalent notion, though.

--
Greg
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-20 Thread Terry Reedy

On 9/20/2016 9:31 AM, Steven D'Aprano wrote:

On Mon, Sep 19, 2016 at 06:18:38AM -0400, Terry Reedy wrote:

On 9/19/2016 4:31 AM, Paul Moore wrote:


shutil.get_terminal_size() is available on all platforms.


On windows, it works with Command Prompt and PowerShell but fails in
IDLE, as it must.


Why "must" it fail?


Good question.  When I wrote that, I had in mind the qualification 'in 
IDLE's default mode, as IDLE is currently structured'.  In the default 
mode with user code executed in a separate no-window process, there is 
currently no way for the child process to know the current size of 
Shell's tk text window in the parent process.  But this is not the real 
story.  See below.



What is it about the IDLE terminal that prevents it from emulating an
actual terminal, like all(?) the other terminal emulation programs
people use?


IDLE's Shell is based on a tk Text and by design is not a terminal 
emulation.  (Which terminal would it emulate?).  Note that with 
proportional fonts, a text editor does not really have character 
columns, though it can be sized according to some 'average' character 
width.  Also, IDLE is a development environment, not a run environment, 
and this impacts a few decisions.



I just opened my Konqueror file & web browser, gave the command "Show
Terminal Emulator", and ran Python:

py> import shutil
py> shutil.get_terminal_size()
os.terminal_size(columns=85, lines=10)

That's spot on perfectly correct. I then resized the window and tried it
again:

py> shutil.get_terminal_size()
os.terminal_size(columns=108, lines=13)


This is what I did and equivalently got with Command Prompt.


I then quit Python, and ran idle3.3 from the Konqueror terminal.
Unfortunately, get_terminal_size() returned the size of the *Konqueror*
terminal, instead of the IDLE terminal. I think that's a bug. I don't
know if its a bug in IDLE or get_terminal_size() or both.


If I run IDLE from Command Prompt, without or with the currently 
deprecated -n option, to run user code in the original IDLE process 
instead of a separate subprocess, I get the same -- the current size of 
the parent Command Prompt.


The reason is that shutil.get_terminal_size first looks for 
int(os.environ['COLUMNS']) (or 'LINES') and if that fails, calls
os.get_terminal_size(sys.__stdout__.fileno()).  In both cases, the 
latter refer to the parent console.


The 'obvious' possible solution is for Shell to set the environment 
variables when created or modified.  On creation would be easy. 
Unfortunately, there is not, AFAIK, an application-accessible Resize 
event.  A feature of tk is that geometry managers handle resizing 
automatically once the user sets the parameters of which widgets can be 
resized, and if so, min sizes and relative weights.  But maybe there is 
a workaround.


I am also not sure if modified environ is propagated to subprocesses. 
On Posix, subprocess.Popen uses os.execvp, so the parent environment is 
inherited by the new process.  I don't know if this means that 
subsequent changes are inherited.  On Windows, Popen used the system 
CreateProcess and I don't know what this does.


A workaround would be to monkeypatch the function.  In the rarely used 
-n mode, this would be easy.  In normal mode, this would require the 
execution server to send an rpc message to the Shell client asking for 
its dimensions so the server can format the string to send to the 
client.  I do not know if the current rpc mechanism is set up for 
requests from server to client.


If not fixed soon, I should add the limitation to the IDLE doc.

--
Terry Jan Reedy

___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-20 Thread Steven D'Aprano
On Mon, Sep 19, 2016 at 06:18:38AM -0400, Terry Reedy wrote:
> On 9/19/2016 4:31 AM, Paul Moore wrote:
> 
> >shutil.get_terminal_size() is available on all platforms.
> 
> On windows, it works with Command Prompt and PowerShell but fails in 
> IDLE, as it must.

Why "must" it fail?

What is it about the IDLE terminal that prevents it from emulating an 
actual terminal, like all(?) the other terminal emulation programs 
people use?

I just opened my Konqueror file & web browser, gave the command "Show 
Terminal Emulator", and ran Python:

py> import shutil
py> shutil.get_terminal_size()
os.terminal_size(columns=85, lines=10)

That's spot on perfectly correct. I then resized the window and tried it 
again:

py> shutil.get_terminal_size()
os.terminal_size(columns=108, lines=13)


I then quit Python, and ran idle3.3 from the Konqueror terminal. 
Unfortunately, get_terminal_size() returned the size of the *Konqueror* 
terminal, instead of the IDLE terminal. I think that's a bug. I don't 
know if its a bug in IDLE or get_terminal_size() or both.


-- 
Steve
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-20 Thread Michel Desmoulin
+1 for this. I regularly miss this feature.

Le 17/09/2016 à 13:12, João Matos a écrit :
> Hello,
> 
> In other interpreted programming languages the clear screen command
> (whatever it is) also does not clear the session.
> It just clears the screen clutter.
> 
> As I said, this would be very useful for newbies, which don't know
> anything about usercustomize or sitecustomize.
> 
> 
> Best regards,
> 
> JM
> 
> 
> On 17-09-2016 12:07, Chris Angelico wrote:
>> On Sat, Sep 17, 2016 at 8:51 PM, João Matos  wrote:
>>> I would like to suggest adding a clear command (not function) to Python.
>>> It's simple purpose would be to clear the REPL screen, leaving the >>>
>>> prompt at the top left of the screen.
>>>
>>> This is something very basic but also very useful for newbies learning
>>> Python from the REPL.
>>> After some trial and errors it is best to start with a clean screen.
>>> Clearing the screen helps clear your mind.
>>>
>> I'm not sure that it _is_ helpful, given that you're starting with a
>> clean screen but not restarting the session (so you'll still have all
>> the state from your previous work). If you want a completely fresh
>> start, just exit Python, clear the screen with a shell command, and
>> re-enter.
>>
>> The easiest way to play around with this would be to create a pure
>> Python clear() function in your usercustomize or sitecustomize, and
>> then try it in your own workflow - see whether it annoys you that it
>> doesn't change the interpreter state. Maybe it will, maybe it won't.
>>
>> ChrisA
>> ___
>> Python-ideas mailing list
>> Python-ideas@python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
> 
> ___
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-20 Thread Chris Angelico
On Tue, Sep 20, 2016 at 6:20 PM, Paul Moore  wrote:
> There
> have been occasional deviations from this (for example, the "as" in
> "import foo as bar" was, for a time, only a keyword in that specific
> context) but I don't believe any of them survived long-term.

async and await are similarly context-sensitive for a couple of
versions, to make the breakage simpler. But again, it's not intended
to be long-term.

ChrisA
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-20 Thread אלעזר
On Tue, Sep 20, 2016 at 4:56 AM Steven D'Aprano  wrote:

> On Mon, Sep 19, 2016 at 01:35:53PM -0700, João Matos wrote:
> > Hello,
> >
> > I don't see why creating a clear command would interfere with
> dict.clear()
> > which is a function/method.
>
> For the same reason that you can't have a method called foo.while or
> foo.if or foo.raise. If clear is a "command" (a statement) it would need
> to be a keyword, like while, if and raise.
>
>
> This is since in Python there are no contextual keywords (like "override"
and "final" in C++). I remember encountering error in a Django project
where accessing u.pass was a syntax error, but there *was* a field "pass"
in u and they had to resort to getattr(u, "pass").
What is the reasoning behind that decision?

Elazar
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-20 Thread João Matos
Hello,

You are correct.
Thanks for the explanation.

Best regards,

JM


terça-feira, 20 de Setembro de 2016 às 02:56:57 UTC+1, Steven D'Aprano 
escreveu:

> On Mon, Sep 19, 2016 at 01:35:53PM -0700, João Matos wrote: 
> > Hello, 
> > 
> > I don't see why creating a clear command would interfere with 
> dict.clear() 
> > which is a function/method. 
>
> For the same reason that you can't have a method called foo.while or 
> foo.if or foo.raise. If clear is a "command" (a statement) it would need 
> to be a keyword, like while, if and raise. 
>
>
> -- 
> Steve 
> ___ 
> Python-ideas mailing list 
> python...@python.org  
> https://mail.python.org/mailman/listinfo/python-ideas 
> Code of Conduct: http://python.org/psf/codeofconduct/ 
>
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-19 Thread Steven D'Aprano
On Mon, Sep 19, 2016 at 01:35:53PM -0700, João Matos wrote:
> Hello,
> 
> I don't see why creating a clear command would interfere with dict.clear() 
> which is a function/method.

For the same reason that you can't have a method called foo.while or 
foo.if or foo.raise. If clear is a "command" (a statement) it would need 
to be a keyword, like while, if and raise.


-- 
Steve
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-19 Thread Paul Moore
On 19 September 2016 at 21:35, João Matos  wrote:
> Hello,
>
> I don't see why creating a clear command would interfere with dict.clear()
> which is a function/method.
>
> Although my first idea was a clear command, I have no problem if it is a
> clear() function from site.py.
>
> I didn't suggest cls because it is normally used to mean class.
>
> I use Windows and tested a simple (possibly not the best of course) solution
> that seems to work in REPL (but not in IDLE).
>
> import os
> import sys
>
> def clear():
> if sys.platform == 'win32':
> os.system('cls')
> else:
> os.system('clear')

Ah, I think people had misunderstood your proposal of a "command" as
something where you didn't have to type the parentheses. I know I did.
If you're OK with what you type at the REPL being

>>> clear()

then that's much easier to achieve. For example, if you install click
(https://pypi.python.org/pypi/click) you can just add "from click
import clear" at the start of your REPL session (or add it to your
PYTHONSTARTUP if you want it always available) and you're done.

Adding similar functionality to the stdlib somewhere (for example os
or shutil - I doubt it's going to get accepted as a builtin) isn't
inconceivable. I'm still not sure the benefit is sufficient to be
worthwhile (it might be more useful to just bite the bullet and bundle
one of the many curses variants for Windows into the distribution, and
make the curses module cross-platform once and for all, rather than
implementing individual bits of functionality) but if someone wanted
to contribute a patch on the tracker, it might be accepted.

Paul
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-19 Thread João Matos
Hello,

I don't see why creating a clear command would interfere with dict.clear() 
which is a function/method.

Although my first idea was a clear command, I have no problem if it is a 
clear() function from site.py.

I didn't suggest cls because it is normally used to mean class.

I use Windows and tested a simple (possibly not the best of course) 
solution that seems to work in REPL (but not in IDLE).

import os
import sys

def clear():
if sys.platform == 'win32':
os.system('cls')
else:
os.system('clear')


Best regards,

JM


segunda-feira, 19 de Setembro de 2016 às 03:33:45 UTC+1, Steven D'Aprano 
escreveu:

> On Sat, Sep 17, 2016 at 11:51:16AM +0100, João Matos wrote: 
> > Hello, 
> > 
> > I would like to suggest adding a clear command (not function) to Python. 
>
> While technically "clear" could be a command, I think it should not be. 
>
> First off, making clear a reserved keyword, and a statement, like print 
> in Python 2, raise or import, would be a major backwards compatibility 
> break. It would mean dict.clear() has to be renamed, and it would break 
> lots of existing code. 
>
> So making clear a keyword is not going to happen. 
>
> If could be a pseudo-built-in, like help(), quit() and exit(), added to 
> built-ins by the site module. In that case, it is *technically* possible 
> to have it operate without the parentheses: 
>
> class ClearType: 
> def __repr__(self): 
> # code to clear the screen here 
> ... 
>
> clear = ClearType() 
>
> so that when you enter clear at the interactive interpreter, __repr__ is 
> called and it clears the screen. But I would argue against it. Instead, 
> it is better to use the same convention that executable code that has 
> side-effects should be implemented as a function call. 
>
> So I suggest following the design of exit() and quit(): 
>
> py> exit 
> Use exit() or Ctrl-D (i.e. EOF) to exit 
>
>
> class ClearType: 
> def __repr__(self): 
> return "Use clear() or Ctrl-L (i.e. FF) to clear the screen" 
> def __call__(self): 
> # clear screen code goes here 
>
> clear = ClearType()  # or possibly cls ? 
>
>
>
> That is, *if* we add this function at all. 
>
> Personally, I agree with you. There are many different ways of clearing 
> the screen, but they depend on the specific terminal used, whether 
> readline is active or not, the operating system, etc. I think that 
> interactive use is important enough that we should have a standard way 
> of clearing the screen. I personally often find myself just holding down 
> the Enter key until I have a blank screen. 
>
> In this ticket: 
>
> http://bugs.python.org/issue27771 
>
> Raymond Hettinger mentions that it is an often-requested feature by 
> learners, and I believe that IDLE has an active task for this feature: 
>
> http://bugs.python.org/issue6143 
>
> but I don't see any tasks for a clear screen command for the default 
> REPL. 
>
> I'm in favour of adding a clear() *function* to the site.py module, 
> similar to exit/quit/help, but not making it "magical" or a keyword that 
> doesn't require brackets. But I don't know how to implement it for the 
> large variety of terminals and operating systems supported by Python. 
>
> (The fallback if all else fails is easy: get the height of the terminal, 
> in lines, and print that many blank lines.) 
>
>
> -- 
> Steve 
> ___ 
> Python-ideas mailing list 
> python...@python.org  
> https://mail.python.org/mailman/listinfo/python-ideas 
> Code of Conduct: http://python.org/psf/codeofconduct/ 
>
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-19 Thread eryk sun
On Mon, Sep 19, 2016 at 1:12 PM, Paul Moore  wrote:
> By the way - if you're on a system with readline support included with
> Python, GNU readline apparently has a binding for clear-screen
> (CTRL-L) so you may well have this functionality already (I don;'t use
> Unix or readline, so I can't comment for sure).

Hooking Ctrl+L to clear the screen can be implemented for Windows
Vista and later via the ReadConsole pInputControl parameter, as called
by PyOS_StdioReadline. It should be possible to match how GNU readline
works -- i.e. clear the screen, reprint the prompt, flush the input
buffer, and write the current line's input back to the input buffer.

The pInputControl parameter can also be used to implement Unix-style
Ctrl+D to end a read anywhere on a line, whereas the classic
[Ctrl+Z][Enter] has to be entered at the start of a line.
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-19 Thread Paul Moore
On 19 September 2016 at 13:10, Paul Moore  wrote:
>
> For this particular suggestion, though, I don't think that's the case.
> I think it's going to either be something that's accepted into the
> stdlib, or something that's rejected as too platform-specific or messy
> to standardise, and people should roll their own implementation.

Note, by the way, that in the form of a Python function, this
capability is already available in lots of places - colorama and click
include it, for a start. And even the stdlib has a version in the
curses module (albeit not available on Windows).

In the light of this, there seems little reason to water down this
proposal to "provide a clear screen function in the stdlib".

Taken in its original form of "add a command to the REPL to clear the
screen", the main issue is that the standard Python REPL simply
doesn't have a concept of "REPL commands", so there's nowhere really
to add that functionality without a relatively major overhaul. People
who want a richer REPL can look at other options like IDLE, or
IPython/Jupyter.

By the way - if you're on a system with readline support included with
Python, GNU readline apparently has a binding for clear-screen
(CTRL-L) so you may well have this functionality already (I don;'t use
Unix or readline, so I can't comment for sure).

So the proposal becomes limited to:

"""
Add a capability to the standard REPL to recognise and support
"commands" (as distinct from executable Python code) and provide a
"clear screen" command.
"""

This feature is only needed for users of the standard Python REPL, on
systems without readline support (i.e. Windows). I don't think that's
a significant enough benefit to warrant the change (even if we take
into account the potential opportunity for additional commands that
we'd get from adding command support to the REPL).

Paul
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-19 Thread Chris Angelico
On Mon, Sep 19, 2016 at 9:56 PM, Steven D'Aprano  wrote:
> On Mon, Sep 19, 2016 at 06:38:00PM +1000, Chris Angelico wrote:
>
>> Sounds good to me. This is definitely sounding complicated and messy
>> enough to justify (a) writing a function to clear the screen, and (b)
>> testing that function thoroughly as a PyPI module before pushing
>> anything into the stdlib.
>
> This isn't Node.js where standard operating procedure
> is to rely on the third-party npm ecosystem even for tiny, ten line
> functions. And we should be *glad* that's not the case:

I agree; however, the bar for getting something onto PyPI is far lower
than the stdlib, and it makes it far easier to get a bit of testing
(hey, folks, here's what I'm playing with, can you try pip-installing
it and see if it works on your platform please). So the "first on
PyPI, then in the stdlib" route does have a lot of merit. But you're
quite right that we don't want to depend on a ton of packages

ChrisA
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-19 Thread Paul Moore
On 19 September 2016 at 12:56, Steven D'Aprano  wrote:
>
> For relatively small pieces of functionality, if it is useful enough, we
> should just add it to the std lib, and if it isn't, we should just say
> it isn't useful enough. We shouldn't condemn supporters of the idea to
> this false hope that if they can convince a thousand, or a million,
> people to download their package, it will be added to PyPI

"... to the stdlib".

Agreed. However, there are libraries already on PyPI that try to act
in the role of "useful bits that aren't in the stdlib". The boltons
project is one that I know of. Maybe a middle ground between stdlib
inclusion and outright rejection would be the suggestion to submit the
code to one of those 3rd party projects? There's no implication that
doing so means that there's any better chance of getting accepted for
the stdlib, but it does offer an option for people who want to publish
their idea for wider use.

For this particular suggestion, though, I don't think that's the case.
I think it's going to either be something that's accepted into the
stdlib, or something that's rejected as too platform-specific or messy
to standardise, and people should roll their own implementation.

I'm inclined to think there may be some scope for a blog entry or
HOWTO on customising your personal Python environment - what
facilities there are to do so, what other options (such as different
REPLs) are available, and how to judge whether it's worth doing or
not. Lots of people (myself included at times!) seem to be unaware of
the options available. It's not something I'm likely to ever write,
but if anyone is interested in doing so, it might be useful background
for this type of discussion.

Paul
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-19 Thread Steven D'Aprano
On Mon, Sep 19, 2016 at 06:38:00PM +1000, Chris Angelico wrote:

> Sounds good to me. This is definitely sounding complicated and messy
> enough to justify (a) writing a function to clear the screen, and (b)
> testing that function thoroughly as a PyPI module before pushing
> anything into the stdlib.

This isn't Node.js where standard operating procedure 
is to rely on the third-party npm ecosystem even for tiny, ten line 
functions. And we should be *glad* that's not the case:

http://www.theregister.co.uk/2016/03/23/npm_left_pad_chaos/

People don't often install minor or small packages off PyPI as a matter 
of course: they either roll their own, or do without, depending on which 
is more painful, or whether they are even allowed to download 
third-party software (not everybody is), or whether managing the 
dependency is more or less work than writing their own.

I get it that not everything belongs in the std lib, and I get it that 
for *some* people (but not all) its easy to rely on packages in PyPI. 
But there's quite considerable friction in getting *small* things off 
PyPI. For starters, you have to know it exists, you have to trust that 
its functional and well-written. There is a huge amount of junk, 
abandoned and "version 0.1" packages on PyPI that nobody in their right 
mind should use. Dependencies don't come for free, they add complexity 
and cost to a project. Not everything belongs as a package on PyPI either.

If the cost of dealing with the added dependency is greater than 
the benefit gained, people won't use third-party packages on PyPI. For 
small but useful functionality, saying "Put it on PyPI" is effectively 
just a dismissal.

For relatively small pieces of functionality, if it is useful enough, we 
should just add it to the std lib, and if it isn't, we should just say 
it isn't useful enough. We shouldn't condemn supporters of the idea to 
this false hope that if they can convince a thousand, or a million, 
people to download their package, it will be added to PyPI.


-- 
Steve
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-19 Thread Terry Reedy

On 9/19/2016 4:31 AM, Paul Moore wrote:


shutil.get_terminal_size() is available on all platforms.


On windows, it works with Command Prompt and PowerShell but fails in 
IDLE, as it must.  In the absence of knowledge, it guesses the default 
of 80 x 24 (as documented).  AFAIK, there is no way whether an answer of 
80 x 24 is correct or arbitrary nonsense.  Returning 0 x 0 instead would 
be more informative.




Terry Jan Reedy

___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-19 Thread Chris Angelico
On Mon, Sep 19, 2016 at 6:31 PM, Paul Moore  wrote:
> On 19 September 2016 at 03:40, Chris Angelico  wrote:
>> On Mon, Sep 19, 2016 at 12:32 PM, Steven D'Aprano  
>> wrote:
>>> (The fallback if all else fails is easy: get the height of the terminal,
>>> in lines, and print that many blank lines.)
>>
>> Assuming you can get the height in lines. Have you tried that in the
>> default Windows shell? I don't think tcgetattr works on Windows.
>
> shutil.get_terminal_size() is available on all platforms.
>
> I don't think it would be unreasonable to add shutil.clear_terminal()
> (at the moment, get_terminal_size is the only "terminal handling"
> function in shutil, but it would be the obvious place to add others if
> we chose to do so).

Sounds good to me. This is definitely sounding complicated and messy
enough to justify (a) writing a function to clear the screen, and (b)
testing that function thoroughly as a PyPI module before pushing
anything into the stdlib.

ChrisA
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-19 Thread Paul Moore
On 19 September 2016 at 03:40, Chris Angelico  wrote:
> On Mon, Sep 19, 2016 at 12:32 PM, Steven D'Aprano  wrote:
>> (The fallback if all else fails is easy: get the height of the terminal,
>> in lines, and print that many blank lines.)
>
> Assuming you can get the height in lines. Have you tried that in the
> default Windows shell? I don't think tcgetattr works on Windows.

shutil.get_terminal_size() is available on all platforms.

I don't think it would be unreasonable to add shutil.clear_terminal()
(at the moment, get_terminal_size is the only "terminal handling"
function in shutil, but it would be the obvious place to add others if
we chose to do so).

Paul
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-18 Thread Stephen J. Turnbull
Chris Angelico writes:
 > On Mon, Sep 19, 2016 at 12:32 PM, Steven D'Aprano  
 > wrote:
 > > (The fallback if all else fails is easy: get the height of the terminal,
 > > in lines, and print that many blank lines.)
 > 
 > Assuming you can get the height in lines. Have you tried that in the
 > default Windows shell? I don't think tcgetattr works on Windows.

Since a command is out of the question (look what happened to print!),
it's a matter of defining a callable with a repr that explains how to
call it (like help and friends).  So if someone really wants this to
happen, I would say the thing to do is to define that callable, put it
on PyPI, add support for all the platforms, fix all the bugs, and when
there are a couple million downloads, suggest preloading it in
interpreter then.  Don't forget how to document how to add it to
site.py.

But I would think that nowadays we'd push in the opposite direction
(as with print).  That is, with the great improvements in IDLE
(batteries included!), IPython, and now we have Jupyter, you could now
argue that the built-in REPL should lose at least one of the two exit
functions (since EOF is a sufficient reason to exit).  (help() still
makes sense as the public interface to __doc__.)

In other words, the built-in REPL should just provide some
line-editing features and otherwise simply read lines, incrementally
compile and execute them, and print results.  Leave UI conveniences to
other applications that (gasp!) specialize in providing consistent,
powerful UI that isn't going to feel like bat guano on Tim's keyboard.
IMHO YMMV, of course.

___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-18 Thread Chris Angelico
On Mon, Sep 19, 2016 at 12:32 PM, Steven D'Aprano  wrote:
> (The fallback if all else fails is easy: get the height of the terminal,
> in lines, and print that many blank lines.)

Assuming you can get the height in lines. Have you tried that in the
default Windows shell? I don't think tcgetattr works on Windows.

ChrisA
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-18 Thread Steven D'Aprano
On Sat, Sep 17, 2016 at 11:51:16AM +0100, João Matos wrote:
> Hello,
> 
> I would like to suggest adding a clear command (not function) to Python.

While technically "clear" could be a command, I think it should not be.

First off, making clear a reserved keyword, and a statement, like print 
in Python 2, raise or import, would be a major backwards compatibility 
break. It would mean dict.clear() has to be renamed, and it would break 
lots of existing code.

So making clear a keyword is not going to happen.

If could be a pseudo-built-in, like help(), quit() and exit(), added to 
built-ins by the site module. In that case, it is *technically* possible 
to have it operate without the parentheses:

class ClearType:
def __repr__(self):
# code to clear the screen here
...

clear = ClearType()

so that when you enter clear at the interactive interpreter, __repr__ is 
called and it clears the screen. But I would argue against it. Instead, 
it is better to use the same convention that executable code that has 
side-effects should be implemented as a function call.

So I suggest following the design of exit() and quit():

py> exit
Use exit() or Ctrl-D (i.e. EOF) to exit


class ClearType:
def __repr__(self):
return "Use clear() or Ctrl-L (i.e. FF) to clear the screen"
def __call__(self):
# clear screen code goes here

clear = ClearType()  # or possibly cls ?



That is, *if* we add this function at all.

Personally, I agree with you. There are many different ways of clearing 
the screen, but they depend on the specific terminal used, whether 
readline is active or not, the operating system, etc. I think that 
interactive use is important enough that we should have a standard way 
of clearing the screen. I personally often find myself just holding down 
the Enter key until I have a blank screen.

In this ticket:

http://bugs.python.org/issue27771

Raymond Hettinger mentions that it is an often-requested feature by 
learners, and I believe that IDLE has an active task for this feature:

http://bugs.python.org/issue6143

but I don't see any tasks for a clear screen command for the default 
REPL.

I'm in favour of adding a clear() *function* to the site.py module, 
similar to exit/quit/help, but not making it "magical" or a keyword that 
doesn't require brackets. But I don't know how to implement it for the 
large variety of terminals and operating systems supported by Python.

(The fallback if all else fails is easy: get the height of the terminal, 
in lines, and print that many blank lines.)


-- 
Steve
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-17 Thread Terry Reedy

On 9/17/2016 6:51 AM, João Matos wrote:

Hello,

I would like to suggest adding a clear command (not function) to Python.
It's simple purpose would be to clear the REPL screen, leaving the >>>
prompt at the top left of the screen.

This is something very basic but also very useful for newbies learning
Python from the REPL.
After some trial and errors it is best to start with a clean screen.
Clearing the screen helps clear your mind.

Historically it is a common command in interpreted languages.


Python is not an 'interpreted' language in the sense that you are using 
the word.  It is a compiled language that includes its compile function 
as part of the runtime.  This enables an interactive mode.


Python does not have commands, only statements, expressions, and 
functions, etcetera.  This is why the 'exit()' and 'quit()' 'commands' 
are functions and require the parentheses.  In interactive mode, a 
statement consisting of a single indentifier (or expression) causes 
display of a representation of the resulting object.


You could request that the site module also add a clear() function. But 
the next problem is that clearing the screen is not a Python function 
and the Python runtime does not normally know how to do so.  The 
implementation of 'clear screen' is specific to the particular console 
or window or terminal emulation.  The control sequence for a window that 
emulates VT100 does not work on Windows Command Prompt (excepting 
possibly a non-default CP on Win 10) or necessarily IDLE or IPython or 
PyCharm or ... .  The only assumption that Python makes about the screen 
it is running on is that '\n' is interpreted as a newline.



IDLE has a 'Restart Shell' menu command with key binding.  It does not 
physically clear the screen but it draws a double line, which serves 
most of the 'clear the mind' purpose.  If there is an editor window 
open, one can close and reopen the Shell window to start really fresh.


At least on Windows, Select All (^A) + Del (or Backspace) is the common 
idiom for clearing an editor box in a GUI.  Is this used on other OSes? 
This works in an IDLE editor also.  It is disabled in the Shell because 
IDLE's original designers thought to protect beginners from doing such. 
A clear screen menu option has been requested, but it has never become a 
top priority.  In any case, this, like other current clear screen 
commands, would work because the command would go to the window manager 
and not to python.


--
Terry Jan Reedy


___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-17 Thread eryk sun
On Sat, Sep 17, 2016 at 1:15 PM, Wes Turner  wrote:
>   !cls #windows

cmd's built-in cls command doesn't clear just the screen, like a VT100
\x1b[1J. It clears the console's entire scrollback buffer. Unix
`clear` may also work like that. With GNOME Terminal in Linux, `clear`
leaves a single screen in the scrollback buffer.
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-17 Thread João Matos

Hello,

I searched and found several possible solutions to clear the screen in 
the CPython REPL, but all are, in my opinion, complex for a newbie.
The existence of a clear command would be a simple and obvious, 
therefore accessible to newbies.


Best regards,

JM



On 17-09-2016 14:34, eryk sun wrote:

On Sat, Sep 17, 2016 at 11:11 AM, João Matos  wrote:

On 17-09-2016 12:07, Oleg Broytman wrote:

 Pressing [Ctrl]+[L] works for me.

Doesn't work on Windows.

Windows 10 added VT100 support to the console, so you can create a
little cls() function to clear the screen:

 cls = lambda: print('\x1b[1J', end='', flush=True)

VT100 support isn't enabled by default. However, cmd.exe always
enables this mode. So run doskey.exe via os.system to enable VT100
mode while setting a convenient "cls" alias:

 import os
 os.system(r'doskey /exename=python.exe cls=cls()')

This alias substitutes "cls()" for "cls" at the beginning of a line.
This saves you from having to type "()" all the time. The alias isn't
active for other programs; e.g. if you execute
subprocess.call('powershell'), then "cls" will be the PowerShell alias
for Clear-Host.

You can also use ctypes instead of os.system and doskey.exe:

 import ctypes
 kernel32 = ctypes.WinDLL('kernel32')
 hStdOut = kernel32.GetStdHandle(-11)

 # enable VT100 mode
 mode = (ctypes.c_uint * 1)()
 kernel32.GetConsoleMode(hStdOut, mode)
 kernel32.SetConsoleMode(hStdOut, mode[0] | 4)

 # define a cls() function and set a console alias
 cls = lambda: print('\x1b[1J', end='', flush=True)
 kernel32.AddConsoleAliasW('cls', 'cls()', 'python.exe')

For older versions of Windows you can use ANSICON or ConEmu, which use
DLL injection to extend the console API. Python's colorama and
pyreadline modules should also work for this.
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Re: [Python-ideas] Suggestion: Clear screen command for the REPL

2016-09-17 Thread João Matos

Hello,

I know about those IPython commands and I searched and found several 
possible solutions to clear the screen in the CPython REPL, but all are, 
in my opinion, complex for a newbie.
The existence of a clear command would be simple and obvious, therefore 
accessible to newbies.



Best regards,

JM



On 17-09-2016 14:15, Wes Turner wrote:

With IPython, there are a number of ways to reset the terminal display:

  clear  # %clear
  !cls #windows
  !reset

- 
http://stackoverflow.com/questions/6892191/clearing-the-screen-in-ipython

  - Ctrl-L is a readline binding
  - 
http://pythonhosted.org/pyreadline/usage.html#pyreadline-with-python-interpreter

  - https://anaconda.org/anaconda/pyreadline
  - https://pypi.python.org/pypi/pyreadline
  - IPython >= 5 no longer uses pyreadline (instead, python prompt 
toolkit)
- 
https://github.com/jonathanslenders/python-prompt-toolkit/blob/master/prompt_toolkit/shortcuts.py 
#def clear



 - 
http://unix.stackexchange.com/questions/124762/how-does-clear-command-work
- 
http://urwid.org/reference/display_modules.html#urwid.raw_display.Screen.clear

- https://rosettacode.org/wiki/Terminal_control/Clear_the_screen#Python

On Saturday, September 17, 2016, João Matos > wrote:


Hello,

In other interpreted programming languages the clear screen
command (whatever it is) also does not clear the session.
It just clears the screen clutter.

As I said, this would be very useful for newbies, which don't know
anything about usercustomize or sitecustomize.


Best regards,

JM


On 17-09-2016 12:07, Chris Angelico wrote:

On Sat, Sep 17, 2016 at 8:51 PM, João Matos
 wrote:

I would like to suggest adding a clear command (not
function) to Python.
It's simple purpose would be to clear the REPL screen,
leaving the >>>
prompt at the top left of the screen.

This is something very basic but also very useful for
newbies learning
Python from the REPL.
After some trial and errors it is best to start with a
clean screen.
Clearing the screen helps clear your mind.

I'm not sure that it _is_ helpful, given that you're starting
with a
clean screen but not restarting the session (so you'll still
have all
the state from your previous work). If you want a completely fresh
start, just exit Python, clear the screen with a shell
command, and
re-enter.

The easiest way to play around with this would be to create a pure
Python clear() function in your usercustomize or
sitecustomize, and
then try it in your own workflow - see whether it annoys you
that it
doesn't change the interpreter state. Maybe it will, maybe it
won't.

ChrisA
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas

Code of Conduct: http://python.org/psf/codeofconduct/



___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas

Code of Conduct: http://python.org/psf/codeofconduct/




___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/