diff --git a/README.md b/README.md index ba610f8..3ad2119 100644 --- a/README.md +++ b/README.md @@ -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]') @@ -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}) diff --git a/index.js b/index.js index e501e78..55abdee9 100644 --- a/index.js +++ b/index.js @@ -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 = '' @@ -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 }) }