Exploring terminfo

Cameron Simpson cs at cskk.id.au
Fri Jan 15 21:50:38 EST 2021


On 15Jan2021 18:36, Alan Gauld <alan.gauld at yahoo.co.uk> wrote:
>> One difference is that the name prompt is being written to stdout in
>> the C version and stderr in the Python version. But I don't see why
>> that would matter.
>
>That could make a big difference, the putp() function specifically
>states that it writes to stdout.

If you want an example of code using the curses.ti* functions with 
arbitrary Python files, have a gander at this:

    https://hg.sr.ht/~cameron-simpson/css/browse/lib/python/cs/upd.py?rev=tip

It's on PyPI if you want easy installation in addition to the source:

    https://pypi.org/project/cs.upd/

I'm not (yet) doing colour with that, but I _am_ doing cursor motions 
etc, since that supports multiple lines of display (typical use case is 
multiple progress bars for parallel tasks).

It looks up and uses a few things, like insert_line etc, since the 
interface with my logging goes: go to the top status line, insert a 
line, write the error message, resume.

>> Now the question: is there a way to tell the curses module to flush
>> its stdout FILE stream?
>
>Indeed. But unless it's trivial it rather defeats the concept of
>using the terminfo functions to create text effects without
>diving into full curses screen control! And that was what I
>was hoping to uncover.
>
>I wonder if I can use the os module to mess with the
>file descriptors.... hmmm.

That is massive overkill. It is enough to write various strings to your 
desired output and flush it. As encountered earlier, curses strings are 
bytes, and need to be decodes as ASCII for str use.

>Thanks for that snippet, I had no idea that input() wrote
>to stderr

Lots of things like to do that. It is a hack, but it does avoid 
polluting your stdout with prompts. (Taxonomy of Bugs: "plug: a message 
dropped in the middle of the output data stream".)

>nor that Python added a stdout layer on top of libc.

It might put its stdout layer directly on top of fd 1, rather than the C 
stdout system. That rings a bell with me.

>Do you know where that is documented? I'd be interested in
>reading more.

The io module?

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list