-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (35 loc) · 1.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'use strict';
const {inspect} = require('util');
const ansiRegex = require('ansi-regex');
const inspectWithKind = require('inspect-with-kind');
const sliceAnsi = require('slice-ansi');
const stringWidth = require('string-width');
const endRegex = new RegExp(`(?=(?:${ansiRegex().source})*$)`, 'u');
module.exports = process.stdout && process.stdout.isTTY ? function ttyTruncate(...args) {
const argLen = args.length;
if (argLen !== 1) {
throw new RangeError(`Expected 1 argument (<string>), but got ${argLen || 'no'} arguments.`);
}
const [str] = args;
if (typeof str !== 'string') {
throw new TypeError(`Expected a string to truncate to the current text terminal width, but got ${
inspectWithKind(str)
}.`);
}
if (str.includes('\n')) {
throw new Error(`tty-truncate doesn't support string with newline, but got ${inspect(str)}.`);
}
const {columns} = process.stdout;
if (columns === 0) {
return '';
}
if (stringWidth(str) <= columns) {
return str;
}
if (columns === 1) {
return sliceAnsi(str, 0, 1) || '…';
}
return sliceAnsi(str, 0, columns - 1).replace(endRegex, '…');
} : function unsupported() {
throw new Error('tty-truncate doesn\'t support non-TTY environments.');
};