├── .editorconfig ├── .gitattributes ├── .github ├── funding.yml ├── security.md └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── fixture.js ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/funding.yml: -------------------------------------------------------------------------------- 1 | github: sindresorhus 2 | open_collective: sindresorhus 3 | tidelift: npm/term-size 4 | custom: https://sindresorhus.com/donate 5 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} on ${{ matrix.os }} 8 | runs-on: ${{ matrix.os }} 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 20 14 | - 18 15 | os: 16 | - ubuntu-latest 17 | - windows-latest 18 | steps: 19 | - uses: actions/checkout@v4 20 | - uses: actions/setup-node@v4 21 | with: 22 | node-version: ${{ matrix.node-version }} 23 | - run: npm install 24 | - run: npm test 25 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /fixture.js: -------------------------------------------------------------------------------- 1 | import terminalSize from './index.js'; 2 | 3 | const {columns, rows} = terminalSize(); 4 | console.log(`${columns}\n${rows}`); 5 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | export type TerminalSize = { 2 | columns: number; 3 | rows: number; 4 | }; 5 | 6 | /** 7 | Reliably get the terminal window size. 8 | 9 | @example 10 | ``` 11 | import terminalSize from 'terminal-size'; 12 | 13 | terminalSize(); 14 | //=> {columns: 143, rows: 24} 15 | ``` 16 | */ 17 | export default function terminalSize(): TerminalSize; 18 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import process from 'node:process'; 2 | import {execFileSync} from 'node:child_process'; 3 | import fs from 'node:fs'; 4 | import tty from 'node:tty'; 5 | 6 | const defaultColumns = 80; 7 | const defaultRows = 24; 8 | 9 | const exec = (command, arguments_, {shell, env} = {}) => 10 | execFileSync(command, arguments_, { 11 | encoding: 'utf8', 12 | stdio: ['ignore', 'pipe', 'ignore'], 13 | timeout: 500, 14 | shell, 15 | env, 16 | }).trim(); 17 | 18 | const create = (columns, rows) => ({ 19 | columns: Number.parseInt(columns, 10), 20 | rows: Number.parseInt(rows, 10), 21 | }); 22 | 23 | const createIfNotDefault = (maybeColumns, maybeRows) => { 24 | const {columns, rows} = create(maybeColumns, maybeRows); 25 | 26 | if (Number.isNaN(columns) || Number.isNaN(rows)) { 27 | return; 28 | } 29 | 30 | if (columns === defaultColumns && rows === defaultRows) { 31 | return; 32 | } 33 | 34 | return {columns, rows}; 35 | }; 36 | 37 | export default function terminalSize() { 38 | const {env, stdout, stderr} = process; 39 | 40 | if (stdout?.columns && stdout?.rows) { 41 | return create(stdout.columns, stdout.rows); 42 | } 43 | 44 | if (stderr?.columns && stderr?.rows) { 45 | return create(stderr.columns, stderr.rows); 46 | } 47 | 48 | // These values are static, so not the first choice. 49 | if (env.COLUMNS && env.LINES) { 50 | return create(env.COLUMNS, env.LINES); 51 | } 52 | 53 | const fallback = { 54 | columns: defaultColumns, 55 | rows: defaultRows, 56 | }; 57 | 58 | if (process.platform === 'win32') { 59 | // We include `tput` for Windows users using Git Bash. 60 | return tput() ?? fallback; 61 | } 62 | 63 | if (process.platform === 'darwin') { 64 | return devTty() ?? tput() ?? fallback; 65 | } 66 | 67 | return devTty() ?? tput() ?? resize() ?? fallback; 68 | } 69 | 70 | const devTty = () => { 71 | try { 72 | // eslint-disable-next-line no-bitwise 73 | const flags = process.platform === 'darwin' ? fs.constants.O_EVTONLY | fs.constants.O_NONBLOCK : fs.constants.O_NONBLOCK; 74 | // eslint-disable-next-line new-cap 75 | const {columns, rows} = tty.WriteStream(fs.openSync('/dev/tty', flags)); 76 | return {columns, rows}; 77 | } catch {} 78 | }; 79 | 80 | // On macOS, this only returns correct values when stdout is not redirected. 81 | const tput = () => { 82 | try { 83 | // `tput` requires the `TERM` environment variable to be set. 84 | const columns = exec('tput', ['cols'], {env: {TERM: 'dumb', ...process.env}}); 85 | const rows = exec('tput', ['lines'], {env: {TERM: 'dumb', ...process.env}}); 86 | 87 | if (columns && rows) { 88 | return createIfNotDefault(columns, rows); 89 | } 90 | } catch {} 91 | }; 92 | 93 | // Only exists on Linux. 94 | const resize = () => { 95 | // `resize` is preferred as it works even when all file descriptors are redirected 96 | // https://linux.die.net/man/1/resize 97 | try { 98 | const size = exec('resize', ['-u']).match(/\d+/g); 99 | 100 | if (size.length === 2) { 101 | return createIfNotDefault(size[0], size[1]); 102 | } 103 | } catch {} 104 | }; 105 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import terminalSize, {type TerminalSize} from './index.js'; 3 | 4 | const size: TerminalSize = terminalSize(); 5 | expectType(size.columns); 6 | expectType(size.rows); 7 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "terminal-size", 3 | "version": "4.0.0", 4 | "description": "Reliably get the terminal window size (columns & rows)", 5 | "license": "MIT", 6 | "repository": "sindresorhus/terminal-size", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": { 15 | "types": "./index.d.ts", 16 | "default": "./index.js" 17 | }, 18 | "engines": { 19 | "node": ">=18" 20 | }, 21 | "scripts": { 22 | "test": "xo && ava && tsd" 23 | }, 24 | "files": [ 25 | "index.js", 26 | "index.d.ts", 27 | "vendor" 28 | ], 29 | "keywords": [ 30 | "terminal", 31 | "size", 32 | "console", 33 | "window", 34 | "width", 35 | "height", 36 | "columns", 37 | "rows", 38 | "lines", 39 | "tty", 40 | "redirected" 41 | ], 42 | "devDependencies": { 43 | "ava": "^5.3.1", 44 | "execa": "^8.0.1", 45 | "tsd": "^0.29.0", 46 | "xo": "^0.56.0" 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # terminal-size 2 | 3 | > Reliably get the terminal window size 4 | 5 | Because [`process.stdout.columns`](https://nodejs.org/api/tty.html#tty_writestream_columns) doesn't exist when run [non-interactively](http://www.tldp.org/LDP/abs/html/intandnonint.html), for example, in a child process or when piped. This module even works when all the TTY file descriptors are redirected! 6 | 7 | Confirmed working on macOS, Linux, and Windows. 8 | 9 | ## Install 10 | 11 | ```sh 12 | npm install terminal-size 13 | ``` 14 | 15 | ## Usage 16 | 17 | ```js 18 | import terminalSize from 'terminal-size'; 19 | 20 | terminalSize(); 21 | //=> {columns: 143, rows: 24} 22 | ``` 23 | 24 | ## API 25 | 26 | ### terminalSize() 27 | 28 | Returns an `object` with `columns` and `rows` properties. 29 | 30 | ## Related 31 | 32 | - [terminal-size-cli](https://github.com/sindresorhus/terminal-size-cli) - CLI for this module 33 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import process from 'node:process'; 2 | import test from 'ava'; 3 | import {execa} from 'execa'; 4 | import terminalSize from './index.js'; 5 | 6 | test('main', t => { 7 | const size = terminalSize(); 8 | console.log('Main size:', size); 9 | t.true(size.columns > 0); 10 | t.true(size.rows > 0); 11 | }); 12 | 13 | test('child', async t => { 14 | const {stdout} = await execa('node', ['fixture.js']); 15 | const [columns, rows] = stdout.split('\n').map(line => Number.parseInt(line, 10)); 16 | console.log('Child size:', {columns, rows}); 17 | t.true(Number.parseInt(columns, 10) > 0); 18 | t.true(Number.parseInt(rows, 10) > 0); 19 | }); 20 | 21 | test('no TERM environment variable', t => { 22 | const envTerm = process.env.TERM; 23 | process.env.TERM = undefined; 24 | const size = terminalSize(); 25 | process.env.TERM = envTerm; 26 | 27 | console.log('Size with no $TERM:', size); 28 | t.true(size.columns > 0); 29 | t.true(size.rows > 0); 30 | }); 31 | --------------------------------------------------------------------------------