├── .editorconfig ├── .eslintrc.json ├── .gitignore ├── .travis.yml ├── .verb.md ├── LICENSE ├── README.md ├── appveyor.yml ├── cli.js ├── example.js ├── index.js ├── package.json ├── resize.gif ├── test.js └── utils.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | end_of_line = lf 6 | charset = utf-8 7 | indent_size = 2 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [{**/{actual,fixtures,expected,templates}/**,*.md}] 12 | trim_trailing_whitespace = false 13 | insert_final_newline = false -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "ecmaFeatures": { 3 | "modules": true, 4 | "experimentalObjectRestSpread": true 5 | }, 6 | 7 | "env": { 8 | "browser": false, 9 | "es6": true, 10 | "node": true, 11 | "mocha": true 12 | }, 13 | 14 | "globals": { 15 | "document": false, 16 | "navigator": false, 17 | "window": false 18 | }, 19 | 20 | "rules": { 21 | "accessor-pairs": 2, 22 | "arrow-spacing": [2, { "before": true, "after": true }], 23 | "block-spacing": [2, "always"], 24 | "brace-style": [2, "1tbs", { "allowSingleLine": true }], 25 | "comma-dangle": [2, "never"], 26 | "comma-spacing": [2, { "before": false, "after": true }], 27 | "comma-style": [2, "last"], 28 | "constructor-super": 2, 29 | "curly": [2, "multi-line"], 30 | "dot-location": [2, "property"], 31 | "eol-last": 2, 32 | "eqeqeq": [2, "allow-null"], 33 | "generator-star-spacing": [2, { "before": true, "after": true }], 34 | "handle-callback-err": [2, "^(err|error)$" ], 35 | "indent": [2, 2, { "SwitchCase": 1 }], 36 | "key-spacing": [2, { "beforeColon": false, "afterColon": true }], 37 | "keyword-spacing": [2, { "before": true, "after": true }], 38 | "new-cap": [2, { "newIsCap": true, "capIsNew": false }], 39 | "new-parens": 2, 40 | "no-array-constructor": 2, 41 | "no-caller": 2, 42 | "no-class-assign": 2, 43 | "no-cond-assign": 2, 44 | "no-const-assign": 2, 45 | "no-control-regex": 2, 46 | "no-debugger": 2, 47 | "no-delete-var": 2, 48 | "no-dupe-args": 2, 49 | "no-dupe-class-members": 2, 50 | "no-dupe-keys": 2, 51 | "no-duplicate-case": 2, 52 | "no-empty-character-class": 2, 53 | "no-eval": 2, 54 | "no-ex-assign": 2, 55 | "no-extend-native": 2, 56 | "no-extra-bind": 2, 57 | "no-extra-boolean-cast": 2, 58 | "no-extra-parens": [2, "functions"], 59 | "no-fallthrough": 2, 60 | "no-floating-decimal": 2, 61 | "no-func-assign": 2, 62 | "no-implied-eval": 2, 63 | "no-inner-declarations": [2, "functions"], 64 | "no-invalid-regexp": 2, 65 | "no-irregular-whitespace": 2, 66 | "no-iterator": 2, 67 | "no-label-var": 2, 68 | "no-labels": 2, 69 | "no-lone-blocks": 2, 70 | "no-mixed-spaces-and-tabs": 2, 71 | "no-multi-spaces": 2, 72 | "no-multi-str": 2, 73 | "no-multiple-empty-lines": [2, { "max": 1 }], 74 | "no-native-reassign": 0, 75 | "no-negated-in-lhs": 2, 76 | "no-new": 2, 77 | "no-new-func": 2, 78 | "no-new-object": 2, 79 | "no-new-require": 2, 80 | "no-new-wrappers": 2, 81 | "no-obj-calls": 2, 82 | "no-octal": 2, 83 | "no-octal-escape": 2, 84 | "no-proto": 0, 85 | "no-redeclare": 2, 86 | "no-regex-spaces": 2, 87 | "no-return-assign": 2, 88 | "no-self-compare": 2, 89 | "no-sequences": 2, 90 | "no-shadow-restricted-names": 2, 91 | "no-spaced-func": 2, 92 | "no-sparse-arrays": 2, 93 | "no-this-before-super": 2, 94 | "no-throw-literal": 2, 95 | "no-trailing-spaces": 0, 96 | "no-undef": 2, 97 | "no-undef-init": 2, 98 | "no-unexpected-multiline": 2, 99 | "no-unneeded-ternary": [2, { "defaultAssignment": false }], 100 | "no-unreachable": 2, 101 | "no-unused-vars": [2, { "vars": "all", "args": "none" }], 102 | "no-useless-call": 0, 103 | "no-with": 2, 104 | "one-var": [0, { "initialized": "never" }], 105 | "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }], 106 | "padded-blocks": [0, "never"], 107 | "quotes": [2, "single", "avoid-escape"], 108 | "radix": 2, 109 | "semi": [2, "always"], 110 | "semi-spacing": [2, { "before": false, "after": true }], 111 | "space-before-blocks": [2, "always"], 112 | "space-before-function-paren": [2, "never"], 113 | "space-in-parens": [2, "never"], 114 | "space-infix-ops": 2, 115 | "space-unary-ops": [2, { "words": true, "nonwords": false }], 116 | "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }], 117 | "use-isnan": 2, 118 | "valid-typeof": 2, 119 | "wrap-iife": [2, "any"], 120 | "yoda": [2, "never"] 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # always ignore files 2 | *.DS_Store 3 | *.sublime-* 4 | 5 | # test related, or directories generated by tests 6 | test/actual 7 | actual 8 | coverage 9 | .nyc* 10 | 11 | # npm 12 | node_modules 13 | npm-debug.log 14 | 15 | # yarn 16 | yarn.lock 17 | yarn-error.log 18 | 19 | # misc 20 | _gh_pages 21 | _draft 22 | _drafts 23 | bower_components 24 | vendor 25 | temp 26 | tmp 27 | TODO.md 28 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | os: 3 | - linux 4 | - osx 5 | language: node_js 6 | node_js: 7 | - node 8 | - '7' 9 | - '6' 10 | - '5' 11 | - '4' 12 | - '0.12' 13 | - '0.10' 14 | -------------------------------------------------------------------------------- /.verb.md: -------------------------------------------------------------------------------- 1 | ## Usage 2 | 3 | ```js 4 | var size = require('{%= name %}'); 5 | ``` 6 | 7 | ## CLI 8 | 9 | ```sh 10 | $ window-size 11 | # height: 40 12 | # width : 145 13 | ``` 14 | 15 | ## API 16 | 17 | ### windowSize 18 | 19 | The main export is either an object with `width` and `height` properties, or `undefined` if unable to get width and height. 20 | 21 | ```js 22 | var size = require('{%= name %}'); 23 | console.log(size); 24 | //=> {width: 80, height: 25} 25 | ``` 26 | 27 | ### .get 28 | 29 | Calls the main function to get the up-to-date window size. 30 | 31 | ```js 32 | console.log(size.get()); 33 | //=> {width: 80, height: 25} 34 | ``` 35 | 36 | **Example** 37 | 38 | See [example.js](example.js) for the code used in the below gif. 39 | 40 | ```js 41 | process.stdout.on('resize', function() { 42 | console.log(size.get()); 43 | }); 44 | ``` 45 | 46 | ![resize event example]({%= contentLink("resize.gif") %}) 47 | 48 | **HEADS UP!** 49 | 50 | Note that some platforms only provide the initial terminal size, not the actual size after it has been resized by the user. 51 | 52 | ### .env 53 | 54 | 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`. 55 | 56 | ```js 57 | console.log(size.env()); 58 | ``` 59 | 60 | ### .tty 61 | 62 | 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`. 63 | 64 | ```js 65 | console.log(size.tty()); 66 | ``` 67 | 68 | ### .win 69 | 70 | Get the terminal size in Windows 10+. 71 | 72 | ```js 73 | console.log(size.win()); 74 | ``` 75 | 76 | Note that this method calls [execSync][] to get the size, and must be called directly, as it **is not** called by the main function. 77 | 78 | 79 | ### .tput 80 | 81 | Get the terminal size by calling the unix `$ tput` command. 82 | 83 | ```js 84 | console.log(size.tput()); 85 | ``` 86 | 87 | Note that this method calls [execSync][] to get the size, and must be called directly, as it **is not** called by the main function. 88 | 89 | ### utils 90 | 91 | 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. 92 | 93 | Because of this, the functions have been exported in a separate file and can be required directly. 94 | 95 | ```js 96 | var utils = require('window-size/utils'); 97 | console.log(utils.win()); 98 | ``` 99 | 100 | [execSync]: https://nodejs.org/api/child_process.html#child_process_child_process_execsync_command_options 101 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015-2017, Jon Schlinkert 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 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._ -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | # Test against this version of Node.js 2 | environment: 3 | matrix: 4 | # node.js 5 | - nodejs_version: "8.0" 6 | - nodejs_version: "7.0" 7 | - nodejs_version: "6.0" 8 | - nodejs_version: "5.0" 9 | - nodejs_version: "4.0" 10 | - nodejs_version: "0.12" 11 | - nodejs_version: "0.10" 12 | 13 | # Install scripts. (runs after repo cloning) 14 | install: 15 | # Get the latest stable version of Node.js or io.js 16 | - ps: Install-Product node $env:nodejs_version 17 | # install modules 18 | - npm install 19 | 20 | # Post-install test scripts. 21 | test_script: 22 | # Output useful info for debugging. 23 | - node --version 24 | - npm --version 25 | # run tests 26 | - npm test 27 | 28 | # Don't actually build. 29 | build: off 30 | -------------------------------------------------------------------------------- /cli.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var size = require('./'); 4 | var helpText = [ 5 | 'Usage', 6 | ' $ window-size', 7 | '', 8 | 'Example', 9 | ' $ window-size', 10 | ' height: 40 ', 11 | ' width : 145', 12 | '' 13 | ].join('\n'); 14 | 15 | function showSize() { 16 | console.log('height: ' + size.height); 17 | console.log('width : ' + size.width); 18 | } 19 | 20 | switch (process.argv[2]) { 21 | case 'help': 22 | case '--help': 23 | case '-h': 24 | console.log(helpText); 25 | break; 26 | default: { 27 | showSize(); 28 | break; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | var size = require('./'); 2 | var wrap = require('justified'); 3 | 4 | var text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.'; 5 | 6 | function opts(margin) { 7 | process.stdout.write('\x1bc'); 8 | var width = size.get().width - (margin * 3); 9 | return {indent: margin, width: width}; 10 | } 11 | 12 | console.log(wrap(text, opts(12))); 13 | 14 | process.stdout.on('resize', function() { 15 | console.log(wrap(text, opts(12))); 16 | }); 17 | 18 | setInterval(function() { 19 | }, 0); 20 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * window-size 3 | * 4 | * Copyright (c) 2014-2017, Jon Schlinkert. 5 | * Released under the MIT License. 6 | */ 7 | 8 | 'use strict'; 9 | 10 | var define = require('define-property'); 11 | var utils = require('./utils'); 12 | 13 | /** 14 | * Expose `windowSize` 15 | */ 16 | 17 | module.exports = utils.get(); 18 | 19 | if (module.exports) { 20 | /** 21 | * Expose `windowSize.get` method 22 | */ 23 | 24 | define(module.exports, 'get', utils.get); 25 | 26 | /** 27 | * Expose methods for unit tests 28 | */ 29 | 30 | define(module.exports, 'env', utils.env); 31 | define(module.exports, 'tty', utils.tty); 32 | define(module.exports, 'tput', utils.tput); 33 | define(module.exports, 'win', utils.win); 34 | } 35 | 36 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "window-size", 3 | "description": "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 | "version": "1.1.1", 5 | "homepage": "https://github.com/jonschlinkert/window-size", 6 | "author": "Jon Schlinkert (https://github.com/jonschlinkert)", 7 | "contributors": [ 8 | "Benjamin E. Coe (https://twitter.com/benjamincoe)", 9 | "Brian Woodward (https://twitter.com/doowb)", 10 | "Carlos Hernández Gómez (http://www.twitter.com/k4rliky)", 11 | "Jannis Redmann (http://jannisr.de)", 12 | "Jon Schlinkert (http://twitter.com/jonschlinkert)", 13 | "Siddharth Kannan (http://icyflame.github.io)" 14 | ], 15 | "repository": "jonschlinkert/window-size", 16 | "bugs": { 17 | "url": "https://github.com/jonschlinkert/window-size/issues" 18 | }, 19 | "license": "MIT", 20 | "files": [ 21 | "cli.js", 22 | "index.js", 23 | "utils.js" 24 | ], 25 | "main": "index.js", 26 | "bin": "cli.js", 27 | "engines": { 28 | "node": ">= 0.10.0" 29 | }, 30 | "scripts": { 31 | "test": "mocha" 32 | }, 33 | "dependencies": { 34 | "define-property": "^1.0.0", 35 | "is-number": "^3.0.0" 36 | }, 37 | "devDependencies": { 38 | "gulp-format-md": "^0.1.12", 39 | "justified": "^0.2.1", 40 | "mocha": "^3.3.0" 41 | }, 42 | "keywords": [ 43 | "columns", 44 | "console", 45 | "darwin", 46 | "height", 47 | "lines", 48 | "redirected", 49 | "resize", 50 | "rows", 51 | "size", 52 | "term", 53 | "term-size", 54 | "terminal", 55 | "tty", 56 | "unix", 57 | "width", 58 | "win", 59 | "window", 60 | "windows" 61 | ], 62 | "verb": { 63 | "related": { 64 | "list": [ 65 | "base-cli", 66 | "lint-deps", 67 | "yargs" 68 | ] 69 | }, 70 | "toc": false, 71 | "layout": "default", 72 | "tasks": [ 73 | "readme" 74 | ], 75 | "plugins": [ 76 | "gulp-format-md" 77 | ], 78 | "lint": { 79 | "reflinks": true 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /resize.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jonschlinkert/window-size/28f3e0ffba18846d5db4919a65804aa076e4ffc5/resize.gif -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | require('mocha'); 4 | var isCI = process.env.TRAVIS || process.env.APPVEYOR || process.env.CI; 5 | var assert = require('assert'); 6 | var utils = require('./utils'); 7 | var size = require('./'); 8 | var tty = require('tty'); 9 | 10 | function override(obj) { 11 | var getWindowSize = obj.getWindowSize; 12 | var columns = obj.columns; 13 | var rows = obj.rows; 14 | 15 | obj.getWindowSize = null; 16 | obj.columns = null; 17 | obj.rows = null; 18 | 19 | return function() { 20 | obj.getWindowSize = getWindowSize; 21 | obj.columns = columns; 22 | obj.rows = rows; 23 | }; 24 | } 25 | 26 | describe('window-size', function() { 27 | it('should return an object with width and height', function() { 28 | if (!process.env.APPVEYOR) { 29 | assert.equal(typeof size.width, 'number'); 30 | assert.equal(typeof size.height, 'number'); 31 | } 32 | }); 33 | 34 | it('should expose a `.get` method to get up-to-date size', function() { 35 | if (!process.env.APPVEYOR) { 36 | var s = size.get(); 37 | assert.equal(typeof s.width, 'number'); 38 | assert.equal(typeof s.height, 'number'); 39 | } 40 | }); 41 | 42 | it('should get size from process.stdout', function() { 43 | if (!process.env.APPVEYOR) { 44 | var count = 0; 45 | var restore = override(process.stdout); 46 | process.stdout.getWindowSize = function() { 47 | count++; 48 | }; 49 | 50 | size.get(); 51 | restore(); 52 | assert.equal(count, 1); 53 | } 54 | }); 55 | 56 | it('should get size from process.stderr', function() { 57 | if (!process.env.APPVEYOR) { 58 | var restoreOut = override(process.stdout); 59 | var restoreErr = override(process.stderr); 60 | 61 | var count = 0; 62 | process.stderr.getWindowSize = function() { 63 | count++; 64 | }; 65 | 66 | size.get(); 67 | restoreOut(); 68 | restoreErr(); 69 | assert.equal(count, 1); 70 | } 71 | }); 72 | 73 | it('should get size from process.env', function() { 74 | if (!process.env.APPVEYOR) { 75 | process.env.COLUMNS = 80; 76 | process.env.ROWS = 25; 77 | 78 | var s = size.env(); 79 | assert(s); 80 | assert.equal(s.width, 80); 81 | assert.equal(s.height, 25); 82 | 83 | process.env.COLUMNS = null; 84 | process.env.ROWS = null; 85 | } 86 | }); 87 | 88 | it('should get size from tty', function() { 89 | if (!process.env.APPVEYOR) { 90 | var restoreOut = override(process.stdout); 91 | var restoreErr = override(process.stderr); 92 | var restoreTty = override(tty); 93 | 94 | var count = 0; 95 | tty.getWindowSize = function() { 96 | count++; 97 | }; 98 | 99 | size.get(); 100 | restoreOut(); 101 | restoreErr(); 102 | restoreTty(); 103 | assert.equal(count, 1); 104 | } 105 | }); 106 | 107 | it('should get size from tput', function() { 108 | if (!isCI) { 109 | var s = size.tput(); 110 | assert(s); 111 | assert.equal(typeof s.width, 'number'); 112 | assert.equal(typeof s.height, 'number'); 113 | } 114 | }); 115 | 116 | describe('utils', function() { 117 | it('should expose a `.get` method to get up-to-date size', function() { 118 | var s = utils.get(); 119 | if (process.env.APPVEYOR) { 120 | assert.equal(typeof s, 'undefined'); 121 | } else { 122 | assert.equal(typeof s.width, 'number'); 123 | assert.equal(typeof s.height, 'number'); 124 | } 125 | }); 126 | 127 | it('should get size from process.env', function() { 128 | process.env.COLUMNS = 80; 129 | process.env.ROWS = 25; 130 | 131 | var s = utils.env(); 132 | assert(s); 133 | assert.equal(s.width, 80); 134 | assert.equal(s.height, 25); 135 | 136 | process.env.COLUMNS = null; 137 | process.env.ROWS = null; 138 | }); 139 | 140 | it('should get size from tty', function() { 141 | var restoreTty = override(tty); 142 | 143 | var count = 0; 144 | tty.getWindowSize = function() { 145 | count++; 146 | }; 147 | 148 | utils.tty({}); 149 | restoreTty(); 150 | assert.equal(count, 1); 151 | }); 152 | 153 | it('should get size from tput', function() { 154 | if (!isCI) { 155 | var s = utils.tput(); 156 | assert(s); 157 | assert.equal(typeof s.width, 'number'); 158 | assert.equal(typeof s.height, 'number'); 159 | } 160 | }); 161 | }); 162 | }); 163 | 164 | -------------------------------------------------------------------------------- /utils.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * window-size 3 | * 4 | * Copyright (c) 2014-2017, Jon Schlinkert. 5 | * Released under the MIT License. 6 | */ 7 | 8 | 'use strict'; 9 | 10 | var os = require('os'); 11 | var isNumber = require('is-number'); 12 | var cp = require('child_process'); 13 | 14 | function windowSize(options) { 15 | options = options || {}; 16 | return streamSize(options, 'stdout') || 17 | streamSize(options, 'stderr') || 18 | envSize() || 19 | ttySize(options); 20 | } 21 | 22 | function streamSize(options, name) { 23 | var stream = (process && process[name]) || options[name]; 24 | var size; 25 | 26 | if (!stream) return; 27 | if (typeof stream.getWindowSize === 'function') { 28 | size = stream.getWindowSize(); 29 | if (isSize(size)) { 30 | return { 31 | width: size[0], 32 | height: size[1], 33 | type: name 34 | }; 35 | } 36 | } 37 | 38 | size = [stream.columns, stream.rows]; 39 | if (isSize(size)) { 40 | return { 41 | width: Number(size[0]), 42 | height: Number(size[1]), 43 | type: name 44 | }; 45 | } 46 | } 47 | 48 | function envSize() { 49 | if (process && process.env) { 50 | var size = [process.env.COLUMNS, process.env.ROWS]; 51 | if (isSize(size)) { 52 | return { 53 | width: Number(size[0]), 54 | height: Number(size[1]), 55 | type: 'process.env' 56 | }; 57 | } 58 | } 59 | } 60 | 61 | function ttySize(options, stdout) { 62 | var tty = options.tty || require('tty'); 63 | if (tty && typeof tty.getWindowSize === 'function') { 64 | var size = tty.getWindowSize(stdout); 65 | if (isSize(size)) { 66 | return { 67 | width: Number(size[1]), 68 | height: Number(size[0]), 69 | type: 'tty' 70 | }; 71 | } 72 | } 73 | } 74 | 75 | function winSize() { 76 | if (os.release().startsWith('10')) { 77 | var cmd = 'wmic path Win32_VideoController get CurrentHorizontalResolution,CurrentVerticalResolution'; 78 | var numberPattern = /\d+/g; 79 | var code = cp.execSync(cmd).toString(); 80 | var size = code.match(numberPattern); 81 | if (isSize(size)) { 82 | return { 83 | width: Number(size[0]), 84 | height: Number(size[1]), 85 | type: 'windows' 86 | }; 87 | } 88 | } 89 | } 90 | 91 | function tputSize() { 92 | try { 93 | var buf = cp.execSync('tput cols && tput lines', {stdio: ['ignore', 'pipe', process.stderr]}); 94 | var size = buf.toString().trim().split('\n'); 95 | if (isSize(size)) { 96 | return { 97 | width: Number(size[0]), 98 | height: Number(size[1]), 99 | type: 'tput' 100 | }; 101 | } 102 | } catch (err) {} 103 | } 104 | 105 | /** 106 | * Returns true if the given size array has 107 | * valid height and width values. 108 | */ 109 | 110 | function isSize(size) { 111 | return Array.isArray(size) && isNumber(size[0]) && isNumber(size[1]); 112 | } 113 | 114 | /** 115 | * Expose `windowSize` 116 | */ 117 | 118 | module.exports = { 119 | get: windowSize, 120 | env: envSize, 121 | tty: ttySize, 122 | tput: tputSize, 123 | win: winSize 124 | }; 125 | --------------------------------------------------------------------------------