Skip to content

Commit

Permalink
leave the string as is if the terminal is too narrow
Browse files Browse the repository at this point in the history
  • Loading branch information
shinnn committed Jul 3, 2019
1 parent b11ecfe commit 5442646
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion 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.

Expand Down
21 changes: 6 additions & 15 deletions README.md
Expand Up @@ -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
Expand Down Expand Up @@ -76,29 +76,20 @@ 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

* [neat-frame](https://github.com/shinnn/neat-frame) – works on both TTY and non-TTY environments, in exchange for larger dependency size

## License

[ISC License](./LICENSE) © 2018 - 2019 Shinnosuke Watanabe
[ISC License](./LICENSE) © 2018 - 2019 Watanabe Shinnosuke
8 changes: 7 additions & 1 deletion index.js
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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 .",
Expand Down
10 changes: 9 additions & 1 deletion test.js
Expand Up @@ -10,7 +10,7 @@ Object.defineProperties(process.stdout, {
value: true
},
columns: {
value: 10
value: 15
}
});

Expand Down Expand Up @@ -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 \(<string>\), but got no arguments\./u,
Expand Down

0 comments on commit 5442646

Please sign in to comment.