Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine the terminal width when piped (to a pager like less?)

If my program is being piped to another program at the command-line, is there any way for me to still determine the width thereof?

I'm working in Node, if that's relevant, although any general POSIX solution is welcome (I can figure out how to port it to my environment myself.) Currently, I can use process.stdout.columns in Node; but that's linked to the process.stdout, which in this case is piped.

like image 517
ELLIOTTCABLE Avatar asked Oct 31 '22 04:10

ELLIOTTCABLE


1 Answers

Actually, POSIX does not define the C ioctl call used for getting the window size (see standard). But the TIOCGWINSZ call (or similar TIOCGSIZE) is widely available. Node probably relies on that in its checks.

This package checks the width for the standard output and failing that, checks the standard error: window-size (see source code).

A similar question was asked before in How wide is the node.js console?

like image 186
Thomas Dickey Avatar answered Nov 15 '22 05:11

Thomas Dickey