Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Termcap/terminfo for Python?

923 views
Skip to first unread message

Oleg Broytmann

unread,
Oct 17, 2001, 3:31:28 PM10/17/01
to
On Wed, Oct 17, 2001 at 03:03:38PM -0400, Greg Ward wrote:
> Does anyone know if there exists an interface to termcap or terminfo for
> Python?

curses.tigetflag
curses.tigetnum
curses.tigetstr

Example:

phd@phd 1 >> python
Python 2.1.1 (#1, Aug 8 2001, 19:17:29)
[GCC 2.95.2 20000220 (Debian GNU/Linux)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> import curses
>>> curses.setupterm()
>>> curses.tigetstr("khome")
'\x1b[1~'

I don't know which attributes describe width/height of a terminal.

Oleg.
--
Oleg Broytmann http://phd.pp.ru/ p...@phd.pp.ru
Programmers don't die, they just GOSUB without RETURN.

Greg Ward

unread,
Oct 17, 2001, 3:03:38 PM10/17/01
to
Does anyone know if there exists an interface to termcap or terminfo for
Python? I did a Google search for "terminfo python", and the only
vaguely relevant hits were:
* O'Reilly's *Termcap & Terminfo* book (because O'Reilly's
sidebar has the word "Python" in it)
* a post to comp.long.python by Oliver Andrich from 1996

Not a good sign. Perhaps I'm missing something. Or am I really the
only person in Python's long and glorious history who has wanted to find
out the width of his terminal?

Greg
--
Greg Ward - software developer gw...@mems-exchange.org
MEMS Exchange http://www.mems-exchange.org

Greg Ward

unread,
Oct 17, 2001, 5:24:35 PM10/17/01
to
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")

Oleg Broytmann

unread,
Oct 18, 2001, 4:09:43 AM10/18/01
to
On Wed, Oct 17, 2001 at 05:24:35PM -0400, Greg Ward wrote:
> 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")

Now thank YOU :) I think I'll easily find neccessary information on it.
I didn't know about tiget* functions before your question. But I thought
"well, there is no direct termcap/info interfaces, may be curses?" and
found all neccessary things in docs :) I ran an example, got a traceback
with the mesage "at least run setupterm()" - and voila! :)

Michael Hudson

unread,
Oct 18, 2001, 5:36:22 AM10/18/01
to
Greg Ward <gw...@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

0 new messages