Giter Club home page Giter Club logo

term-size's Introduction

terminal-size

Reliably get the terminal window size

Because process.stdout.columns doesn't exist when run non-interactively, for example, in a child process or when piped. This module even works when all the TTY file descriptors are redirected!

Confirmed working on macOS, Linux, and Windows.

Install

npm install terminal-size

Usage

import terminalSize from 'terminal-size';

terminalSize();
//=> {columns: 143, rows: 24}

API

terminalSize()

Returns an object with columns and rows properties.

Related

term-size's People

Contributors

bendingbender avatar brandon93s avatar coreyfarrell avatar fabiospampinato avatar ianobermiller avatar richienb avatar rvagg avatar shinnn avatar sindresorhus avatar styfle avatar timdp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

term-size's Issues

Not working when piped on macOS

I had a similar idea, to read the columns using tput 'cols' for that matter, but still, the child process, does not have any knowledge of the parent window width, hence it does not quite work, it cannot be used to reliably get the window width, as you can see below.

screen shot 2016-09-11 at 11 13 01
Looking at the above, the tests should not have passed. Sizes are different.

Codesign / notarize binary for Mac distribution

Issuehunt badges

I am working on a product that distributes node and npm as a dependency for an application, which revealed that the 'term-size' MacOS binary is not signed. As 'term-size' is a dependency of npm (a transitive dependency inside boxen which is inside of update-notifier), it would be good for the distributed binary to be codesigned by the entity that makes the binary.

The current workaround is to codesign it ourselves.


IssueHunt Summary

rvagg rvagg has been rewarded.

Backers (Total: $40.00)

Submitted pull Requests


Tips


IssueHunt has been backed by the following sponsors. Become a sponsor

Window size detection for Git Bash pager falls back to default values

Hey there, this is kind of a follow-up to #22. It works fine for the normal git bash window, however when opening e.g. a less pager in that window, it falls back to the default 80/24 values.
I've debugged it and I think I found a bug, but I'd like your confirmation on this.

STR

  1. Comment out the following in the index.js to simulate the pager without having to actually have a pager (line 60 is where we'd like to go):
    grafik
  2. Temporarily add the following to the bottom of the index.js to execute the size function upon calling the script
var x = terminalSize();
console.log(x);
  1. Execute the index.js in a Git Bash and see that it's not returning the correct window size.
    grafik

Possible Solution

The index.js contains this line:
grafik

The 'ignore' is duplicated and when I replace the second one with process.stderr, it's working fine.

grafik

Resulting in correct window sizes:

grafik

Any opinion on this?

Use native FFI when available

Node.js, or rather V8, is planning native FFI support. This would be much better than spawning binaries.

Just for reference, I can't use node-ffi today as it's a native addon and that comes with too many headaches.

why not use the standard tty package in combination with opening /dev/tty

I can't print the output to stdout, because its redirected :-), so I use the exit status.

Tested on Linux with node >= 6.x. Also tried rows. I'd expect this to be ancient unix behaviour shared with OS X.

core/node (master $ u=) % node -p 'require("tty").WriteStream(require("fs").openSync("/dev/tty", "w")).columns'     
174
core/node (master $ u=) % node -p 'process.exit(require("tty").WriteStream(require("fs").openSync("/dev/tty", "w")).columns)' < /dev/null >/dev/null 2>/dev/null; echo $?
174
core/node (master $ u=) % nvm run 13 -p 'process.exit(require("tty").WriteStream(require("fs").openSync("/dev/tty", "w")).columns)' < /dev/null >/dev/null 2>/dev/null; echo $?
174
core/node (master $ u=) % nvm run 12 -p 'process.exit(require("tty").WriteStream(require("fs").openSync("/dev/tty", "w")).columns)' < /dev/null >/dev/null 2>/dev/null; echo $?
174
core/node (master $ u=) % nvm run 10 -p 'process.exit(require("tty").WriteStream(require("fs").openSync("/dev/tty", "w")).columns)' < /dev/null >/dev/null 2>/dev/null; echo $?
174
core/node (master $ u=) % nvm run 8 -p 'process.exit(require("tty").WriteStream(require("fs").openSync("/dev/tty", "w")).columns)' < /dev/null >/dev/null 2>/dev/null; echo $?

174
core/node (master $ u=) % nvm run 6 -p 'process.exit(require("tty").WriteStream(require("fs").openSync("/dev/tty", "w")).columns)' < /dev/null >/dev/null 2>/dev/null; echo $?

174

Process suspended when stdout and stderr are redirected and the process is daemonized

So trying to fix this issue I found that in this line of term-size stops the execution:

const size = execa.sync('resize', ['-u']).stdout.match(/\d+/g);

Reproduce

With the vendor/resize binary, execute a script:

#!/usr/bin/env node
const path = require('path')
const execa = require('execa')
const size = execa.shellSync(path.join(__dirname, 'resize'), ['-u']).stdout.match(/\d+/g)
console.log(size)

And executing like this:

$ ./index.js >log.txt 2>&1 &
[2] 15736
[2]  + 15736 suspended (tty output)  ./index.js > log.txt 2>&1
$ cat log.txt

$ 

This doesn't happen when you don't run the process in the background (so, without the last &):

$ ./index.js >log.txt 2>&1
$ cat log.txt
[ '111', '25' ]
$

This could be an error in execa or in the resize binary, I have no clue.

vendor/resize build instruction/source

I'd like to be able to rebuild vendor/resize binary for the debian package. Or can I skip that binary if I only wanted to be able to use term-size from ava.

Support Apple silicon (M1/M2) chips natively

I have an Apple silicon device and am trying to avoid installing Rosetta and use native versions of applications. When using term-size, I get this error:

/bin/sh: /Users/x/y/node_modules/term-size/vendor/macos/term-size: Bad CPU type in executable

It would be great if term-size were updated to support Apple silicon without requiring Rosetta, either by including an Apple silicon binary or not requiring a binary at all.

Support for Git Bash (xTerm / mintty)

Hey there,

could you add support for the Git Bash console (that you get by default when installing on Windows)?
Getting the size has basically been done (I shamelessly stole most of this from here, around line 90)

const {execSync} = require('child_process');
var buf = execSync('tput cols && tput lines', {stdio: ['ignore', 'pipe', process.stderr]});
var sz = buf.toString().trim().split('\n');
if(sz && sz.length == 2){
    return create(sz[0], sz[1]);
}

I can create a PR if you want, however I'm not really a js-dev, so you might want to do this yourself.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.