@rushstack/terminal
Advanced tools
Comparing version 0.6.0 to 0.7.0
@@ -321,7 +321,52 @@ /** | ||
/** | ||
* Applies word wrapping. If maxLineLength is unspecified, then it defaults to the | ||
* console width. | ||
* Applies word wrapping. | ||
* | ||
* @param text - The text to wrap | ||
* @param maxLineLength - The maximum length of a line, defaults to the console width | ||
* @param indent - The number of spaces to indent the wrapped lines, defaults to 0 | ||
*/ | ||
static wrapWords(text: string, maxLineLength?: number, indent?: number): string; | ||
/** | ||
* Applies word wrapping. | ||
* | ||
* @param text - The text to wrap | ||
* @param maxLineLength - The maximum length of a line, defaults to the console width | ||
* @param linePrefix - The string to prefix each line with, defaults to '' | ||
*/ | ||
static wrapWords(text: string, maxLineLength?: number, linePrefix?: string): string; | ||
/** | ||
* Applies word wrapping. | ||
* | ||
* @param text - The text to wrap | ||
* @param maxLineLength - The maximum length of a line, defaults to the console width | ||
* @param indentOrLinePrefix - The number of spaces to indent the wrapped lines or the string to prefix | ||
* each line with, defaults to no prefix | ||
*/ | ||
static wrapWords(text: string, maxLineLength?: number, indentOrLinePrefix?: number | string): string; | ||
/** | ||
* Applies word wrapping and returns an array of lines. | ||
* | ||
* @param text - The text to wrap | ||
* @param maxLineLength - The maximum length of a line, defaults to the console width | ||
* @param indent - The number of spaces to indent the wrapped lines, defaults to 0 | ||
*/ | ||
static wrapWordsToLines(text: string, maxLineLength?: number, indent?: number): string[]; | ||
/** | ||
* Applies word wrapping and returns an array of lines. | ||
* | ||
* @param text - The text to wrap | ||
* @param maxLineLength - The maximum length of a line, defaults to the console width | ||
* @param linePrefix - The string to prefix each line with, defaults to '' | ||
*/ | ||
static wrapWordsToLines(text: string, maxLineLength?: number, linePrefix?: string): string[]; | ||
/** | ||
* Applies word wrapping and returns an array of lines. | ||
* | ||
* @param text - The text to wrap | ||
* @param maxLineLength - The maximum length of a line, defaults to the console width | ||
* @param indentOrLinePrefix - The number of spaces to indent the wrapped lines or the string to prefix | ||
* each line with, defaults to no prefix | ||
*/ | ||
static wrapWordsToLines(text: string, maxLineLength?: number, indentOrLinePrefix?: number | string): string[]; | ||
/** | ||
* Displays a message in the console wrapped in a box UI. | ||
@@ -328,0 +373,0 @@ * |
@@ -8,5 +8,5 @@ // This file is read by tools that parse documentation comments conforming to the TSDoc standard. | ||
"packageName": "@microsoft/api-extractor", | ||
"packageVersion": "7.36.4" | ||
"packageVersion": "7.37.0" | ||
} | ||
] | ||
} |
@@ -19,7 +19,52 @@ import { ITerminal } from '@rushstack/node-core-library'; | ||
/** | ||
* Applies word wrapping. If maxLineLength is unspecified, then it defaults to the | ||
* console width. | ||
* Applies word wrapping. | ||
* | ||
* @param text - The text to wrap | ||
* @param maxLineLength - The maximum length of a line, defaults to the console width | ||
* @param indent - The number of spaces to indent the wrapped lines, defaults to 0 | ||
*/ | ||
static wrapWords(text: string, maxLineLength?: number, indent?: number): string; | ||
/** | ||
* Applies word wrapping. | ||
* | ||
* @param text - The text to wrap | ||
* @param maxLineLength - The maximum length of a line, defaults to the console width | ||
* @param linePrefix - The string to prefix each line with, defaults to '' | ||
*/ | ||
static wrapWords(text: string, maxLineLength?: number, linePrefix?: string): string; | ||
/** | ||
* Applies word wrapping. | ||
* | ||
* @param text - The text to wrap | ||
* @param maxLineLength - The maximum length of a line, defaults to the console width | ||
* @param indentOrLinePrefix - The number of spaces to indent the wrapped lines or the string to prefix | ||
* each line with, defaults to no prefix | ||
*/ | ||
static wrapWords(text: string, maxLineLength?: number, indentOrLinePrefix?: number | string): string; | ||
/** | ||
* Applies word wrapping and returns an array of lines. | ||
* | ||
* @param text - The text to wrap | ||
* @param maxLineLength - The maximum length of a line, defaults to the console width | ||
* @param indent - The number of spaces to indent the wrapped lines, defaults to 0 | ||
*/ | ||
static wrapWordsToLines(text: string, maxLineLength?: number, indent?: number): string[]; | ||
/** | ||
* Applies word wrapping and returns an array of lines. | ||
* | ||
* @param text - The text to wrap | ||
* @param maxLineLength - The maximum length of a line, defaults to the console width | ||
* @param linePrefix - The string to prefix each line with, defaults to '' | ||
*/ | ||
static wrapWordsToLines(text: string, maxLineLength?: number, linePrefix?: string): string[]; | ||
/** | ||
* Applies word wrapping and returns an array of lines. | ||
* | ||
* @param text - The text to wrap | ||
* @param maxLineLength - The maximum length of a line, defaults to the console width | ||
* @param indentOrLinePrefix - The number of spaces to indent the wrapped lines or the string to prefix | ||
* each line with, defaults to no prefix | ||
*/ | ||
static wrapWordsToLines(text: string, maxLineLength?: number, indentOrLinePrefix?: number | string): string[]; | ||
/** | ||
* Displays a message in the console wrapped in a box UI. | ||
@@ -26,0 +71,0 @@ * |
"use strict"; | ||
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. | ||
// See LICENSE in the project root for license information. | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.PrintUtilities = exports.DEFAULT_CONSOLE_WIDTH = void 0; | ||
const wordwrap_1 = __importDefault(require("wordwrap")); | ||
/** | ||
@@ -26,31 +22,71 @@ * A sensible fallback column width for consoles. | ||
static getConsoleWidth() { | ||
const stdout = process.stdout; | ||
if (stdout && stdout.columns) { | ||
return stdout.columns; | ||
} | ||
var _a; | ||
return (_a = process.stdout) === null || _a === void 0 ? void 0 : _a.columns; | ||
} | ||
/** | ||
* Applies word wrapping. If maxLineLength is unspecified, then it defaults to the | ||
* console width. | ||
*/ | ||
static wrapWords(text, maxLineLength, indent) { | ||
if (!indent) { | ||
indent = 0; | ||
static wrapWords(text, maxLineLength, indentOrLinePrefix) { | ||
const wrappedLines = PrintUtilities.wrapWordsToLines(text, maxLineLength, indentOrLinePrefix // TS is confused by the overloads | ||
); | ||
return wrappedLines.join('\n'); | ||
} | ||
static wrapWordsToLines(text, maxLineLength, indentOrLinePrefix) { | ||
var _a; | ||
let linePrefix; | ||
switch (typeof indentOrLinePrefix) { | ||
case 'number': | ||
linePrefix = ' '.repeat(indentOrLinePrefix); | ||
break; | ||
case 'string': | ||
linePrefix = indentOrLinePrefix; | ||
break; | ||
default: | ||
linePrefix = ''; | ||
break; | ||
} | ||
const linePrefixLength = linePrefix.length; | ||
if (!maxLineLength) { | ||
maxLineLength = PrintUtilities.getConsoleWidth() || exports.DEFAULT_CONSOLE_WIDTH; | ||
} | ||
// Apply word wrapping and the provided indent, while also respecting existing newlines | ||
// Apply word wrapping and the provided line prefix, while also respecting existing newlines | ||
// and prefix spaces that may exist in the text string already. | ||
const lines = text.split(/\r?\n/); | ||
const wrappedLines = lines.map((line) => { | ||
var _a; | ||
const startingSpace = line.match(/^ +/); | ||
const addlIndent = ((_a = startingSpace === null || startingSpace === void 0 ? void 0 : startingSpace[0]) === null || _a === void 0 ? void 0 : _a.length) || 0; | ||
if (addlIndent > 0) { | ||
line = line.replace(/^ +/, ''); | ||
const wrappedLines = []; | ||
for (const line of lines) { | ||
if (line.length + linePrefixLength <= maxLineLength) { | ||
wrappedLines.push(linePrefix + line); | ||
} | ||
return (0, wordwrap_1.default)(indent + addlIndent, maxLineLength - indent - addlIndent, { mode: 'soft' })(line); | ||
}); | ||
return wrappedLines.join('\n'); | ||
else { | ||
const lineAdditionalPrefix = ((_a = line.match(/^\s*/)) === null || _a === void 0 ? void 0 : _a[0]) || ''; | ||
const whitespaceRegexp = /\s+/g; | ||
let currentWhitespaceMatch = null; | ||
let previousWhitespaceMatch; | ||
let currentLineStartIndex = lineAdditionalPrefix.length; | ||
let previousBreakRanOver = false; | ||
while ((currentWhitespaceMatch = whitespaceRegexp.exec(line)) !== null) { | ||
if (currentWhitespaceMatch.index + linePrefixLength - currentLineStartIndex > maxLineLength) { | ||
let whitespaceToSplitAt; | ||
if (!previousWhitespaceMatch || | ||
// Handle the case where there are two words longer than the maxLineLength in a row | ||
previousBreakRanOver) { | ||
whitespaceToSplitAt = currentWhitespaceMatch; | ||
} | ||
else { | ||
whitespaceToSplitAt = previousWhitespaceMatch; | ||
} | ||
wrappedLines.push(linePrefix + | ||
lineAdditionalPrefix + | ||
line.substring(currentLineStartIndex, whitespaceToSplitAt.index)); | ||
previousBreakRanOver = whitespaceToSplitAt.index - currentLineStartIndex > maxLineLength; | ||
currentLineStartIndex = whitespaceToSplitAt.index + whitespaceToSplitAt[0].length; | ||
} | ||
else { | ||
previousBreakRanOver = false; | ||
} | ||
previousWhitespaceMatch = currentWhitespaceMatch; | ||
} | ||
if (currentLineStartIndex < line.length) { | ||
wrappedLines.push(linePrefix + lineAdditionalPrefix + line.substring(currentLineStartIndex)); | ||
} | ||
} | ||
} | ||
return wrappedLines; | ||
} | ||
@@ -68,4 +104,3 @@ /** | ||
const maxLineLength = boxWidth - 10; | ||
const wrappedMessage = PrintUtilities.wrapWords(message, maxLineLength); | ||
const wrappedMessageLines = wrappedMessage.split('\n'); | ||
const wrappedMessageLines = PrintUtilities.wrapWordsToLines(message, maxLineLength); | ||
// ╔═══════════╗ | ||
@@ -72,0 +107,0 @@ // ║ Message ║ |
{ | ||
"name": "@rushstack/terminal", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"description": "User interface primitives for console applications", | ||
@@ -14,3 +14,2 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"wordwrap": "~1.0.0", | ||
"@rushstack/node-core-library": "3.60.0" | ||
@@ -21,3 +20,2 @@ }, | ||
"@types/node": "18.17.15", | ||
"@types/wordwrap": "~1.0.0", | ||
"colors": "~1.2.1", | ||
@@ -24,0 +22,0 @@ "@rushstack/eslint-config": "3.3.4", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
190476
2
6
2643
- Removedwordwrap@~1.0.0
- Removedwordwrap@1.0.0(transitive)