Skip to content
This repository was archived by the owner on Mar 8, 2023. It is now read-only.

Commit 996ce61

Browse files
committed
init
0 parents  commit 996ce61

File tree

9 files changed

+188
-0
lines changed

9 files changed

+188
-0
lines changed

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*]
4+
indent_style = tab
5+
end_of_line = lf
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true
9+
10+
[{package.json,*.yml}]
11+
indent_style = space
12+
indent_size = 2

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: node_js
2+
node_js:
3+
- '5'
4+
- '4'
5+
- '0.12'
6+
- '0.10'

index.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
var wrapAnsi = require('wrap-ansi');
3+
var stringWidth = require('string-width');
4+
5+
module.exports = function (input, columns) {
6+
var ellipsis = '…';
7+
8+
if (typeof input !== 'string') {
9+
throw new TypeError('Expected `input` to be a string, got ' + typeof input);
10+
}
11+
12+
if (typeof columns !== 'number') {
13+
throw new TypeError('Expected `columns` to be a number, got ' + typeof columns);
14+
}
15+
16+
if (columns < 1) {
17+
return '';
18+
}
19+
20+
if (columns === 1) {
21+
return ellipsis;
22+
}
23+
24+
if (stringWidth(input) <= columns) {
25+
return input;
26+
}
27+
28+
return wrapAnsi(input, columns - 1, {hard: true}).split('\n')[0] + ellipsis;
29+
};

license

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

package.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"name": "cli-truncate",
3+
"version": "0.0.0",
4+
"description": "Truncate a string to a specific width in the terminal",
5+
"license": "MIT",
6+
"repository": "sindresorhus/cli-truncate",
7+
"author": {
8+
"name": "Sindre Sorhus",
9+
"email": "[email protected]",
10+
"url": "sindresorhus.com"
11+
},
12+
"engines": {
13+
"node": ">=0.10.0"
14+
},
15+
"scripts": {
16+
"test": "xo && ava"
17+
},
18+
"files": [
19+
"index.js"
20+
],
21+
"keywords": [
22+
"truncate",
23+
"ellipsis",
24+
"text",
25+
"limit",
26+
"slice",
27+
"cli",
28+
"terminal",
29+
"term",
30+
"shell",
31+
"width",
32+
"ansi"
33+
],
34+
"dependencies": {
35+
"string-width": "^1.0.1",
36+
"wrap-ansi": "^2.0.0"
37+
},
38+
"devDependencies": {
39+
"ava": "*",
40+
"xo": "*"
41+
}
42+
}

readme.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# cli-truncate [![Build Status](https://travis-ci.org/sindresorhus/cli-truncate.svg?branch=master)](https://travis-ci.org/sindresorhus/cli-truncate)
2+
3+
> Truncate a string to a specific width in the terminal
4+
5+
Gracefully handles [ANSI escapes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles). Like a string styled with [`chalk`](https://github.com/chalk/chalk).
6+
7+
8+
## Install
9+
10+
```
11+
$ npm install --save cli-truncate
12+
```
13+
14+
15+
## Usage
16+
17+
```js
18+
const cliTruncate = require('cli-truncate');
19+
20+
cliTruncate('unicorn', 4);
21+
//=> 'uni…'
22+
23+
cliTruncate('\u001b[31municorn\u001b[39m', 4);
24+
//=> '\u001b[31muni\u001b[39m…'
25+
26+
// truncate the paragraph to the terminal width
27+
const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.';
28+
cliTruncate(paragraph, process.stdout.columns));
29+
//=> 'Lorem ipsum dolor sit amet, consectetuer adipiscing…'
30+
```
31+
32+
33+
## API
34+
35+
### cliTruncate(input, columns)
36+
37+
#### input
38+
39+
Type: `string`
40+
41+
Text to truncate.
42+
43+
#### columns
44+
45+
Type: `number`
46+
47+
Columns to occupy in the terminal.
48+
49+
50+
## Related
51+
52+
- [wrap-ansi](https://github.com/chalk/wrap-ansi) - Wordwrap a string with ANSI escape codes
53+
- [slice-ansi](https://github.com/chalk/slice-ansi) - Slice a string with ANSI escape codes
54+
55+
56+
## License
57+
58+
MIT © [Sindre Sorhus](https://sindresorhus.com)

test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import test from 'ava';
2+
import m from './';
3+
4+
test(t => {
5+
t.is(m('unicorn', 4), 'uni…');
6+
t.is(m('unicorn', 1), '…');
7+
t.is(m('unicorn', 0), '');
8+
t.is(m('unicorn', -4), '');
9+
t.is(m('unicorn', 20), 'unicorn');
10+
t.is(m('unicorn', 7), 'unicorn');
11+
t.is(m('unicorn', 6), 'unico…');
12+
t.is(m('\u001b[31municorn\u001b[39m', 7), '\u001b[31municorn\u001b[39m');
13+
t.is(m('\u001b[31municorn\u001b[39m', 1), '…');
14+
t.is(m('\u001b[31municorn\u001b[39m', 4), '\u001b[31muni\u001b[39m…');
15+
// TODO
16+
t.skip.is(m('a\ud83c\ude00b\ud83c\ude00c', 5), 'a\ud83c\ude00b…', 'surrogate pairs');
17+
t.skip.is(m('안녕하세요', 3), '안…', 'wide char');
18+
});

0 commit comments

Comments
 (0)