tclsh

why does tput lines return different values?


I'm running a tclsh script and I'm trying to determine the number of lines in the terminal window. When I run the command from the command line I get the right number (70).

$ tclsh
% tput lines
70

But when I run the command from inside a puts/exec I get a different number (24).

$ tclsh
% puts [exec tput lines]
24

The number I'm looking for is 70... what am I doing wrong?


Solution

  • 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.

    Reference: https://groups.google.com/g/comp.lang.tcl/c/bX8wjU8Z2To

    $ tput lines
    43
    $ tput cols
    87
    $ tclsh
    % tput lines
    43
    % tput cols
    87
    % exec tput lines
    24
    % exec tput cols
    80
    % exec tput lines >@stdout
    43
    % exec tput cols >@stdout
    87
    %