Reliably get the terminal window size

Overview

term-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 term-size

Usage

const termSize = require('term-size');

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

API

termSize()

Returns an object with columns and rows properties.

Info

The bundled macOS binary is signed and hardened.

Related


Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.
Comments
  • Fix bug where execFileSync returned Uint8Array instead of String

    Fix bug where execFileSync returned Uint8Array instead of String

    I'm using cmder (/ConEmu) on Windows 10, and child_process.execFileSync is returning an Uint8Array instead of a String. Resulting in a fixed console width of 80 columns; the fallback width, as the array doesn't have a trim method.

    Fixed this by forcing the response to become a string, before calling the trim method.

    opened by smeijer 4
  • Codesign / notarize binary for Mac distribution

    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

    :gift: Rewarded on Issuehunt 
    opened by benbrackenc74 4
  • Not working when piped on macOS

    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.

    opened by aichholzer 4
  • tput: No value for $TERM and no -T specified

    tput: No value for $TERM and no -T specified

    I'm seeing this error when process.env.TERM is undefined and process.stdout.isTTY is undefined.

    tput: No value for $TERM and no -T specified

    It happens on line 60 when tput cols is executed.

    https://github.com/sindresorhus/term-size/blob/92aa07ee3a1cc28360af1c908f8a2ae10b6552bd/index.js#L60

    opened by styfle 3
  • Update execa from 0.7.x to 0.8.x

    Update execa from 0.7.x to 0.8.x

    https://github.com/sindresorhus/execa/compare/v0.7.0...v0.8.0

    Nothing affects term-size, but this update helps better deduplication for the project using the latest execa.

    opened by shinnn 3
  • Do not construct dirname on Linux

    Do not construct dirname on Linux

    I'm bundling term-size using esbuild, which handles import.meta.url a bit differently than other bundlers.

    Now, in my case, the fileURLToPath call is dead code, because I'm running my bundle on Linux, where term-size doesn't need to invoke any commands. So I was thinking it could be more lazy, and coincidentally fix the issue I'm facing.

    This PR moves the __dirname emulation into a helper function that's only called on macOS and Windows.

    I've also put the line-splitting in there because I prefer DRY code, but it's a bit esoteric this way. Would you prefer an explicit splitLines (or even a dependency on split-lines)? Or alternatively, I could make a less invasive PR and only extract the __dirname construction into a helper function, for example.

    ... Or we could find a better way to deal with the lack of __dirname in ESM. 😉

    opened by timdp 2
  • Ignore stderr on spawned processes

    Ignore stderr on spawned processes

    When using term-info on a non-interactive terminal (like in CI or a pre-commit hook in Sublime Merge), resize and tput write out to stderr:

    Opening `/dev/tty` failed (6): Device not configured
    resize:  can't open terminal /dev/tty
    tput: No value for $TERM and no -T specified
    

    We can suppress these by passing 'ignore' for the third tuple value of stdio, which corresponds to stderr.

    opened by ianobermiller 2
  • .DS_Store files in the package

    .DS_Store files in the package

    2.2.0 has a bunch of .DS_Store files in the vendor subdirectory. https://unpkg.com/browse/[email protected]/vendor/

    Publishing from a newer npm version should not include them, but in case they are included you can add a .npmignore file in the vendor subdirectory and add .DS_Store

    opened by reviewher 2
  • vendor/resize build instruction/source

    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.

    opened by pravi 2
  • New macOS binary, signed with Node.js Foundation identity and hardened

    New macOS binary, signed with Node.js Foundation identity and hardened

    Ref: https://github.com/sindresorhus/macos-term-size/pull/3/ Ref: https://github.com/nodejs/node/pull/31459

    $ codesign -dv vendor/macos/term-size
    Executable=/Users/rvagg/git/term-size/vendor/macos/term-size
    Identifier=term-size
    Format=Mach-O thin (x86_64)
    CodeDirectory v=20500 size=277 flags=0x10000(runtime) hashes=3+2 location=embedded
    Signature size=8986
    Timestamp=30 Jan 2020 at 9:29:23 pm
    Info.plist=not bound
    TeamIdentifier=HX7739G8FX
    Runtime Version=10.14.0
    Sealed Resources=none
    Internal requirements count=1 size=172
    

    Have also tested the binary on a 10.10 machine and it's :thumbsup:.

    (for puzzled onlookers: this is being done because this is bundled with npm and we need to notarize our packages which includes having all executable binaries signed)


    IssueHunt Summary

    Referenced issues

    This pull request has been submitted to:


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

    opened by rvagg 0
  • Slim down dependency tree

    Slim down dependency tree

    • Replaced “execa” with the native “child_process” module.
    • Imported “strip-eof” directly.

    I tested this manually as the provided tests won't hit the lines I changed, and they will basically always pass anyway since this library always provides a fallback size.

    opened by fabiospampinato 0
  • Support for Git Bash (xTerm / mintty)

    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.

    opened by Velociraptor45 0
  • term-size package contains exe file

    term-size package contains exe file

    Hi, is it possible to eliminate the exe file inside the package? https://registry.npmjs.org/term-size/-/term-size-2.1.1.tgz because of this our organization it security web filtering block access to this module and it causes us a lot of trouble.

    opened by myyosi 1
  • why not use the standard tty package in combination with opening /dev/tty

    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
    
    opened by sam-github 7
  • Use native FFI when available

    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.

    opened by sindresorhus 0
  • Process suspended when stdout and stderr are redirected and the process is daemonized

    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.

    opened by pablopunk 3
Releases(v3.0.2)
  • v3.0.2(May 24, 2022)

  • v3.0.1(Oct 7, 2021)

  • v3.0.0(Aug 18, 2021)

  • v2.2.1(Oct 25, 2020)

  • v2.2.0(Jan 30, 2020)

    • New macOS binary, signed with Node.js Foundation identity and hardened (#16) 723563c

    https://github.com/sindresorhus/term-size/compare/v2.1.1...v2.2.0

    Source code(tar.gz)
    Source code(zip)
  • v2.1.1(Dec 18, 2019)

  • v2.1.0(May 25, 2019)

  • v2.0.0(Apr 28, 2019)

    Breaking:

    • Require Node.js 8 (#9) 85e3e8e

    Enhancements:

    • Add TypeScript definition (#9) 85e3e8e

    https://github.com/sindresorhus/term-size/compare/v1.2.0...v2.0.0

    Source code(tar.gz)
    Source code(zip)
Owner
Sindre Sorhus
Full-Time Open-Sourcerer. Wants more empathy & kindness in open source. Focuses on Swift & JavaScript. Makes macOS apps, CLI tools, npm packages. Likes unicorns
Sindre Sorhus
Terminal ui for discord with interactive terminal

dickord why No fucking clue i was bored or something. why does it look dogshit Try and find a node module that supports terminal functions like trauma

Hima 3 Nov 7, 2022
Sublime-like terminal-based text editor

slap ?? slap is a Sublime-like terminal-based text editor that strives to make editing from the terminal easier. It has: first-class mouse support (ev

slap 6.1k Jan 1, 2023
A terminal-to-gif recorder minus the headaches.

ttystudio A terminal-to-gif recorder minus the headaches. Record your terminal and compile it to a GIF or APNG without any external dependencies, bash

Christopher Jeffrey (JJ) 3.2k Dec 23, 2022
rtail(1) - Terminal output to the browser in seconds, using UNIX pipes.

rtail(1) Terminal output to the browser in seconds, using UNIX pipes. rtail is a command line utility that grabs every line in stdin and broadcasts it

Kilian Ciuffolo 1.6k Jan 6, 2023
Pipeable javascript. Quickly filter, map, and reduce from the terminal

Pipeable JavaScript - another utility like sed/awk/wc... but with JS! Quickly filter, map and reduce from the command line. Features a streaming API.

Daniel St. Jules 410 Dec 10, 2022
Translations with speech synthesis in your terminal as a node package

Normit Normit is an easy way to translate stuff in your terminal. You can check out its Ruby gem version termit. Installation npm install normit -g Us

Paweł Urbanek 234 Jan 1, 2023
Terminal recorder: Record your termial session into HTML

terminal-recorder Terminal recorder allows you to record your bash session, and export it to html so then you can share it with your friends. GitHub P

Cristian Cortez 104 Mar 3, 2022
Terminal task list

listr Terminal task list Install $ npm install --save listr Usage const execa = require('execa'); const Listr = require('listr'); const tasks = new

Sam Verschueren 3.1k Jan 3, 2023
📜 Create mutable log lines into the terminal, and give life to your logs!

Because Logging can be pretty and fun Installation $ npm install draftlog What it does It allows you to re-write a line of your log after being writt

Ivan Seidel 1.2k Dec 31, 2022
Display images in the terminal

terminal-image Display images in the terminal Works in any terminal that supports colors. In iTerm, the image will be displayed in full resolution, si

Sindre Sorhus 905 Dec 25, 2022
:rainbow: Beautiful color gradients in terminal output

gradient-string Beautiful color gradients in terminal output Install $ npm i gradient-string Usage const gradient = require('gradient-string'); cons

Boris K 864 Jan 3, 2023
Create clickable links in the terminal

terminal-link Create clickable links in the terminal Install $ npm install terminal-link Usage import terminalLink from 'terminal-link'; const link

Sindre Sorhus 539 Dec 31, 2022
Truncate a string to a specific width in the terminal

cli-truncate Truncate a string to a specific width in the terminal Gracefully handles ANSI escapes. Like a string styled with chalk. It also supports

Sindre Sorhus 78 Oct 10, 2022
Execute shell commands in terminal

Execute shell commands in terminal

skanehira 9 Dec 11, 2021
Add a hungry turtle to your terminal and feed it every time you mistype 'npm' as 'nom'

Nom Does this ever happen to you? You happily code away on a project, navigating the command line like a pro, testing, error logging, installing packa

Meike Hankewicz 5 Apr 26, 2022
Just a minimal library to do some terminal stuff.

Termctl A simple library to do some basic terminal stuff. Usage const termctl = require("termctl"); Note: We have tested this on Linux Mint and Window

Biraj 4 Sep 28, 2021
A terminal for a more modern age

Downloads: Latest release Repositories: Debian/Ubuntu-based, RPM-based Latest nightly build This README is also available in: Korean 简体中文 Tabby (forme

null 41.8k Dec 30, 2022
DataENV is a cli tool that allows you to save data temporarily using your terminal.

DataEnv CLI Instllation npm install -g dataenv Usage Table of Contents LocalStorage npx dataenv save Parameters npx dataenv show Parameters npx dataen

PGamerX 2 Feb 5, 2022