Termcap/terminfo for Python?

Michael Hudson mwh at python.net
Thu Oct 18 05:36:22 EDT 2001


Greg Ward <gward at mems-exchange.org> writes:

> On 17 October 2001, Oleg Broytmann said:
> >    curses.tigetflag
> >    curses.tigetnum
> >    curses.tigetstr
> 
> Ahh, thank you.
> 
> >    I don't know which attributes describe width/height of a terminal.
> 
> For the record:
>   curses.tigetnum("cols")
>   curses.tigetnum("lines")
> 

Here's what I do:

def getheightwidth():
    """ getwidth() -> (int, int)

    Return the height and width of the console in characters """
    try:
        return int(os.environ["LINES"]), int(os.environ["COLUMNS"])
    except KeyError:
        height, width = struct.unpack(
            "hhhh", ioctl(0, TIOCGWINSZ ,"\000"*8))[0:2]
        if not height: return 25, 80
        return height, width

Where TIOCGWINSZ lives is an interesting game across Python versions.
It's in termios now.

I should probably look at terminfo too, but that would be the last
check.

I think ncurses' own order of checking goes:

env vars
ioctl
terminfo
25x80

but I haven't looked at this for a while.

HTH,
M.

-- 
48. The best book on programming for the layman is "Alice in
    Wonderland"; but that's because it's the best book on
    anything for the layman.
  -- Alan Perlis, http://www.cs.yale.edu/homes/perlis-alan/quotes.html



More information about the Python-list mailing list