From 54426463f9f202d880bc63b95fe33aeb20b7f8c5 Mon Sep 17 00:00:00 2001 From: Watanabe Shinnosuke Date: Wed, 3 Jul 2019 19:41:57 +0900 Subject: [PATCH] leave the string as is if the terminal is too narrow --- LICENSE | 2 +- README.md | 21 ++++++--------------- index.js | 8 +++++++- package.json | 2 +- test.js | 10 +++++++++- 5 files changed, 24 insertions(+), 19 deletions(-) diff --git a/LICENSE b/LICENSE index 0935460..e4e77d9 100644 --- a/LICENSE +++ b/LICENSE @@ -1,5 +1,5 @@ ISC License (ISC) -Copyright 2018 - 2019 Shinnosuke Watanabe +Copyright 2018 - 2019 Watanabe Shinnosuke Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. diff --git a/README.md b/README.md index 8f4901a..10968ac 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ Generate simple framed text from a string`)); ## Installation -[Use](https://docs.npmjs.com/cli/install) [npm](https://docs.npmjs.com/getting-started/what-is-npm). +[Use](https://docs.npmjs.com/cli/install) [npm](https://docs.npmjs.com/about-npm/). ``` npm install tty-width-frame @@ -76,24 +76,15 @@ ttyWidthFrame('abcdefghijklmnopqrstuvwxyz'); */ ``` -On a [non-interactive](http://www.tldp.org/LDP/abs/html/intandnonint.html) script, it thrown an `Error` instead. +When the terminal window is too narrow, or more specifically, its width is less than 15, it just returns the original `string`. ```javascript -// cp.js -const ttyWidthFrame = require('tty-width-frame'); +// When the terminal width is 14 -ttyWidthFrame('This script throws an error.'); +ttyWidthFrame('abcdefghijklmnopqrstuvwxyz'); //=> 'abcdefghijklmnopqrstuvwxyz' ``` -```javascript -const {execFile} = require('child_process'); -const {promisify} = require('util'); - -(async () => { - const {stderr} = await promisify(execFile)('node', ['cp.js']); - // Error: tty-width-frame only supports TTY environments, but the program is running under a non-TTY environment. -})(); -``` +On a [non-interactive](https://www.tldp.org/LDP/abs/html/intandnonint.html) script, it throws an `Error` instead. ## Related project @@ -101,4 +92,4 @@ const {promisify} = require('util'); ## License -[ISC License](./LICENSE) © 2018 - 2019 Shinnosuke Watanabe +[ISC License](./LICENSE) © 2018 - 2019 Watanabe Shinnosuke diff --git a/index.js b/index.js index cdd1013..3a1dd5b 100644 --- a/index.js +++ b/index.js @@ -23,8 +23,14 @@ module.exports = process.stdout && process.stdout.isTTY ? function ttyWidthFrame }.`); } + const {columns} = process.stdout; + + if (columns < 15) { + return str; + } + // ' ┌'.length + '┐ '.length === 6 - const contentWidth = process.stdout.columns - 6; + const contentWidth = columns - 6; const padding = ` │${' '.repeat(contentWidth)}│`; const verticalBar = '─'.repeat(contentWidth); diff --git a/package.json b/package.json index 8ea07d5..87635bd 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "1.0.2", "description": "Generate simple framed text fitting for the current text terminal", "repository": "shinnn/tty-width-frame", - "author": "Shinnosuke Watanabe (https://github.com/shinnn)", + "author": "Watanabe Shinnosuke (https://github.com/shinnn)", "license": "ISC", "scripts": { "pretest": "eslint .", diff --git a/test.js b/test.js index 0c31e52..2be5dfa 100644 --- a/test.js +++ b/test.js @@ -10,7 +10,7 @@ Object.defineProperties(process.stdout, { value: true }, columns: { - value: 10 + value: 15 } }); @@ -75,6 +75,14 @@ test('ttyWidthFrame()', async t => { await unlink(tmp); + Object.defineProperty(process.stdout, 'columns', {value: 14}); + + t.equal( + ttyWidthFrame('a'.repeat(100)), + 'a'.repeat(100), + 'should starts with an upper frame.' + ); + t.throws( () => ttyWidthFrame(), /^RangeError.*Expected 1 argument \(\), but got no arguments\./u,