import os,osproc,parseutils,strutils
var terminalwidth:int
terminalwidth = execCmd("tput cols")
echo "TerminalWidth : ",terminalwidth
However there maybe is only the errorcode returned, rather then the output. The output echoed on the terminal is correct and changes if I adjust the terminal width. Using execCmdEx , I get a tuple back, but it always contains [80,0] , which is wrong.
So? Do you think we have some special code in execCmdEx that lies to you? Or maybe tput is simply broken, like everything else in Unix?
http://stackoverflow.com/questions/14308166/tput-cols-doesnt-work-properly-in-a-script
#!/usr/bin/env python
import subprocess
command = ['tput', 'cols']
def get_terminal_width():
try:
width = int(subprocess.check_output(command))
except OSError as e:
print("Invalid Command '{0}': exit status ({1})".format(
command[0], e.errno))
except subprocess.CalledProcessError as e:
print("Command '{0}' returned non-zero exit status: ({1})".format(
command, e.returncode))
else:
return width
def main():
width = get_terminal_width()
if width:
print(width)
if __name__ == "__main__":
main()
The reason for this is that Nim's osproc redirects the input. For tput cols to work the input must stay stdin. I needed something like this a few days ago as well, so I guess osproc should be extended so that you can select which streams to redirect and which to keep to the standard ones.
Alternatively you can use the lower level C functions (which don't seem to be standardized at all...):
type WinSize = object
row, col, xpixel, ypixel: cushort
const TIOCGWINSZ = 0x5413
proc ioctl(fd: cint, request: culong, argp: pointer)
{.importc, header: "<sys/ioctl.h>".}
var size: WinSize
ioctl(0, TIOCGWINSZ, addr size)
echo size
echo size.col
This echoes:
(row: 24, col: 89, xpixel: 712, ypixel: 336)
89