The POSIX standard is, indeed, that the value of the COLUMNS
environment variable supersedes other ways of obtaining the terminal size.
The standard describes one of the variable's purposes as "display[ing] data in an area arbitrarily smaller than the terminal or window" amongst other things, which matches what you want.
Those other ways are not standardized, but they are commonly an ioctl()
of TIOCGWINSZ
against a terminal device open file descriptor.
Your program does this; albeit that it just assumes without isatty()
checks that its standard output is a terminal device.
Unfortunately, there are programs that do not respect the COLUMNS
environment variable and yours is sadly one of them.
Instead it provides is own idiosyncratic non-standard mechanism, the detection
and defaultwidth
settings in its configuration file.
Adding to the sadness is that these operate the other way around to the COLUMNS
environment variable.
They are a fallback, not an override.
You have to turn detection
off, so that the program stops using TIOCGWINSZ
and falls back to the default.
You have to then set a value for defaultwidth
, which is 80 if unset and 65536 with detection forced off if zero.
Further addition to the sadness is that turning detection
off applies to both width and height, unlike the POSIX standard COLUMNS
and LINES
variables which operate independently of each other.
So you have to concern yourself with a defaultheight
as well, which is 24 if unset and 65536 with detection forced off if zero.
You could, alternatively, leave detection on and fool your program with a pseudo-terminal instead.
tmux
is a complex way to achieve this, with all that key stuffing and intermediary file use.
Using Bernstein ptybandage
and stty columns
(against the internal pseudo-terminal) is simpler, takes one line, and outputs to standard output in real time as your program is running:
ptybandage sh -c 'stty columns 40 ; exec task'
Further reading
- Daniel J. Bernstein (1996).
ptybandage
. djbwares.
- Jonathan de Boyne Pollard (2014).
ptybandage
. nosh Guide. Softwares.