Skip to content

Commit d064922

Browse files
maxrimuebcoe
authored andcommittedDec 18, 2017
feat: add fallback for window width (#45)
1 parent 3e3ff71 commit d064922

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed
 

‎README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ easily create complex multi-column command-line-interfaces.
1010
## Example
1111

1212
```js
13-
var ui = require('cliui')({
14-
width: 80
15-
})
13+
var ui = require('cliui')()
1614

1715
ui.div('Usage: $0 [command] [options]')
1816

@@ -88,6 +86,7 @@ cliui = require('cliui')
8886
### cliui({width: integer})
8987

9088
Specify the maximum width of the UI being generated.
89+
If no width is provided, cliui will try to get the current window's width and use it, and if that doesn't work, width will be set to `80`.
9190

9291
### cliui({wrap: boolean})
9392

‎index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,10 @@ function _minWidth (col) {
282282
return minWidth
283283
}
284284

285+
function getWindowWidth () {
286+
if (typeof process === 'object' && process.stdout && process.stdout.columns) return process.stdout.columns
287+
}
288+
285289
function alignRight (str, width) {
286290
str = str.trim()
287291
var padding = ''
@@ -310,7 +314,7 @@ module.exports = function (opts) {
310314
opts = opts || {}
311315

312316
return new UI({
313-
width: (opts || {}).width || 80,
317+
width: (opts || {}).width || getWindowWidth() || 80,
314318
wrap: typeof opts.wrap === 'boolean' ? opts.wrap : true
315319
})
316320
}

0 commit comments

Comments
 (0)
Please sign in to comment.