1 | # window-size [![NPM version](https://img.shields.io/npm/v/window-size.svg?style=flat)](https://www.npmjs.com/package/window-size) [![NPM monthly downloads](https://img.shields.io/npm/dm/window-size.svg?style=flat)](https://npmjs.org/package/window-size) [![NPM total downloads](https://img.shields.io/npm/dt/window-size.svg?style=flat)](https://npmjs.org/package/window-size) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/window-size.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/window-size) [![Windows Build Status](https://img.shields.io/appveyor/ci/jonschlinkert/window-size.svg?style=flat&label=AppVeyor)](https://ci.appveyor.com/project/jonschlinkert/window-size)
|
2 |
|
3 | > 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.
|
4 |
|
5 | ## Install
|
6 |
|
7 | Install with [npm](https://www.npmjs.com/):
|
8 |
|
9 | ```sh
|
10 | $ npm install --save window-size
|
11 | ```
|
12 |
|
13 | ## Usage
|
14 |
|
15 | ```js
|
16 | var size = require('window-size');
|
17 | ```
|
18 |
|
19 | ## CLI
|
20 |
|
21 | ```sh
|
22 | $ window-size
|
23 | # height: 40
|
24 | # width : 145
|
25 | ```
|
26 |
|
27 | ## API
|
28 |
|
29 | ### windowSize
|
30 |
|
31 | The main export is either an object with `width` and `height` properties, or `undefined` if unable to get width and height.
|
32 |
|
33 | ```js
|
34 | var size = require('window-size');
|
35 | console.log(size);
|
36 | //=> {width: 80, height: 25}
|
37 | ```
|
38 |
|
39 | ### .get
|
40 |
|
41 | Calls the main function to get the up-to-date window size.
|
42 |
|
43 | ```js
|
44 | console.log(size.get());
|
45 | //=> {width: 80, height: 25}
|
46 | ```
|
47 |
|
48 | **Example**
|
49 |
|
50 | See [example.js](example.js) for the code used in the below gif.
|
51 |
|
52 | ```js
|
53 | process.stdout.on('resize', function() {
|
54 | console.log(size.get());
|
55 | });
|
56 | ```
|
57 |
|
58 | ![resize event example](https://github.com/jonschlinkert/window-size/blob/master/resize.gif)
|
59 |
|
60 | **HEADS UP!**
|
61 |
|
62 | Note that some platforms only provide the initial terminal size, not the actual size after it has been resized by the user.
|
63 |
|
64 | ### .env
|
65 |
|
66 | 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`.
|
67 |
|
68 | ```js
|
69 | console.log(size.env());
|
70 | ```
|
71 |
|
72 | ### .tty
|
73 |
|
74 | 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`.
|
75 |
|
76 | ```js
|
77 | console.log(size.tty());
|
78 | ```
|
79 |
|
80 | ### .win
|
81 |
|
82 | Get the terminal size in Windows 10+.
|
83 |
|
84 | ```js
|
85 | console.log(size.win());
|
86 | ```
|
87 |
|
88 | Note that this method calls [execSync](https://nodejs.org/api/child_process.html#child_process_child_process_execsync_command_options) to get the size, and must be called directly, as it **is not** called by the main function.
|
89 |
|
90 | ### .tput
|
91 |
|
92 | Get the terminal size by calling the unix `$ tput` command.
|
93 |
|
94 | ```js
|
95 | console.log(size.tput());
|
96 | ```
|
97 |
|
98 | Note that this method calls [execSync](https://nodejs.org/api/child_process.html#child_process_child_process_execsync_command_options) to get the size, and must be called directly, as it **is not** called by the main function.
|
99 |
|
100 | ### utils
|
101 |
|
102 | 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.
|
103 |
|
104 | Because of this, the functions have been exported in a separate file and can be required directly.
|
105 |
|
106 | ```js
|
107 | var utils = require('window-size/utils');
|
108 | console.log(utils.win());
|
109 | ```
|
110 |
|
111 | ## About
|
112 |
|
113 | ### Related projects
|
114 |
|
115 | * [base-cli](https://www.npmjs.com/package/base-cli): Plugin for base-methods that maps built-in methods to CLI args (also supports methods from a… [more](https://github.com/node-base/base-cli) | [homepage](https://github.com/node-base/base-cli "Plugin for base-methods that maps built-in methods to CLI args (also supports methods from a few plugins, like 'base-store', 'base-options' and 'base-data'.")
|
116 | * [lint-deps](https://www.npmjs.com/package/lint-deps): CLI tool that tells you when dependencies are missing from package.json and offers you a… [more](https://github.com/jonschlinkert/lint-deps) | [homepage](https://github.com/jonschlinkert/lint-deps "CLI tool that tells you when dependencies are missing from package.json and offers you a choice to install them. Also tells you when dependencies are listed in package.json but are not being used anywhere in your project. Node.js command line tool and API")
|
117 | * [yargs](https://www.npmjs.com/package/yargs): yargs the modern, pirate-themed, successor to optimist. | [homepage](http://yargs.js.org/ "yargs the modern, pirate-themed, successor to optimist.")
|
118 |
|
119 | ### Contributing
|
120 |
|
121 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
|
122 |
|
123 | ### Contributors
|
124 |
|
125 | | **Commits** | **Contributor** |
|
126 | | --- | --- |
|
127 | | 23 | [jonschlinkert](https://github.com/jonschlinkert) |
|
128 | | 11 | [doowb](https://github.com/doowb) |
|
129 | | 4 | [bcoe](https://github.com/bcoe) |
|
130 | | 3 | [icyflame](https://github.com/icyflame) |
|
131 | | 2 | [derhuerst](https://github.com/derhuerst) |
|
132 | | 1 | [karliky](https://github.com/karliky) |
|
133 |
|
134 | ### Building docs
|
135 |
|
136 | _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
|
137 |
|
138 | To generate the readme, run the following command:
|
139 |
|
140 | ```sh
|
141 | $ npm install -g verbose/verb#dev verb-generate-readme && verb
|
142 | ```
|
143 |
|
144 | ### Running tests
|
145 |
|
146 | 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:
|
147 |
|
148 | ```sh
|
149 | $ npm install && npm test
|
150 | ```
|
151 |
|
152 | ### Author
|
153 |
|
154 | **Jon Schlinkert**
|
155 |
|
156 | * [github/jonschlinkert](https://github.com/jonschlinkert)
|
157 | * [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
|
158 |
|
159 | ### License
|
160 |
|
161 | Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).
|
162 | Released under the [MIT License](LICENSE).
|
163 |
|
164 | ***
|
165 |
|
166 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.7.0, on July 27, 2018._ |
\ | No newline at end of file |