Skip to content

Commit

Permalink
refactor: use process.stdout.columns instead of window-size (#737)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrimue authored and bcoe committed Dec 29, 2016
1 parent 66573c8 commit 19a897b
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
12 changes: 8 additions & 4 deletions lib/usage.js
Expand Up @@ -110,12 +110,11 @@ module.exports = function (yargs, y18n) {
}

function getWrap () {
// lazily call windowWidth() because it's very expensive,
// and only needs to be called if the user wants to show usage/help
if (!wrapSet) {
wrap = windowWidth()
wrapSet = true
}

return wrap
}

Expand Down Expand Up @@ -143,6 +142,7 @@ module.exports = function (yargs, y18n) {
return acc
}, {})
)

var theWrap = getWrap()
var ui = require('cliui')({
width: theWrap,
Expand Down Expand Up @@ -401,8 +401,12 @@ module.exports = function (yargs, y18n) {

// guess the width of the console window, max-width 80.
function windowWidth () {
const wsize = require('window-size')
return wsize.width ? Math.min(80, wsize.width) : null
var maxWidth = 80
if (typeof process === 'object' && process.stdout && process.stdout.columns) {
return Math.min(maxWidth, process.stdout.columns)
} else {
return maxWidth
}
}

// logic for displaying application version.
Expand Down
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -23,7 +23,6 @@
"set-blocking": "^2.0.0",
"string-width": "^1.0.2",
"which-module": "^1.0.0",
"window-size": "^0.2.0",
"y18n": "^3.2.1",
"yargs-parser": "^4.2.0"
},
Expand Down
2 changes: 1 addition & 1 deletion test/usage.js
Expand Up @@ -1284,7 +1284,7 @@ describe('usage tests', function () {
return this.skip()
}

var width = require('window-size').width
var width = process.stdout.columns

var r = checkUsage(function () {
return yargs([])
Expand Down
2 changes: 1 addition & 1 deletion yargs.js
Expand Up @@ -849,7 +849,7 @@ function Yargs (processArgs, cwd, parentRequire) {
}

self.terminalWidth = function () {
return require('window-size').width
return process.stdout.columns
}

Object.defineProperty(self, 'argv', {
Expand Down

0 comments on commit 19a897b

Please sign in to comment.