Exploring terminfo

Grant Edwards grant.b.edwards at gmail.com
Thu Jan 14 18:08:38 EST 2021


On 2021-01-14, Eli the Bearded <*@eli.users.panix.com> wrote:

> When I've wanted to do simple things like bold and clear, I've used the
> tput(1) tool. You can capture stdout from the tool and use the output
> over and over. Typically I've done this in shell scripts:
>
> #!/bin/sh
> bold=$(tput smso)	# set mode stand out
> nobold=$(tput rmso)	# remove mode stand out
> [...]

I was about to suggest using tput.

https://tldp.org/HOWTO/Bash-Prompt-HOWTO/x405.html
https://linuxcommand.org/lc3_adv_tput.php
https://martin-thoma.com/colorize-your-scripts-output/

>From python you could actually invoke the tput executable every time
you wanted to do something, or you could cache the output strings produced by
tput as demonstrated in the bash script above.

Alternatively, I think you can use the ncurses library to retrieve the control
strings (just don't use any ncurses input/output calls), like this example from
https://stackoverflow.com/questions/6199285/tput-cup-in-python-on-the-commandline:

    from curses import *
    setupterm()

    cols = tigetnum("cols")
    lines = tigetnum("lines")
    print str(cols) + "x" + str(lines)

    place_begin = tparm(tigetstr("cup"), 15, 14)
    place_end = tparm(tigetstr("cup"), 50, 0)

    print place_begin + "-- some text --" + place_end




More information about the Python-list mailing list