@rushstack/terminal 0.5.38 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/terminal.d.ts +47 -2
- package/dist/tsdoc-metadata.json +1 -1
- package/lib/PrintUtilities.d.ts +47 -2
- package/lib/PrintUtilities.d.ts.map +1 -1
- package/lib/PrintUtilities.js +62 -27
- package/lib/PrintUtilities.js.map +1 -1
- package/package.json +6 -8
package/dist/terminal.d.ts
CHANGED
|
@@ -318,10 +318,55 @@ export declare class PrintUtilities {
|
|
|
318
318
|
*/
|
|
319
319
|
static getConsoleWidth(): number | undefined;
|
|
320
320
|
/**
|
|
321
|
-
* Applies word wrapping.
|
|
322
|
-
*
|
|
321
|
+
* Applies word wrapping.
|
|
322
|
+
*
|
|
323
|
+
* @param text - The text to wrap
|
|
324
|
+
* @param maxLineLength - The maximum length of a line, defaults to the console width
|
|
325
|
+
* @param indent - The number of spaces to indent the wrapped lines, defaults to 0
|
|
323
326
|
*/
|
|
324
327
|
static wrapWords(text: string, maxLineLength?: number, indent?: number): string;
|
|
328
|
+
/**
|
|
329
|
+
* Applies word wrapping.
|
|
330
|
+
*
|
|
331
|
+
* @param text - The text to wrap
|
|
332
|
+
* @param maxLineLength - The maximum length of a line, defaults to the console width
|
|
333
|
+
* @param linePrefix - The string to prefix each line with, defaults to ''
|
|
334
|
+
*/
|
|
335
|
+
static wrapWords(text: string, maxLineLength?: number, linePrefix?: string): string;
|
|
336
|
+
/**
|
|
337
|
+
* Applies word wrapping.
|
|
338
|
+
*
|
|
339
|
+
* @param text - The text to wrap
|
|
340
|
+
* @param maxLineLength - The maximum length of a line, defaults to the console width
|
|
341
|
+
* @param indentOrLinePrefix - The number of spaces to indent the wrapped lines or the string to prefix
|
|
342
|
+
* each line with, defaults to no prefix
|
|
343
|
+
*/
|
|
344
|
+
static wrapWords(text: string, maxLineLength?: number, indentOrLinePrefix?: number | string): string;
|
|
345
|
+
/**
|
|
346
|
+
* Applies word wrapping and returns an array of lines.
|
|
347
|
+
*
|
|
348
|
+
* @param text - The text to wrap
|
|
349
|
+
* @param maxLineLength - The maximum length of a line, defaults to the console width
|
|
350
|
+
* @param indent - The number of spaces to indent the wrapped lines, defaults to 0
|
|
351
|
+
*/
|
|
352
|
+
static wrapWordsToLines(text: string, maxLineLength?: number, indent?: number): string[];
|
|
353
|
+
/**
|
|
354
|
+
* Applies word wrapping and returns an array of lines.
|
|
355
|
+
*
|
|
356
|
+
* @param text - The text to wrap
|
|
357
|
+
* @param maxLineLength - The maximum length of a line, defaults to the console width
|
|
358
|
+
* @param linePrefix - The string to prefix each line with, defaults to ''
|
|
359
|
+
*/
|
|
360
|
+
static wrapWordsToLines(text: string, maxLineLength?: number, linePrefix?: string): string[];
|
|
361
|
+
/**
|
|
362
|
+
* Applies word wrapping and returns an array of lines.
|
|
363
|
+
*
|
|
364
|
+
* @param text - The text to wrap
|
|
365
|
+
* @param maxLineLength - The maximum length of a line, defaults to the console width
|
|
366
|
+
* @param indentOrLinePrefix - The number of spaces to indent the wrapped lines or the string to prefix
|
|
367
|
+
* each line with, defaults to no prefix
|
|
368
|
+
*/
|
|
369
|
+
static wrapWordsToLines(text: string, maxLineLength?: number, indentOrLinePrefix?: number | string): string[];
|
|
325
370
|
/**
|
|
326
371
|
* Displays a message in the console wrapped in a box UI.
|
|
327
372
|
*
|
package/dist/tsdoc-metadata.json
CHANGED
package/lib/PrintUtilities.d.ts
CHANGED
|
@@ -16,10 +16,55 @@ export declare class PrintUtilities {
|
|
|
16
16
|
*/
|
|
17
17
|
static getConsoleWidth(): number | undefined;
|
|
18
18
|
/**
|
|
19
|
-
* Applies word wrapping.
|
|
20
|
-
*
|
|
19
|
+
* Applies word wrapping.
|
|
20
|
+
*
|
|
21
|
+
* @param text - The text to wrap
|
|
22
|
+
* @param maxLineLength - The maximum length of a line, defaults to the console width
|
|
23
|
+
* @param indent - The number of spaces to indent the wrapped lines, defaults to 0
|
|
21
24
|
*/
|
|
22
25
|
static wrapWords(text: string, maxLineLength?: number, indent?: number): string;
|
|
26
|
+
/**
|
|
27
|
+
* Applies word wrapping.
|
|
28
|
+
*
|
|
29
|
+
* @param text - The text to wrap
|
|
30
|
+
* @param maxLineLength - The maximum length of a line, defaults to the console width
|
|
31
|
+
* @param linePrefix - The string to prefix each line with, defaults to ''
|
|
32
|
+
*/
|
|
33
|
+
static wrapWords(text: string, maxLineLength?: number, linePrefix?: string): string;
|
|
34
|
+
/**
|
|
35
|
+
* Applies word wrapping.
|
|
36
|
+
*
|
|
37
|
+
* @param text - The text to wrap
|
|
38
|
+
* @param maxLineLength - The maximum length of a line, defaults to the console width
|
|
39
|
+
* @param indentOrLinePrefix - The number of spaces to indent the wrapped lines or the string to prefix
|
|
40
|
+
* each line with, defaults to no prefix
|
|
41
|
+
*/
|
|
42
|
+
static wrapWords(text: string, maxLineLength?: number, indentOrLinePrefix?: number | string): string;
|
|
43
|
+
/**
|
|
44
|
+
* Applies word wrapping and returns an array of lines.
|
|
45
|
+
*
|
|
46
|
+
* @param text - The text to wrap
|
|
47
|
+
* @param maxLineLength - The maximum length of a line, defaults to the console width
|
|
48
|
+
* @param indent - The number of spaces to indent the wrapped lines, defaults to 0
|
|
49
|
+
*/
|
|
50
|
+
static wrapWordsToLines(text: string, maxLineLength?: number, indent?: number): string[];
|
|
51
|
+
/**
|
|
52
|
+
* Applies word wrapping and returns an array of lines.
|
|
53
|
+
*
|
|
54
|
+
* @param text - The text to wrap
|
|
55
|
+
* @param maxLineLength - The maximum length of a line, defaults to the console width
|
|
56
|
+
* @param linePrefix - The string to prefix each line with, defaults to ''
|
|
57
|
+
*/
|
|
58
|
+
static wrapWordsToLines(text: string, maxLineLength?: number, linePrefix?: string): string[];
|
|
59
|
+
/**
|
|
60
|
+
* Applies word wrapping and returns an array of lines.
|
|
61
|
+
*
|
|
62
|
+
* @param text - The text to wrap
|
|
63
|
+
* @param maxLineLength - The maximum length of a line, defaults to the console width
|
|
64
|
+
* @param indentOrLinePrefix - The number of spaces to indent the wrapped lines or the string to prefix
|
|
65
|
+
* each line with, defaults to no prefix
|
|
66
|
+
*/
|
|
67
|
+
static wrapWordsToLines(text: string, maxLineLength?: number, indentOrLinePrefix?: number | string): string[];
|
|
23
68
|
/**
|
|
24
69
|
* Displays a message in the console wrapped in a box UI.
|
|
25
70
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PrintUtilities.d.ts","sourceRoot":"","sources":["../src/PrintUtilities.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"PrintUtilities.d.ts","sourceRoot":"","sources":["../src/PrintUtilities.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;AAEzD;;;;GAIG;AACH,eAAO,MAAM,qBAAqB,EAAE,MAAW,CAAC;AAEhD;;;;GAIG;AACH,qBAAa,cAAc;IACzB;;OAEG;WACW,eAAe,IAAI,MAAM,GAAG,SAAS;IAInD;;;;;;OAMG;WACW,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,aAAa,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM;IACtF;;;;;;OAMG;WACW,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,aAAa,CAAC,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,MAAM;IAC1F;;;;;;;OAOG;WACW,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,aAAa,CAAC,EAAE,MAAM,EAAE,kBAAkB,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM;IAc3G;;;;;;OAMG;WACW,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,aAAa,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE;IAC/F;;;;;;OAMG;WACW,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,aAAa,CAAC,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE;IACnG;;;;;;;OAOG;WACW,gBAAgB,CAC5B,IAAI,EAAE,MAAM,EACZ,aAAa,CAAC,EAAE,MAAM,EACtB,kBAAkB,CAAC,EAAE,MAAM,GAAG,MAAM,GACnC,MAAM,EAAE;IA4EX;;;;OAIG;WACW,iBAAiB,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,SAAS,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI;CAqB/F"}
|
package/lib/PrintUtilities.js
CHANGED
|
@@ -1,12 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
|
3
3
|
// See LICENSE in the project root for license information.
|
|
4
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
5
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
6
|
-
};
|
|
7
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
5
|
exports.PrintUtilities = exports.DEFAULT_CONSOLE_WIDTH = void 0;
|
|
9
|
-
const wordwrap_1 = __importDefault(require("wordwrap"));
|
|
10
6
|
/**
|
|
11
7
|
* A sensible fallback column width for consoles.
|
|
12
8
|
*
|
|
@@ -23,35 +19,75 @@ class PrintUtilities {
|
|
|
23
19
|
* Returns the width of the console, measured in columns
|
|
24
20
|
*/
|
|
25
21
|
static getConsoleWidth() {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
return stdout.columns;
|
|
29
|
-
}
|
|
22
|
+
var _a;
|
|
23
|
+
return (_a = process.stdout) === null || _a === void 0 ? void 0 : _a.columns;
|
|
30
24
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
25
|
+
static wrapWords(text, maxLineLength, indentOrLinePrefix) {
|
|
26
|
+
const wrappedLines = PrintUtilities.wrapWordsToLines(text, maxLineLength, indentOrLinePrefix // TS is confused by the overloads
|
|
27
|
+
);
|
|
28
|
+
return wrappedLines.join('\n');
|
|
29
|
+
}
|
|
30
|
+
static wrapWordsToLines(text, maxLineLength, indentOrLinePrefix) {
|
|
31
|
+
var _a;
|
|
32
|
+
let linePrefix;
|
|
33
|
+
switch (typeof indentOrLinePrefix) {
|
|
34
|
+
case 'number':
|
|
35
|
+
linePrefix = ' '.repeat(indentOrLinePrefix);
|
|
36
|
+
break;
|
|
37
|
+
case 'string':
|
|
38
|
+
linePrefix = indentOrLinePrefix;
|
|
39
|
+
break;
|
|
40
|
+
default:
|
|
41
|
+
linePrefix = '';
|
|
42
|
+
break;
|
|
38
43
|
}
|
|
44
|
+
const linePrefixLength = linePrefix.length;
|
|
39
45
|
if (!maxLineLength) {
|
|
40
46
|
maxLineLength = PrintUtilities.getConsoleWidth() || exports.DEFAULT_CONSOLE_WIDTH;
|
|
41
47
|
}
|
|
42
|
-
// Apply word wrapping and the provided
|
|
48
|
+
// Apply word wrapping and the provided line prefix, while also respecting existing newlines
|
|
43
49
|
// and prefix spaces that may exist in the text string already.
|
|
44
50
|
const lines = text.split(/\r?\n/);
|
|
45
|
-
const wrappedLines =
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
if (addlIndent > 0) {
|
|
50
|
-
line = line.replace(/^ +/, '');
|
|
51
|
+
const wrappedLines = [];
|
|
52
|
+
for (const line of lines) {
|
|
53
|
+
if (line.length + linePrefixLength <= maxLineLength) {
|
|
54
|
+
wrappedLines.push(linePrefix + line);
|
|
51
55
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
56
|
+
else {
|
|
57
|
+
const lineAdditionalPrefix = ((_a = line.match(/^\s*/)) === null || _a === void 0 ? void 0 : _a[0]) || '';
|
|
58
|
+
const whitespaceRegexp = /\s+/g;
|
|
59
|
+
let currentWhitespaceMatch = null;
|
|
60
|
+
let previousWhitespaceMatch;
|
|
61
|
+
let currentLineStartIndex = lineAdditionalPrefix.length;
|
|
62
|
+
let previousBreakRanOver = false;
|
|
63
|
+
while ((currentWhitespaceMatch = whitespaceRegexp.exec(line)) !== null) {
|
|
64
|
+
if (currentWhitespaceMatch.index + linePrefixLength - currentLineStartIndex > maxLineLength) {
|
|
65
|
+
let whitespaceToSplitAt;
|
|
66
|
+
if (!previousWhitespaceMatch ||
|
|
67
|
+
// Handle the case where there are two words longer than the maxLineLength in a row
|
|
68
|
+
previousBreakRanOver) {
|
|
69
|
+
whitespaceToSplitAt = currentWhitespaceMatch;
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
whitespaceToSplitAt = previousWhitespaceMatch;
|
|
73
|
+
}
|
|
74
|
+
wrappedLines.push(linePrefix +
|
|
75
|
+
lineAdditionalPrefix +
|
|
76
|
+
line.substring(currentLineStartIndex, whitespaceToSplitAt.index));
|
|
77
|
+
previousBreakRanOver = whitespaceToSplitAt.index - currentLineStartIndex > maxLineLength;
|
|
78
|
+
currentLineStartIndex = whitespaceToSplitAt.index + whitespaceToSplitAt[0].length;
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
previousBreakRanOver = false;
|
|
82
|
+
}
|
|
83
|
+
previousWhitespaceMatch = currentWhitespaceMatch;
|
|
84
|
+
}
|
|
85
|
+
if (currentLineStartIndex < line.length) {
|
|
86
|
+
wrappedLines.push(linePrefix + lineAdditionalPrefix + line.substring(currentLineStartIndex));
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return wrappedLines;
|
|
55
91
|
}
|
|
56
92
|
/**
|
|
57
93
|
* Displays a message in the console wrapped in a box UI.
|
|
@@ -64,8 +100,7 @@ class PrintUtilities {
|
|
|
64
100
|
boxWidth = Math.floor(consoleWidth / 2);
|
|
65
101
|
}
|
|
66
102
|
const maxLineLength = boxWidth - 10;
|
|
67
|
-
const
|
|
68
|
-
const wrappedMessageLines = wrappedMessage.split('\n');
|
|
103
|
+
const wrappedMessageLines = PrintUtilities.wrapWordsToLines(message, maxLineLength);
|
|
69
104
|
// ╔═══════════╗
|
|
70
105
|
// ║ Message ║
|
|
71
106
|
// ╚═══════════╝
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PrintUtilities.js","sourceRoot":"","sources":["../src/PrintUtilities.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;;;;AAG3D,wDAAgC;AAGhC;;;;GAIG;AACU,QAAA,qBAAqB,GAAW,EAAE,CAAC;AAEhD;;;;GAIG;AACH,MAAa,cAAc;IACzB;;OAEG;IACI,MAAM,CAAC,eAAe;QAC3B,MAAM,MAAM,GAAoB,OAAO,CAAC,MAAyB,CAAC;QAClE,IAAI,MAAM,IAAI,MAAM,CAAC,OAAO,EAAE;YAC5B,OAAO,MAAM,CAAC,OAAO,CAAC;SACvB;IACH,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,SAAS,CAAC,IAAY,EAAE,aAAsB,EAAE,MAAe;QAC3E,IAAI,CAAC,MAAM,EAAE;YACX,MAAM,GAAG,CAAC,CAAC;SACZ;QAED,IAAI,CAAC,aAAa,EAAE;YAClB,aAAa,GAAG,cAAc,CAAC,eAAe,EAAE,IAAI,6BAAqB,CAAC;SAC3E;QAED,uFAAuF;QACvF,+DAA+D;QAC/D,MAAM,KAAK,GAAa,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QAC5C,MAAM,YAAY,GAAa,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE;;YAChD,MAAM,aAAa,GAA4B,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;YACjE,MAAM,UAAU,GAAW,CAAA,MAAA,aAAa,aAAb,aAAa,uBAAb,aAAa,CAAG,CAAC,CAAC,0CAAE,MAAM,KAAI,CAAC,CAAC;YAE3D,IAAI,UAAU,GAAG,CAAC,EAAE;gBAClB,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;aAChC;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G,GAAG,CAAC,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;SAC/F;QACD,QAAQ,CAAC,SAAS,CAAC,KAAK,GAAG,CAAC,MAAM,CAAC,QAAQ,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC;IACxD,CAAC;CACF;AApED,wCAoEC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport * as tty from 'tty';\nimport wordwrap from 'wordwrap';\nimport { ITerminal } from '@rushstack/node-core-library';\n\n/**\n * A sensible fallback column width for consoles.\n *\n * @public\n */\nexport const DEFAULT_CONSOLE_WIDTH: number = 80;\n\n/**\n * A collection of utilities for printing messages to the console.\n *\n * @public\n */\nexport class PrintUtilities {\n /**\n * Returns the width of the console, measured in columns\n */\n public static getConsoleWidth(): number | undefined {\n const stdout: tty.WriteStream = process.stdout as tty.WriteStream;\n if (stdout && stdout.columns) {\n return stdout.columns;\n }\n }\n\n /**\n * Applies word wrapping. If maxLineLength is unspecified, then it defaults to the\n * console width.\n */\n public static wrapWords(text: string, maxLineLength?: number, indent?: number): string {\n if (!indent) {\n indent = 0;\n }\n\n if (!maxLineLength) {\n maxLineLength = PrintUtilities.getConsoleWidth() || DEFAULT_CONSOLE_WIDTH;\n }\n\n // Apply word wrapping and the provided indent, while also respecting existing newlines\n // and prefix spaces that may exist in the text string already.\n const lines: string[] = text.split(/\\r?\\n/);\n const wrappedLines: string[] = lines.map((line) => {\n const startingSpace: RegExpMatchArray | null = line.match(/^ +/);\n const addlIndent: number = startingSpace?.[0]?.length || 0;\n\n if (addlIndent > 0) {\n line = line.replace(/^ +/, '');\n }\n\n return wordwrap(indent! + addlIndent, maxLineLength! - indent! - addlIndent, { mode: 'soft' })(line);\n });\n\n return wrappedLines.join('\\n');\n }\n\n /**\n * Displays a message in the console wrapped in a box UI.\n *\n * @param boxWidth - The width of the box, defaults to half of the console width.\n */\n public static printMessageInBox(message: string, terminal: ITerminal, boxWidth?: number): void {\n if (!boxWidth) {\n const consoleWidth: number = PrintUtilities.getConsoleWidth() || DEFAULT_CONSOLE_WIDTH;\n boxWidth = Math.floor(consoleWidth / 2);\n }\n const maxLineLength: number = boxWidth - 10;\n const wrappedMessage: string = PrintUtilities.wrapWords(message, maxLineLength);\n const wrappedMessageLines: string[] = wrappedMessage.split('\\n');\n\n // ╔═══════════╗\n // ║ Message ║\n // ╚═══════════╝\n terminal.writeLine(` ╔${'═'.repeat(boxWidth - 2)}╗ `);\n for (const line of wrappedMessageLines) {\n const trimmedLine: string = line.trim();\n const padding: number = boxWidth - trimmedLine.length - 2;\n const leftPadding: number = Math.floor(padding / 2);\n const rightPadding: number = padding - leftPadding;\n terminal.writeLine(` ║${' '.repeat(leftPadding)}${trimmedLine}${' '.repeat(rightPadding)}║ `);\n }\n terminal.writeLine(` ╚${'═'.repeat(boxWidth - 2)}╝ `);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"PrintUtilities.js","sourceRoot":"","sources":["../src/PrintUtilities.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAI3D;;;;GAIG;AACU,QAAA,qBAAqB,GAAW,EAAE,CAAC;AAEhD;;;;GAIG;AACH,MAAa,cAAc;IACzB;;OAEG;IACI,MAAM,CAAC,eAAe;;QAC3B,OAAO,MAAA,OAAO,CAAC,MAAM,0CAAE,OAAO,CAAC;IACjC,CAAC;IA2BM,MAAM,CAAC,SAAS,CACrB,IAAY,EACZ,aAAsB,EACtB,kBAAoC;QAEpC,MAAM,YAAY,GAAa,cAAc,CAAC,gBAAgB,CAC5D,IAAI,EACJ,aAAa,EACb,kBAAwC,CAAC,kCAAkC;SAC5E,CAAC;QACF,OAAO,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACjC,CAAC;IA+BM,MAAM,CAAC,gBAAgB,CAC5B,IAAY,EACZ,aAAsB,EACtB,kBAAoC;;QAEpC,IAAI,UAAkB,CAAC;QACvB,QAAQ,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AC,MAAM,CAAC,QAAQ,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC;QACtD,KAAK,MAAM,IAAI,IAAI,mBAAmB,EAAE;YACtC,MAAM,WAAW,GAAW,IAAI,CAAC,IAAI,EAAE,CAAC;YACxC,MAAM,OAAO,GAAW,QAAQ,GAAG,WAAW,CAAC,MAAM,GAAG,CAAC,CAAC;YAC1D,MAAM,WAAW,GAAW,IAAI,CAAC,KAAK,CAAC,OAAO,GAAG,CAAC,CAAC,CAAC;YACpD,MAAM,YAAY,GAAW,OAAO,GAAG,WAAW,CAAC;YACnD,QAAQ,CAAC,SAAS,CAAC,KAAK,GAAG,CAAC,MAAM,CAAC,WAAW,CAAC,GAAG,WAAW,GAAG,GAAG,CAAC,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;SAC/F;QACD,QAAQ,CAAC,SAAS,CAAC,KAAK,GAAG,CAAC,MAAM,CAAC,QAAQ,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC;IACxD,CAAC;CACF;AAhLD,wCAgLC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport { ITerminal } from '@rushstack/node-core-library';\n\n/**\n * A sensible fallback column width for consoles.\n *\n * @public\n */\nexport const DEFAULT_CONSOLE_WIDTH: number = 80;\n\n/**\n * A collection of utilities for printing messages to the console.\n *\n * @public\n */\nexport class PrintUtilities {\n /**\n * Returns the width of the console, measured in columns\n */\n public static getConsoleWidth(): number | undefined {\n return process.stdout?.columns;\n }\n\n /**\n * Applies word wrapping.\n *\n * @param text - The text to wrap\n * @param maxLineLength - The maximum length of a line, defaults to the console width\n * @param indent - The number of spaces to indent the wrapped lines, defaults to 0\n */\n public static wrapWords(text: string, maxLineLength?: number, indent?: number): string;\n /**\n * Applies word wrapping.\n *\n * @param text - The text to wrap\n * @param maxLineLength - The maximum length of a line, defaults to the console width\n * @param linePrefix - The string to prefix each line with, defaults to ''\n */\n public static wrapWords(text: string, maxLineLength?: number, linePrefix?: string): string;\n /**\n * Applies word wrapping.\n *\n * @param text - The text to wrap\n * @param maxLineLength - The maximum length of a line, defaults to the console width\n * @param indentOrLinePrefix - The number of spaces to indent the wrapped lines or the string to prefix\n * each line with, defaults to no prefix\n */\n public static wrapWords(text: string, maxLineLength?: number, indentOrLinePrefix?: number | string): string;\n public static wrapWords(\n text: string,\n maxLineLength?: number,\n indentOrLinePrefix?: number | string\n ): string {\n const wrappedLines: string[] = PrintUtilities.wrapWordsToLines(\n text,\n maxLineLength,\n indentOrLinePrefix as string | undefined // TS is confused by the overloads\n );\n return wrappedLines.join('\\n');\n }\n\n /**\n * Applies word wrapping and returns an array of lines.\n *\n * @param text - The text to wrap\n * @param maxLineLength - The maximum length of a line, defaults to the console width\n * @param indent - The number of spaces to indent the wrapped lines, defaults to 0\n */\n public static wrapWordsToLines(text: string, maxLineLength?: number, indent?: number): string[];\n /**\n * Applies word wrapping and returns an array of lines.\n *\n * @param text - The text to wrap\n * @param maxLineLength - The maximum length of a line, defaults to the console width\n * @param linePrefix - The string to prefix each line with, defaults to ''\n */\n public static wrapWordsToLines(text: string, maxLineLength?: number, linePrefix?: string): string[];\n /**\n * Applies word wrapping and returns an array of lines.\n *\n * @param text - The text to wrap\n * @param maxLineLength - The maximum length of a line, defaults to the console width\n * @param indentOrLinePrefix - The number of spaces to indent the wrapped lines or the string to prefix\n * each line with, defaults to no prefix\n */\n public static wrapWordsToLines(\n text: string,\n maxLineLength?: number,\n indentOrLinePrefix?: number | string\n ): string[];\n public static wrapWordsToLines(\n text: string,\n maxLineLength?: number,\n indentOrLinePrefix?: number | string\n ): string[] {\n let linePrefix: string;\n switch (typeof indentOrLinePrefix) {\n case 'number':\n linePrefix = ' '.repeat(indentOrLinePrefix);\n break;\n case 'string':\n linePrefix = indentOrLinePrefix;\n break;\n default:\n linePrefix = '';\n break;\n }\n\n const linePrefixLength: number = linePrefix.length;\n\n if (!maxLineLength) {\n maxLineLength = PrintUtilities.getConsoleWidth() || DEFAULT_CONSOLE_WIDTH;\n }\n\n // Apply word wrapping and the provided line prefix, while also respecting existing newlines\n // and prefix spaces that may exist in the text string already.\n const lines: string[] = text.split(/\\r?\\n/);\n\n const wrappedLines: string[] = [];\n for (const line of lines) {\n if (line.length + linePrefixLength <= maxLineLength) {\n wrappedLines.push(linePrefix + line);\n } else {\n const lineAdditionalPrefix: string = line.match(/^\\s*/)?.[0] || '';\n const whitespaceRegexp: RegExp = /\\s+/g;\n let currentWhitespaceMatch: RegExpExecArray | null = null;\n let previousWhitespaceMatch: RegExpExecArray | undefined;\n let currentLineStartIndex: number = lineAdditionalPrefix.length;\n let previousBreakRanOver: boolean = false;\n while ((currentWhitespaceMatch = whitespaceRegexp.exec(line)) !== null) {\n if (currentWhitespaceMatch.index + linePrefixLength - currentLineStartIndex > maxLineLength) {\n let whitespaceToSplitAt: RegExpExecArray | undefined;\n if (\n !previousWhitespaceMatch ||\n // Handle the case where there are two words longer than the maxLineLength in a row\n previousBreakRanOver\n ) {\n whitespaceToSplitAt = currentWhitespaceMatch;\n } else {\n whitespaceToSplitAt = previousWhitespaceMatch;\n }\n\n wrappedLines.push(\n linePrefix +\n lineAdditionalPrefix +\n line.substring(currentLineStartIndex, whitespaceToSplitAt.index)\n );\n previousBreakRanOver = whitespaceToSplitAt.index - currentLineStartIndex > maxLineLength;\n currentLineStartIndex = whitespaceToSplitAt.index + whitespaceToSplitAt[0].length;\n } else {\n previousBreakRanOver = false;\n }\n\n previousWhitespaceMatch = currentWhitespaceMatch;\n }\n\n if (currentLineStartIndex < line.length) {\n wrappedLines.push(linePrefix + lineAdditionalPrefix + line.substring(currentLineStartIndex));\n }\n }\n }\n\n return wrappedLines;\n }\n\n /**\n * Displays a message in the console wrapped in a box UI.\n *\n * @param boxWidth - The width of the box, defaults to half of the console width.\n */\n public static printMessageInBox(message: string, terminal: ITerminal, boxWidth?: number): void {\n if (!boxWidth) {\n const consoleWidth: number = PrintUtilities.getConsoleWidth() || DEFAULT_CONSOLE_WIDTH;\n boxWidth = Math.floor(consoleWidth / 2);\n }\n const maxLineLength: number = boxWidth - 10;\n const wrappedMessageLines: string[] = PrintUtilities.wrapWordsToLines(message, maxLineLength);\n\n // ╔═══════════╗\n // ║ Message ║\n // ╚═══════════╝\n terminal.writeLine(` ╔${'═'.repeat(boxWidth - 2)}╗ `);\n for (const line of wrappedMessageLines) {\n const trimmedLine: string = line.trim();\n const padding: number = boxWidth - trimmedLine.length - 2;\n const leftPadding: number = Math.floor(padding / 2);\n const rightPadding: number = padding - leftPadding;\n terminal.writeLine(` ║${' '.repeat(leftPadding)}${trimmedLine}${' '.repeat(rightPadding)}║ `);\n }\n terminal.writeLine(` ╚${'═'.repeat(boxWidth - 2)}╝ `);\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rushstack/terminal",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "User interface primitives for console applications",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"typings": "dist/terminal.d.ts",
|
|
@@ -11,17 +11,15 @@
|
|
|
11
11
|
"directory": "libraries/terminal"
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"
|
|
15
|
-
"@rushstack/node-core-library": "3.59.7"
|
|
14
|
+
"@rushstack/node-core-library": "3.60.0"
|
|
16
15
|
},
|
|
17
16
|
"devDependencies": {
|
|
18
17
|
"@types/heft-jest": "1.0.1",
|
|
19
|
-
"@types/node": "
|
|
20
|
-
"@types/wordwrap": "~1.0.0",
|
|
18
|
+
"@types/node": "18.17.15",
|
|
21
19
|
"colors": "~1.2.1",
|
|
22
|
-
"@rushstack/eslint-config": "3.3.
|
|
23
|
-
"@rushstack/heft": "0.
|
|
24
|
-
"@rushstack/heft-node-rig": "2.2.
|
|
20
|
+
"@rushstack/eslint-config": "3.3.4",
|
|
21
|
+
"@rushstack/heft": "0.59.0",
|
|
22
|
+
"@rushstack/heft-node-rig": "2.2.23"
|
|
25
23
|
},
|
|
26
24
|
"peerDependencies": {
|
|
27
25
|
"@types/node": "*"
|