Exploring terminfo

Grant Edwards grant.b.edwards at gmail.com
Fri Jan 15 23:26:59 EST 2021


My previous code didn't work if the terminfo entities contained delay
specifiers because the delay specifier was left in the output
datastream. The case where the terminfo description contains delays,
but the delays aren't needed can be handled by simply removing the delay
specifiers by wrapping ncurses.tigetstr as shown below.

If you really _are_ using a physical terminal which requires delays
and is connected to a real serial port, then I think the only
practical solution is to replace sys.stdout.buffer with something that
quacks like io.BufferedWriter and uses ctypes to call fwrite() and
fflush() on FILE *stdout. That means you've got to do all the ncurses
control via curses.putp() and you can't do things like embed control
sequences in python strings like print(f'{bold}Hi there{norm}')


    #!/usr/bin/python
    
    import curses,sys,re
    
    curses.setupterm()
    
    write = sys.stdout.write
    
    def tigetstr(name):
        seq = curses.tigetstr(name)
        return re.sub(b'\$<[0-9.]+[\*/]{0,2}>', b'', seq)
    
    bold = tigetstr('bold').decode('ascii')
    norm = tigetstr('sgr0').decode('ascii')
    cls = tigetstr('clear').decode('ascii')
    cup = tigetstr('cup')
    
    def goto(row,column):
        write(curses.tparm(cup,row,column).decode('ascii'))
    
    def clear():
        write(cls)
    
    clear()    
    name = input("enter name: ")
    
    for row in [3,5,10,20]:
        goto(row, row+5)
        write(f'{bold}Hi there {name}{norm}')
        goto(row+1, row+6)
        write(f'Hi there {name}')
    
    goto(24,0)    
    input("press enter to exit: ")
    clear()



More information about the Python-list mailing list