Last change
on this file since eed0bf8 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1011 bytes
|
Rev | Line | |
---|
[6a3a178] | 1 | // Minimal replacement for ansi string helpers "wrap-ansi" and "strip-ansi".
|
---|
| 2 | // to facilitate ESM and Deno modules.
|
---|
| 3 | // TODO: look at porting https://www.npmjs.com/package/wrap-ansi to ESM.
|
---|
| 4 | // The npm application
|
---|
| 5 | // Copyright (c) npm, Inc. and Contributors
|
---|
| 6 | // Licensed on the terms of The Artistic License 2.0
|
---|
| 7 | // See: https://github.com/npm/cli/blob/4c65cd952bc8627811735bea76b9b110cc4fc80e/lib/utils/ansi-trim.js
|
---|
| 8 | const ansi = new RegExp('\x1b(?:\\[(?:\\d+[ABCDEFGJKSTm]|\\d+;\\d+[Hfm]|' +
|
---|
| 9 | '\\d+;\\d+;\\d+m|6n|s|u|\\?25[lh])|\\w)', 'g');
|
---|
| 10 | export function stripAnsi(str) {
|
---|
| 11 | return str.replace(ansi, '');
|
---|
| 12 | }
|
---|
| 13 | export function wrap(str, width) {
|
---|
| 14 | const [start, end] = str.match(ansi) || ['', ''];
|
---|
| 15 | str = stripAnsi(str);
|
---|
| 16 | let wrapped = '';
|
---|
| 17 | for (let i = 0; i < str.length; i++) {
|
---|
| 18 | if (i !== 0 && (i % width) === 0) {
|
---|
| 19 | wrapped += '\n';
|
---|
| 20 | }
|
---|
| 21 | wrapped += str.charAt(i);
|
---|
| 22 | }
|
---|
| 23 | if (start && end) {
|
---|
| 24 | wrapped = `${start}${wrapped}${end}`;
|
---|
| 25 | }
|
---|
| 26 | return wrapped;
|
---|
| 27 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.