MCPcopy
hub / github.com/Unitech/pm2 / cropToWidth

Function cropToWidth

lib/Common.js:524–536  ·  view source on GitHub ↗

* Crop a (possibly ANSI-colored) single line to `width` visible columns so * it does not wrap on narrow terminals. ANSI escapes are copied through * without counting toward the width, and a reset is appended after the * ellipsis so the truncation cannot leak color into following output.

(str, width)

Source from the content-addressed store, hash-verified

522 * ellipsis so the truncation cannot leak color into following output.
523 */
524function cropToWidth(str, width) {
525 var out = '', visible = 0, i = 0;
526 while (i < str.length) {
527 if (str[i] === '\x1b') {
528 var m = str.slice(i).match(/^\x1b\[[0-9;]*m/);
529 if (m) { out += m[0]; i += m[0].length; continue; }
530 }
531 if (visible >= width - 1)
532 return out + '\u2026\x1b[0m';
533 out += str[i]; visible++; i++;
534 }
535 return out;
536}
537
538Common.printOut = function() {
539 if (process.env.PM2_SILENT === 'true' || process.env.PM2_PROGRAMMATIC === 'true') return false;

Callers 1

Common.jsFile · 0.85

Calls 1

sliceMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…