Exploring terminfo

Grant Edwards grant.b.edwards at gmail.com
Fri Jan 15 17:09:06 EST 2021


On 2021-01-15, Grant Edwards <grant.b.edwards at gmail.com> wrote:
> On 2021-01-15, Alan Gauld via Python-list <python-list at python.org> wrote:
>> On 15/01/2021 17:31, Grant Edwards wrote:
>>
>>> I suspect that the problem is that putp is writing to the libc
>>> "stdout" FILE stream that's declaredin <stdio.h>. That stream
>>> layer/object has buffering that is invisible to Python. 
>>
>> That would indeed explain it.
>>
>>> 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!
>
> Indeed, that appears to be the case for Python 3.x (unless we can
> figure out a way to flush <stdio.h>'s FILE *stdio stream from Python).
> I started a separate thread about that specific topic.

I've already posted code showing how to call fflush(FILE *stdio). An
even cleaner solution is avoid using the libc FILE* stuff
entirely. Just grab the strings you need from ncurses and output them
via Pythons I/O streams:

    import curses
    curses.setupterm()

    bold = curses.tigetstr('bold').decode('ascii')
    norm = curses.tigetstr('sgr0').decode('ascii')
    cls = curses.tigetstr('clear').decode('ascii')

    print(cls,end='')

    name = input("enter name: ")

    print(f'{bold}Hi there {name}{norm}')

    input("press enter to exit: ")


Though that seems to work fine with urxvt, it _might_ not be quite
kosher according the ncurses man page, since it says:

  Parameterized strings should be passed through tparm to instantiate
  them.  All terminfo strings (including the output of tparm) should be
  printed with tputs or putp.

Apparently that's mainly so that it can "apply padding information". I
have no idea what that means:

  The tputs routine applies padding information to the string str and
  outputs it:

Entities that are parameterized (e.g. goto location) would need to be
passed through curses.tiparm() before they're decoded and printed. I'm
goign to try that next.



More information about the Python-list mailing list