Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What is the rational? #4

Closed
runeimp opened this issue Jul 24, 2021 · 7 comments
Closed

What is the rational? #4

runeimp opened this issue Jul 24, 2021 · 7 comments

Comments

@runeimp
Copy link

runeimp commented Jul 24, 2021

I'm curious what use case you had where the terminal size was needed but not available, such as in a non-interactive shell?

BTW, you can use the POSIX standard utility stty , on everything but Windows from what I can tell, to get the terminal size.

Subwindow of KiTTY

$ stty size
35 128
$ stty size &> x; cat x
35 128

If you just want the cols

$ stty size | cut -d' ' -f2
128

iTerm2

$ stty size
25 78
$ stty size &> x; cat x
25 78

tmux in iTerm2

$ tput cols
78
$ tput lines
24
$ stty size
24 78
$ stty size &> x; cat x
24 78

Apple Terminal

$ tput cols 
120
$ tput lines
36
$ stty size            
36 120
$ stty size &> x; cat x
36 120
@Alhadis
Copy link

Alhadis commented Jul 25, 2021

I came here to point this out too.

Note that it's also possible to query the dimensions of terminal windows other than the current one:

$ stty -f /dev/ttys001 size
$ stty -f "`tty`" size

@jpmhouston
Copy link

"Rationale"?

Not (just) trying to be an a**hat, I seriously couldn't figure out what the title meant before opening and thought it was math related.

Nonetheless, up-voted.

@sindresorhus
Copy link
Owner

I needed it for term-size.


// x.js
const childProcess = require('child_process');

childProcess.execFile('stty', ['size'], (error, stdout) => {
	console.log(error || stdout);
});
$ node x.js
Error: Command failed: stty size
stty: stdin isn't a terminal

    at ChildProcess.exithandler (child_process.js:308:12)
    at ChildProcess.emit (events.js:314:20)
    at maybeClose (internal/child_process.js:1022:16)
    at Socket.<anonymous> (internal/child_process.js:444:11)
    at Socket.emit (events.js:314:20)
    at Pipe.<anonymous> (net.js:675:12) {
  killed: false,
  code: 1,
  signal: null,
  cmd: 'stty size'
}

@Alhadis
Copy link

Alhadis commented Aug 2, 2021

Try this:

const childProcess = require("child_process");

const {stdout: tty} = childProcess.spawnSync("tty", {
	stdio: ["inherit", "pipe", "inherit"],
	encoding: "utf8",
});

childProcess.execFile("stty", ["-f", tty.trim(), "size"], (error, stdout) => {
	console.log(error || stdout);
});

However, it's much easier to use the getters that Node.js provides natively:

const size =
	process.stdout.isTTY ? [process.stdout.columns, process.stdout.rows] :
	process.stderr.isTTY ? [process.stderr.columns, process.stderr.rows] :
	process.stdin.isTTY  ? [process.stdin.columns,  process.stdin.rows]  :
	null;

@sindresorhus
Copy link
Owner

  1. It cannot use inherit as that could potentially output stuff directly to the users terminal, and a reusable package should not do that.
  2. It has to work even if the code spawning stty is spawned itself. Meaning multiple levels of spawning.
  3. process.stdout.columns is undefined if it's in a spawned file.

The point of this binary is to get the columns and rows no matter what.

@Alhadis
Copy link

Alhadis commented Aug 2, 2021

It cannot use inherit as that could potentially output stuff directly to the users terminal

Not if you're inheriting only the standard input stream:

 const {stdout: tty} = childProcess.spawnSync("tty", {
-	stdio: ["inherit", "pipe", "inherit"],
+	stdio: ["inherit", "pipe", "pipe"],
 	encoding: "utf8",
 });

The stdio array pertains to STDIN, STDOUT, and STDERR, respectively.

@sindresorhus
Copy link
Owner

Closing as this is no longer used by term-size: https://github.com/sindresorhus/terminal-size/releases/tag/v4.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants