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

Determine terminal width, strange behavior of tput

355 views
Skip to first unread message

de Paljas

unread,
Sep 7, 2010, 7:09:04 AM9/7/10
to
Hi guys,

I'm trying to determine the terminal window from within a TCL script.
I was playing around with Linux's 'tput cols' and get strange
differences between using and not using exec:

% tput cols
153 (correct)
% exec tput cols
80 (incorrect)

So, ok, I got the right answer, but that works only for interactive
shell; in a script I get invalid command tput.

So:
1. How can I get exec tput to behave as tput, or;
2. How do I determine the terminal width from within a TCL script?

de Paljas

unread,
Sep 7, 2010, 10:10:56 AM9/7/10
to
I just found a way to answer (2):

set width [lindex [exec stty size] 1]

Alexandre Ferrieux

unread,
Sep 7, 2010, 10:24:53 AM9/7/10
to
On Sep 7, 9:09 am, de Paljas <depal...@gmail.com> wrote:
> Hi guys,
>
> I'm trying to determine the terminal window from within a TCL script.
> I was playing around with Linux's 'tput cols' and get strange
> differences between using and not using exec:
>
> % tput cols
> 153 (correct)
> % exec tput cols
> 80 (incorrect)
>
> So, ok, I got  the right answer, but that works only for interactive
> shell; in a script I get invalid command tput.
>
> So:
> 1. How can I get exec tput to behave as tput, or;

Use [exec tput cols >@ stdout].
The reason is that without the >@ redirection, Tcl uses a pipe reading
from the child's stdout (to produce [exec]'s result). Hence tput's
ioctl()s operate on the pipe, which gets a default geometry unrelated
to the surrounding terminal.

> 2. How do I determine the terminal width from within a TCL script?

Now, the problem is that you _also_ want the script to get at the
value :}
It turns out that tputs is an utterly stupid tool, designed to be used
only be human eyes: it queries fd 1, *and* writes the info to it !

As you found out, stty is smarter, in that it does its ioctl() on fd
0. This explains why it works in [exec], which has an implicit "<@
stdin" (no redirection needed).

-Alex

0 new messages