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


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

Convert input to upper case on screen as it is typed

Started byBen Finney <ben+python@benfinney.id.au>
First post2016-04-14 13:25 +1000
Last post2016-04-19 17:46 +1200
Articles 11 — 6 participants

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

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Convert input to upper case on screen as it is typed Ben Finney <ben+python@benfinney.id.au> - 2016-04-14 13:25 +1000
    Re: Convert input to upper case on screen as it is typed Dan Sommers <dan@tombstonezero.net> - 2016-04-14 04:16 +0000
      Re: Convert input to upper case on screen as it is typed Ben Finney <ben+python@benfinney.id.au> - 2016-04-14 15:17 +1000
        Re: Convert input to upper case on screen as it is typed Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-04-14 18:35 +1200
      Re: Convert input to upper case on screen as it is typed Ben Finney <ben+python@benfinney.id.au> - 2016-04-14 17:53 +1000
        Re: Convert input to upper case on screen as it is typed Steven D'Aprano <steve@pearwood.info> - 2016-04-14 21:43 +1000
      Re: Convert input to upper case on screen as it is typed Ben Finney <ben+python@benfinney.id.au> - 2016-04-14 18:37 +1000
        Re: Convert input to upper case on screen as it is typed Marko Rauhamaa <marko@pacujo.net> - 2016-04-14 12:22 +0300
      Re: Convert input to upper case on screen as it is typed Chris Angelico <rosuav@gmail.com> - 2016-04-14 18:45 +1000
      Re: Convert input to upper case on screen as it is typed Ben Finney <ben+python@benfinney.id.au> - 2016-04-19 05:11 +1000
        Re: Convert input to upper case on screen as it is typed Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-04-19 17:46 +1200

#106970 — Convert input to upper case on screen as it is typed

FromBen Finney <ben+python@benfinney.id.au>
Date2016-04-14 13:25 +1000
SubjectConvert input to upper case on screen as it is typed
Message-ID<mailman.90.1460604334.15650.python-list@python.org>
How can my Python program convert the user's keyboard input to upper
case, as though the user has CAPS LOCK enabled?

I want to emulate a program running on a computer which doesn't have any
lower-case letters (i.e. a character set more limited than ASCII).

The text input, typed interactively by the user at the keyboard, should
be forced to upper case in real time as though that's what the user
typed.

The command line interface could use Python's standard ‘cmd’ library, or
something else. The conversion could be done by a custom input stream,
or some other way.

I am not interested in completely re-implementing the
character-by-character input system; for example, I would like to
continue making use of readline if it is available. I only want to
translate the resulting stream as it appears visually and as it comes
into the program.

Especially important is that the interface should appear visibly
indistinguishable from someone actually typing upper case text; the
display should only ever show the user's input as the upper case text
the program will process.

-- 
 \           “It ain't so much the things we don't know that get us in |
  `\    trouble. It's the things we know that ain't so.” —Artemus Ward |
_o__)                                     (1834–1867), U.S. journalist |
Ben Finney

[toc] | [next] | [standalone]


#106971

FromDan Sommers <dan@tombstonezero.net>
Date2016-04-14 04:16 +0000
Message-ID<nen5jj$hvi$1@dont-email.me>
In reply to#106970
On Thu, 14 Apr 2016 13:25:14 +1000, Ben Finney wrote:

> How can my Python program convert the user's keyboard input to upper
> case, as though the user has CAPS LOCK enabled?

I don't know which OS you're using, but if I run "stty olcuc" in my
Linux shell, then the input driver does that for me.  (Be careful; it's
tricky at that point to "fix" your shell.)

HTH,
Dan

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


#106972

FromBen Finney <ben+python@benfinney.id.au>
Date2016-04-14 15:17 +1000
Message-ID<mailman.92.1460611058.15650.python-list@python.org>
In reply to#106971
Dan Sommers <dan@tombstonezero.net> writes:

> On Thu, 14 Apr 2016 13:25:14 +1000, Ben Finney wrote:
>
> > How can my Python program convert the user's keyboard input to upper
> > case, as though the user has CAPS LOCK enabled?
>
> I don't know which OS you're using, but if I run "stty olcuc" in my
> Linux shell, then the input driver does that for me.

Thanks for the suggestion. I need a solution that is specific to the
Python program: it should only affect this program, and should not need
anything special done to the terminal when invoking the program.

-- 
 \            “The whole area of [treating source code as intellectual |
  `\    property] is almost assuring a customer that you are not going |
_o__)               to do any innovation in the future.” —Gary Barnett |
Ben Finney

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


#106973

FromGregory Ewing <greg.ewing@canterbury.ac.nz>
Date2016-04-14 18:35 +1200
Message-ID<dn8s0rF84o0U1@mid.individual.net>
In reply to#106972
Ben Finney wrote:
> I need a solution that is specific to the
> Python program: it should only affect this program, and should not need
> anything special done to the terminal when invoking the program.

You might be able to do something with the termios module
to put the tty driver into the appropriate mode.

If you do that, you'll have to be careful to set it back
again before the program exits for any reason, otherwise
your shell session will be messed up.

-- 
Greg

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


#106979

FromBen Finney <ben+python@benfinney.id.au>
Date2016-04-14 17:53 +1000
Message-ID<mailman.96.1460620413.15650.python-list@python.org>
In reply to#106971
Dan Sommers <dan@tombstonezero.net> writes:

> I don't know which OS you're using, but if I run "stty olcuc" in my
> Linux shell, then the input driver does that for me.


Gregory Ewing <greg.ewing@canterbury.ac.nz> writes:

> You might be able to do something with the termios module
> to put the tty driver into the appropriate mode.
>
> If you do that, you'll have to be careful to set it back
> again before the program exits for any reason, otherwise
> your shell session will be messed up.


Thank you both.

Okay, ‘termios.tcgetattr’ will let me preserve the attributes, and with
Dan Sommers's suggestion of which attribute to use, I may have a shot at
setting the terminal attributes.

Then with a top-level exception handler I can clean up by restoring the
saved attributes with ‘termios.tcsetattr’.

I will investigate along those lines.

-- 
 \       “Some people have a problem, and they think “I know, I'll use |
  `\     Perl!”. Now they have some number of problems but they're not |
_o__)     sure whether it's a string or an integer.” —Benno Rice, 2011 |
Ben Finney

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


#106992

FromSteven D'Aprano <steve@pearwood.info>
Date2016-04-14 21:43 +1000
Message-ID<570f8279$0$1590$c3e8da3$5496439d@news.astraweb.com>
In reply to#106979
On Thu, 14 Apr 2016 05:53 pm, Ben Finney wrote:


> Okay, ‘termios.tcgetattr’ will let me preserve the attributes, and with
> Dan Sommers's suggestion of which attribute to use, I may have a shot at
> setting the terminal attributes.
> 
> Then with a top-level exception handler I can clean up by restoring the
> saved attributes with ‘termios.tcsetattr’.
> 
> I will investigate along those lines.

If you get this working, please post your solution here, and consider
publishing it on ActiveState:

http://code.activestate.com/recipes/



Thanks,



-- 
Steven

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


#106983

FromBen Finney <ben+python@benfinney.id.au>
Date2016-04-14 18:37 +1000
Message-ID<mailman.97.1460623092.15650.python-list@python.org>
In reply to#106971
Ben Finney <ben+python@benfinney.id.au> writes:

> Okay, ‘termios.tcgetattr’ will let me preserve the attributes, and
> with Dan Sommers's suggestion of which attribute to use, I may have a
> shot at setting the terminal attributes.

This works!

I can get the current attributes, and preserve them; then, later, force
uppercase of all terminal output regardless what the user types; then,
later, request the flag be restored to its prior setting.

This only addresses how the terminal shows its output. The input is
still received by Python as it was typed. However, converting text
behind the scenes to uppercase is a simple problem.

I had been hoping that I could simply wrap some stream in a simple
“convert what they actually type so it's upper case” text codec, without
fiddling at such a low operating-system specific level. This is rather
more esoteric than I had hoped.

But it is a working solution, and easy enough to hide in a library.

Thanks again.

-- 
 \         “I have the simplest tastes. I am always satisfied with the |
  `\    best.” —Oscar Wilde, quoted in _Chicago Brothers of the Book_, |
_o__)                                                             1917 |
Ben Finney

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


#106988

FromMarko Rauhamaa <marko@pacujo.net>
Date2016-04-14 12:22 +0300
Message-ID<87lh4gsfxr.fsf@elektro.pacujo.net>
In reply to#106983
Ben Finney <ben+python@benfinney.id.au>:

> I had been hoping that I could simply wrap some stream in a simple
> “convert what they actually type so it's upper case” text codec,
> without fiddling at such a low operating-system specific level. This
> is rather more esoteric than I had hoped.

If you run your program in a Linux terminal, you have to abide by the
Linux terminal principles. It is not a simple Python matter. Echoing is
performed by the terminal driver and not by the Python program.

I do commend Python for having such complete coverage of Linux system
facilities.


Marko

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


#106984

FromChris Angelico <rosuav@gmail.com>
Date2016-04-14 18:45 +1000
Message-ID<mailman.98.1460623542.15650.python-list@python.org>
In reply to#106971
On Thu, Apr 14, 2016 at 6:37 PM, Ben Finney <ben+python@benfinney.id.au> wrote:
> I had been hoping that I could simply wrap some stream in a simple
> “convert what they actually type so it's upper case” text codec, without
> fiddling at such a low operating-system specific level. This is rather
> more esoteric than I had hoped.

If all you were doing was reading from stdin, then it probably would
be that easy. Wanting readline to work correctly requires a bit more
work; and don't forget that there are escape sequences that should
*not* be uppercased (as they'll change in meaning). So yeah, it's a
bit esoteric... but I'm glad it's working.

ChrisA

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


#107275

FromBen Finney <ben+python@benfinney.id.au>
Date2016-04-19 05:11 +1000
Message-ID<mailman.155.1461006711.6324.python-list@python.org>
In reply to#106971
Ben Finney <ben+python@benfinney.id.au> writes:

> Ben Finney <ben+python@benfinney.id.au> writes:
>
> > Okay, ‘termios.tcgetattr’ will let me preserve the attributes, and
> > with Dan Sommers's suggestion of which attribute to use, I may have
> > a shot at setting the terminal attributes.
>
> This works!

Except, it doesn't. As designed and documented in the Single Unix
Specification <URL:http://pubs.opengroup.org/onlinepubs/7908799/>,
the behaviour of the OLCUC feature is:

    OLCUC     Map lower case to upper on output. (LEGACY)

    <URL:http://pubs.opengroup.org/onlinepubs/7908799/xbd/termios.html>

That ominous “LEGACY” annotation is applied, notably, to all the
case-conversion features of ‘termios’.

The mapping is ignorant of Unicode, ignorant of ANSI escape sequences;
it blindly converts bytes via a mapping that assumes all bytes are ASCII
text, regardless of whether those bytes actually represent text.

It breaks the “visual bell” feature, among others. I end up with garbage
on the screen, that would otherwise have been Unicode text or useful
control sequences.

> I had been hoping that I could simply wrap some stream in a simple
> “convert what they actually type so it's upper case” text codec,
> without fiddling at such a low operating-system specific level. This
> is rather more esoteric than I had hoped.

And, it turns out, doesn't address the requirements without causing more
problems.

I am still looking for a solution (a Python-specific one would be fine).

-- 
 \           “Anything that we scientists can do to weaken the hold of |
  `\        religion should be done and may in the end be our greatest |
_o__)                  contribution to civilization.” —Steven Weinberg |
Ben Finney

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


#107294

FromGregory Ewing <greg.ewing@canterbury.ac.nz>
Date2016-04-19 17:46 +1200
Message-ID<dnlv1pFhf8eU1@mid.individual.net>
In reply to#107275
Ben Finney wrote:
> 
> I am still looking for a solution (a Python-specific one would be fine).
>

The only other way I can think of is to put the tty
into raw mode and do your own line editing and echoing.
You could wrap it all up in a file-like object for the
rest of the code to use.

-- 
Greg

[toc] | [prev] | [standalone]


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


csiph-web