Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #16663 > unrolled thread

Backspace does not erase in stdout

Started bySteven D'Aprano <steve+comp.lang.python@pearwood.info>
First post2011-12-05 16:09 +0000
Last post2011-12-06 15:24 +0000
Articles 12 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  Backspace does not erase in stdout Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-05 16:09 +0000
    Re: Backspace does not erase in stdout Chris Angelico <rosuav@gmail.com> - 2011-12-06 03:13 +1100
      Re: Backspace does not erase in stdout Grant Edwards <invalid@invalid.invalid> - 2011-12-05 16:23 +0000
        Re: Backspace does not erase in stdout Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-05 17:24 +0000
          Re: Backspace does not erase in stdout Grant Edwards <invalid@invalid.invalid> - 2011-12-05 17:40 +0000
            Re: Backspace does not erase in stdout Chris Angelico <rosuav@gmail.com> - 2011-12-06 09:00 +1100
        Re: Backspace does not erase in stdout Nobody <nobody@nowhere.com> - 2011-12-06 09:27 +0000
          Re: Backspace does not erase in stdout Rick Johnson <rantingrickjohnson@gmail.com> - 2011-12-06 05:21 -0800
            Re: Backspace does not erase in stdout Chris Angelico <rosuav@gmail.com> - 2011-12-07 01:03 +1100
            Re: Backspace does not erase in stdout Roy Smith <roy@panix.com> - 2011-12-06 09:49 -0500
          Re: Backspace does not erase in stdout Roy Smith <roy@panix.com> - 2011-12-06 09:45 -0500
          Re: Backspace does not erase in stdout Grant Edwards <invalid@invalid.invalid> - 2011-12-06 15:24 +0000

#16663 — Backspace does not erase in stdout

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-12-05 16:09 +0000
SubjectBackspace does not erase in stdout
Message-ID<4edcecc6$0$29988$c3e8da3$5496439d@news.astraweb.com>
I have a function which reads characters from stdin and writes stars to 
stdout, but backspacing does not erase the stars as I expected.

Tested in Python 2.6 on Linux. This will almost certainly not work on 
Windows.

import sys, tty, termios
def getpass():
    fd = sys.stdin.fileno()
    old_settings = termios.tcgetattr(fd)
    chars = []
    try:
        tty.setraw(sys.stdin.fileno())
        while 1:
            c = sys.stdin.read(1)
            if c in '\n\r':  # Enter or Return key.
                break
            elif c == '\x7f':  # Backspace key.
                if chars:
                    # Rubout previous character.
                    sys.stdout.write('\b')
                    sys.stdout.flush()
                    del chars[-1]
            else:
                # Obfuscate the letter typed.
                sys.stdout.write('*')
                chars.append(c)
    finally:
        termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
        sys.stdout.write('\n')
    return ''.join(chars)


When I call this function and then type, I get a serious of asterisks as 
expected. Each time I press the backspace key, the cursor moves one 
character to the left, but the asterisks remain visible.

Is there a way to erase the character other than backspacing, writing a 
space, then backspacing again? That feels inelegant.



-- 
Steven

[toc] | [next] | [standalone]


#16664

FromChris Angelico <rosuav@gmail.com>
Date2011-12-06 03:13 +1100
Message-ID<mailman.3305.1323101623.27778.python-list@python.org>
In reply to#16663
On Tue, Dec 6, 2011 at 3:09 AM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
>
> Is there a way to erase the character other than backspacing, writing a
> space, then backspacing again? That feels inelegant.

Emitting "\b \b" is one very common way to do a destructive backspace.
Inelegant? Perhaps, but a common inelegance.

ChrisA

[toc] | [prev] | [next] | [standalone]


#16666

FromGrant Edwards <invalid@invalid.invalid>
Date2011-12-05 16:23 +0000
Message-ID<jbir6r$c3$1@reader1.panix.com>
In reply to#16664
On 2011-12-05, Chris Angelico <rosuav@gmail.com> wrote:
> On Tue, Dec 6, 2011 at 3:09 AM, Steven D'Aprano
><steve+comp.lang.python@pearwood.info> wrote:
>>
>> Is there a way to erase the character other than backspacing, writing a
>> space, then backspacing again? That feels inelegant.
>
> Emitting "\b \b" is one very common way to do a destructive backspace.
> Inelegant? Perhaps, but a common inelegance.

That's pretty much the only way I've seen it done for the past 25
years.

What would be more elegant?

-- 
Grant Edwards               grant.b.edwards        Yow! This PIZZA symbolizes
                                  at               my COMPLETE EMOTIONAL
                              gmail.com            RECOVERY!!

[toc] | [prev] | [next] | [standalone]


#16670

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-12-05 17:24 +0000
Message-ID<4edcfe6b$0$29988$c3e8da3$5496439d@news.astraweb.com>
In reply to#16666
On Mon, 05 Dec 2011 16:23:55 +0000, Grant Edwards wrote:

> On 2011-12-05, Chris Angelico <rosuav@gmail.com> wrote:
>> On Tue, Dec 6, 2011 at 3:09 AM, Steven D'Aprano
>><steve+comp.lang.python@pearwood.info> wrote:
>>>
>>> Is there a way to erase the character other than backspacing, writing
>>> a space, then backspacing again? That feels inelegant.
>>
>> Emitting "\b \b" is one very common way to do a destructive backspace.
>> Inelegant? Perhaps, but a common inelegance.
> 
> That's pretty much the only way I've seen it done for the past 25 years.
> 
> What would be more elegant?

For backspace to actually backspace, and not just move the cursor.

Thanks for those who answered, I guess I'll just do the backspace, space, 
backspace dance.


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#16671

FromGrant Edwards <invalid@invalid.invalid>
Date2011-12-05 17:40 +0000
Message-ID<jbivmm$2jl$1@reader1.panix.com>
In reply to#16670
On 2011-12-05, Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote:
> On Mon, 05 Dec 2011 16:23:55 +0000, Grant Edwards wrote:
>
>> On 2011-12-05, Chris Angelico <rosuav@gmail.com> wrote:
>>> On Tue, Dec 6, 2011 at 3:09 AM, Steven D'Aprano
>>><steve+comp.lang.python@pearwood.info> wrote:
>>>>
>>>> Is there a way to erase the character other than backspacing, writing
>>>> a space, then backspacing again? That feels inelegant.
>>>
>>> Emitting "\b \b" is one very common way to do a destructive backspace.
>>> Inelegant? Perhaps, but a common inelegance.
>> 
>> That's pretty much the only way I've seen it done for the past 25 years.
>> 
>> What would be more elegant?
>
> For backspace to actually backspace, and not just move the cursor.

Ah, but "just move the cursor" is what backspace has always meant. 
It's been that way for 100 years -- since the days of typewriters and
teletypes.  ;)

> Thanks for those who answered, I guess I'll just do the backspace,
> space, backspace dance.

After thinking a while, I do remember one program I ran across
recently that when you hit backspace would erase the entire line, then
rewrite the entire line stopping one character short of where it was
before.  Even at network speeds it was noticable when the link was
encrypted.

-- 
Grant Edwards               grant.b.edwards        Yow! Inside, I'm already
                                  at               SOBBING!
                              gmail.com            

[toc] | [prev] | [next] | [standalone]


#16689

FromChris Angelico <rosuav@gmail.com>
Date2011-12-06 09:00 +1100
Message-ID<mailman.3323.1323122450.27778.python-list@python.org>
In reply to#16671
On Tue, Dec 6, 2011 at 4:40 AM, Grant Edwards <invalid@invalid.invalid> wrote:
> After thinking a while, I do remember one program I ran across
> recently that when you hit backspace would erase the entire line, then
> rewrite the entire line stopping one character short of where it was
> before.  Even at network speeds it was noticable when the link was
> encrypted.

I've seen a few programs that do that. Benefit is that it works even
when (a) you're doing more than just "backspace one letter" (eg
deleting an entire word), and (b) when your logical line wraps over
multiple physical lines. It gets ugly fast, though.

ChrisA

[toc] | [prev] | [next] | [standalone]


#16712

FromNobody <nobody@nowhere.com>
Date2011-12-06 09:27 +0000
Message-ID<pan.2011.12.06.09.27.42.225000@nowhere.com>
In reply to#16666
On Mon, 05 Dec 2011 16:23:55 +0000, Grant Edwards wrote:

>> Emitting "\b \b" is one very common way to do a destructive backspace.
>> Inelegant? Perhaps, but a common inelegance.
> 
> That's pretty much the only way I've seen it done for the past 25
> years.

... before which, it was BS-DEL-BS.

DEL being 127 means that it punches through all of the holes on the paper
tape, causing whatever was there previously to be replaced with a DEL
character. BS-SPC-BS would only punch through one of the holes.

[toc] | [prev] | [next] | [standalone]


#16729

FromRick Johnson <rantingrickjohnson@gmail.com>
Date2011-12-06 05:21 -0800
Message-ID<efd4d62a-10ba-427b-a814-ff5ccc07e4bf@h3g2000yqa.googlegroups.com>
In reply to#16712
On Dec 6, 3:27 am, Nobody <nob...@nowhere.com> wrote:
> On Mon, 05 Dec 2011 16:23:55 +0000, Grant Edwards wrote:
> >> Emitting "\b \b" is one very common way to do a destructive backspace.
> >> Inelegant? Perhaps, but a common inelegance.
>
> > That's pretty much the only way I've seen it done for the past 25
> > years.
>
> ... before which, it was BS-DEL-BS.
>
> DEL being 127 means that it punches through all of the holes on the paper
> tape, causing whatever was there previously to be replaced with a DEL
> character. BS-SPC-BS would only punch through one of the holes.

*Wise Observer Speculates:* Why on earth are we "21st century slaves"
to an archaic mid 20th century technology that punches holes in paper
tape? Anyone?

>>> isinstance(Progress, None)
True

[toc] | [prev] | [next] | [standalone]


#16732

FromChris Angelico <rosuav@gmail.com>
Date2011-12-07 01:03 +1100
Message-ID<mailman.3351.1323180238.27778.python-list@python.org>
In reply to#16729
On Wed, Dec 7, 2011 at 12:21 AM, Rick Johnson
<rantingrickjohnson@gmail.com> wrote:
> *Wise Observer Speculates:* Why on earth are we "21st century slaves"
> to an archaic mid 20th century technology that punches holes in paper
> tape? Anyone?
>
>>>> isinstance(Progress, None)
> True

I'm not sure. Let's see...

* Manned, powered, heavier-than-air flight: A hundred years.
* TCP/IP: Thirty years.
* Sliced bread: Eighty years. (What was the greatest thing BEFORE sliced bread?)
* The English language: A few hundred, depending on how you count

Why are we still eating sliced bread on board aeroplanes while posting
in English on internet forums? Clearly we've made no progress since
then. What language and communication protocol do you propose for the
new python-troll-list?

Getting back to the point though...

The use of 127 for DEL is a consequence of the exact technology of
paper tape. However, the question of whether BS is destructive or not
is separate. Should it parallel other characters (draw character at
cursor then advance cursor, vs retard cursor then erase character at
cursor), or should it parallel cursor movement (eg carriage return,
line feed, vertical tab)? If your BS is defined nondestructively, you
can implement the destructive version with three characters; if it's
defined destructively, you can't implement a nondestructive without
some fiddliness (some systems use characters 28-31 for cursor
movement).

ChrisA

[toc] | [prev] | [next] | [standalone]


#16735

FromRoy Smith <roy@panix.com>
Date2011-12-06 09:49 -0500
Message-ID<roy-D5E20D.09490106122011@news.panix.com>
In reply to#16729
In article 
<efd4d62a-10ba-427b-a814-ff5ccc07e4bf@h3g2000yqa.googlegroups.com>,
 Rick Johnson <rantingrickjohnson@gmail.com> wrote:

> *Wise Observer Speculates:* Why on earth are we "21st century slaves"
> to an archaic mid 20th century technology that punches holes in paper
> tape? Anyone?

Or to an archaic mid 20th century technology that limited lines to 80 
characters?

[toc] | [prev] | [next] | [standalone]


#16734

FromRoy Smith <roy@panix.com>
Date2011-12-06 09:45 -0500
Message-ID<roy-ED40B4.09450806122011@news.panix.com>
In reply to#16712
In article <pan.2011.12.06.09.27.42.225000@nowhere.com>,
 Nobody <nobody@nowhere.com> wrote:

> On Mon, 05 Dec 2011 16:23:55 +0000, Grant Edwards wrote:
> 
> >> Emitting "\b \b" is one very common way to do a destructive backspace.
> >> Inelegant? Perhaps, but a common inelegance.
> > 
> > That's pretty much the only way I've seen it done for the past 25
> > years.
> 
> ... before which, it was BS-DEL-BS.
> 
> DEL being 127 means that it punches through all of the holes on the paper
> tape, causing whatever was there previously to be replaced with a DEL
> character. BS-SPC-BS would only punch through one of the holes.

I don't remember having a DEL key on the ASR-33.  It did have a RUBOUT 
key (http://regmedia.co.uk/2010/06/21/teletype_hereis.jpg) which 
performed the function you describe.  I don't remember if that actually 
sent a 177 code over the wire or if it was a purely local function.

Also, the BS code only caused the printhead to back up.  If you wanted 
to back up the tape, you had to press the "B. SP." button the the punch 
(http://pichotjm.free.fr/Multi8/TTY/ASR33-3.html).

Of course, sending a BS-SPS-BS sequence on a printing terminal didn't do 
anything about erasing what had previously been printed.  To do that, 
you needed one of those fancy vee-de-oh things.  On the ASR-33, to 
prevent passwords from being readable, you either disabled echo, or sent 
something like BS-*-BS-X-BS-$-BS-%-BS-#-BS-@-BS-&-BS-H-BS-Z-BS-M in the 
hopes that nothing would be readable under all that garbage.

[toc] | [prev] | [next] | [standalone]


#16737

FromGrant Edwards <invalid@invalid.invalid>
Date2011-12-06 15:24 +0000
Message-ID<jblc3r$sl8$2@reader1.panix.com>
In reply to#16712
On 2011-12-06, Nobody <nobody@nowhere.com> wrote:
> On Mon, 05 Dec 2011 16:23:55 +0000, Grant Edwards wrote:
>
>>> Emitting "\b \b" is one very common way to do a destructive backspace.
>>> Inelegant? Perhaps, but a common inelegance.
>> 
>> That's pretty much the only way I've seen it done for the past 25
>> years.
>
> ... before which, it was BS-DEL-BS.
>
> DEL being 127 means that it punches through all of the holes on the
> paper tape, causing whatever was there previously to be replaced with
> a DEL character.


> BS-SPC-BS would only punch through one of the holes.

Ah yes.  And when you read a tape, you silently ignored DEL
characters.  I had completely fogotten about that. I only used paper
tape very briefly, and only for storage, never as a "live" medium for
editing.

-- 
Grant Edwards               grant.b.edwards        Yow! I own seven-eighths of
                                  at               all the artists in downtown
                              gmail.com            Burbank!

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web