Skip to content

Commit

Permalink
feat: add fallback for window width (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrimue authored and bcoe committed Dec 18, 2017
1 parent 3e3ff71 commit d064922
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ easily create complex multi-column command-line-interfaces.
## Example

```js
var ui = require('cliui')({
width: 80
})
var ui = require('cliui')()

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

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

Specify the maximum width of the UI being generated.
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`.

### cliui({wrap: boolean})

Expand Down
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,10 @@ function _minWidth (col) {
return minWidth
}

function getWindowWidth () {
if (typeof process === 'object' && process.stdout && process.stdout.columns) return process.stdout.columns
}

function alignRight (str, width) {
str = str.trim()
var padding = ''
Expand Down Expand Up @@ -310,7 +314,7 @@ module.exports = function (opts) {
opts = opts || {}

return new UI({
width: (opts || {}).width || 80,
width: (opts || {}).width || getWindowWidth() || 80,
wrap: typeof opts.wrap === 'boolean' ? opts.wrap : true
})
}

0 comments on commit d064922

Please sign in to comment.