Code Monkey home page Code Monkey logo

window-size's Introduction

window-size NPM version NPM monthly downloads NPM total downloads Linux Build Status Windows Build Status

Reliable way to get the height and width of terminal/console, since it's not calculated or updated the same way on all platforms, environments and node.js versions.

Install

Install with npm:

$ npm install --save window-size

Usage

var size = require('window-size');

CLI

$ window-size
# height: 40
# width : 145

API

windowSize

The main export is either an object with width and height properties, or undefined if unable to get width and height.

var size = require('window-size');
console.log(size); 
//=> {width: 80, height: 25}

.get

Calls the main function to get the up-to-date window size.

console.log(size.get());
//=> {width: 80, height: 25}

Example

See example.js for the code used in the below gif.

process.stdout.on('resize', function() {
  console.log(size.get());
});

resize event example

HEADS UP!

Note that some platforms only provide the initial terminal size, not the actual size after it has been resized by the user.

.env

Get process.env.COLUMNS and process.env.ROWS, if defined. Called by the main function if for some reason size was not available from process.stdout and process.stderr.

console.log(size.env());

.tty

Attempts to get the size from tty. Called by the main function if for some reason size was not available from process.stdout, process.stderr or process.env.

console.log(size.tty());

.win

Get the terminal size in Windows 10+.

console.log(size.win());

Note that this method calls execSync to get the size, and must be called directly, as it is not called by the main function.

.tput

Get the terminal size by calling the unix $ tput command.

console.log(size.tput());

Note that this method calls execSync to get the size, and must be called directly, as it is not called by the main function.

utils

In some environments the main export may not be able to find a window size using the default methods. In this case, size will be undefined and the functions will not be exported.

Because of this, the functions have been exported in a separate file and can be required directly.

var utils = require('window-size/utils');
console.log(utils.win());

About

Related projects

  • base-cli: Plugin for base-methods that maps built-in methods to CLI args (also supports methods from a… more | homepage
  • lint-deps: CLI tool that tells you when dependencies are missing from package.json and offers you a… more | homepage
  • yargs: yargs the modern, pirate-themed, successor to optimist. | homepage

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Contributors

Commits Contributor
23 jonschlinkert
11 doowb
4 bcoe
3 icyflame
2 derhuerst
1 karliky

Building docs

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

Running tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test

Author

Jon Schlinkert

License

Copyright © 2018, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.7.0, on July 27, 2018.

window-size's People

Contributors

derhuerst avatar doowb avatar icyflame avatar jackw01 avatar jonschlinkert avatar karliky 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

window-size's Issues

work with redirected output?

I have a script which redirects its output to a file.
After that line window-size errors reading 'height'.
Is there a way to circumvent this?

npx -y window-size # works fine
# save all output to file, see https://stackoverflow.com/questions/25833676/redirect-echo-output-in-shell-script-to-logfile
exec > >(tee $data/output/scan.log)
exec 2>&1
npx -y window-size # TypeError: Cannot read properties of undefined (reading 'height')
bash -c 'npx -y window-size; exit 0' # same
/root/.npm/_npx/dbb0fbeae5ae92a5/node_modules/window-size/cli.js:16
  console.log('height: ' + size.height);
                                ^

TypeError: Cannot read properties of undefined (reading 'height')
    at showSize (/root/.npm/_npx/dbb0fbeae5ae92a5/node_modules/window-size/cli.js:16:33)
    at Object.<anonymous> (/root/.npm/_npx/dbb0fbeae5ae92a5/node_modules/window-size/cli.js:27:5)
    at Module._compile (node:internal/modules/cjs/loader:1233:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1287:10)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Module._load (node:internal/modules/cjs/loader:938:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)
    at node:internal/main/run_main_module:23:47

Node.js v20.5.1

TypeError when requiring module on cygwin

Running require('prompt-confirm') on cygwin results in below error.
This seems due to windowSize()evaluating to undefined on

module.exports = windowSize();
on cygwin.
undefined is then assigned to module.exports, and later on
define(module.exports, 'get', windowSize);
the error is thrown because it can't assign a property to undefined.

$node index.js
C:\Users\fholzer\Documents\git\prompt-test\node_modules\define-property\index.js:14
    throw new TypeError('expected an object or function.');
    ^

TypeError: expected an object or function.
    at defineProperty (C:\Users\fholzer\Documents\git\prompt-test\node_modules\define-property\index.js:14:11)
    at Object.<anonymous> (C:\Users\fholzer\Documents\git\prompt-test\node_modules\window-size\index.js:125:1)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (C:\Users\fholzer\Documents\git\prompt-test\node_modules\readline-utils\index.js:10:12)

There's two ways of starting a cygwin terminal.

  1. via the "Cygwin64 Terminal" shortcut in the start menu. When starting cygwin this way, the error occurs.
  2. via <path-to-cygwin-installation>\cygwin.bat. When starting cygwin this way, the error doesn't occur.

I'm not sure what makes the difference, but I feel that the more user-friendly way of running cygwin - using the start menu - should work.

does not work if stderr is not a TTY

console.log(require('window-size'))

Running this works fine if stderr is a TTY (not piped). If I run with node example.js ^/dev/null (fish syntax), both width and height are undefined.

afaict line 18 is the problem.

pty.js

With pty.js, the value of this library is undefined.

React to window size changes?

It would be great if this module didn't just set width and height once at initial load time, and instead noticed dynamic changes.

Thanks for your consideration.

Do not work on git bash(windows)

os: windows 7
terminal: git bash

cli.js

#!/usr/bin/env node

const size = require('window-size');

console.log(size); 
$ node cli.js
{ width: 118, height: 39, type: 'stdout' }

# but 
$ ./cli.js
undefined

Looks like your test for 1.0.0 all failed on travis

All of the tests show green, but if you look at the details they say no test was specified. When I upgraded to 1.0.0 my tests on travis failed. I rolled back and now things are fine.

The error stack I was getting on travis looked like this:

/home/travis/build/binduwavell/generator-alfresco-common/node_modules/define-property/index.js:14
    throw new TypeError('expected an object or function.');
    ^
TypeError: expected an object or function.
    at defineProperty (/home/travis/build/binduwavell/generator-alfresco-common/node_modules/define-property/index.js:14:11)
    at Object.<anonymous> (/home/travis/build/binduwavell/generator-alfresco-common/node_modules/window-size/index.js:125:1)
...

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.