No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

string-utils.js 1011B

123456789101112131415161718192021222324252627
  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. }