Skip to content

Commit

Permalink
Fix a case where printMessageInBox would throw if the message contain…
Browse files Browse the repository at this point in the history
…s a line longer than the boxwidth.
  • Loading branch information
iclanton committed Sep 28, 2023
1 parent 328826c commit 02877d2
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@rushstack/terminal",
"comment": "Fix an issue where `PrintUtilities.printMessageInBox` would throw if the message contains a word that is longer than the box width. In this case, `printMessageInBox` will print bars above and below the message, and then print the message lines, allowing the console to wrap them.",
"type": "patch"
}
],
"packageName": "@rushstack/terminal"
}
41 changes: 31 additions & 10 deletions libraries/terminal/src/PrintUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,20 +177,41 @@ export class PrintUtilities {
const consoleWidth: number = PrintUtilities.getConsoleWidth() || DEFAULT_CONSOLE_WIDTH;
boxWidth = Math.floor(consoleWidth / 2);
}

const maxLineLength: number = boxWidth - 10;
const wrappedMessageLines: string[] = PrintUtilities.wrapWordsToLines(message, maxLineLength);

// ╔═══════════╗
// ║ Message ║
// ╚═══════════╝
terminal.writeLine(` ╔${'═'.repeat(boxWidth - 2)}╗ `);
let longestLineLength: number = 0;
const trimmedLines: string[] = [];
for (const line of wrappedMessageLines) {
const trimmedLine: string = line.trim();
const padding: number = boxWidth - trimmedLine.length - 2;
const leftPadding: number = Math.floor(padding / 2);
const rightPadding: number = padding - leftPadding;
terminal.writeLine(` ║${' '.repeat(leftPadding)}${trimmedLine}${' '.repeat(rightPadding)}║ `);
trimmedLines.push(trimmedLine);
longestLineLength = Math.max(longestLineLength, trimmedLine.length);
}

if (longestLineLength > boxWidth - 2) {
// If the longest line is longer than the box, print bars above and below the message
// ═════════════
// Message
// ═════════════
const headerAndFooter: string = ` ${'═'.repeat(boxWidth)}`;
terminal.writeLine(headerAndFooter);
for (const line of wrappedMessageLines) {
terminal.writeLine(` ${line}`);
}

terminal.writeLine(headerAndFooter);
} else {
// ╔═══════════╗
// ║ Message ║
// ╚═══════════╝
terminal.writeLine(` ╔${'═'.repeat(boxWidth - 2)}╗`);
for (const trimmedLine of trimmedLines) {
const padding: number = boxWidth - trimmedLine.length - 2;
const leftPadding: number = Math.floor(padding / 2);
const rightPadding: number = padding - leftPadding;
terminal.writeLine(` ║${' '.repeat(leftPadding)}${trimmedLine}${' '.repeat(rightPadding)}║`);
}
terminal.writeLine(` ╚${'═'.repeat(boxWidth - 2)}╝`);
}
terminal.writeLine(` ╚${'═'.repeat(boxWidth - 2)}╝ `);
}
}
12 changes: 12 additions & 0 deletions libraries/terminal/src/test/PrintUtilities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,17 @@ describe(PrintUtilities.name, () => {
PrintUtilities.printMessageInBox(userMessage, terminal, 50);
validateOutput(50);
});

it('Handles a case where there is a word longer than the boxwidth', () => {
const userMessage: string = [
'Annnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn error occurred while pushing commits to git remote. Please make sure you have installed and enabled git lfs. The easiest way to do that is run the provided setup script:',
'',
' common/scripts/setup.sh',
''
].join('\n');

PrintUtilities.printMessageInBox(userMessage, terminal, 50);
validateOutput(50);
});
});
});
119 changes: 68 additions & 51 deletions libraries/terminal/src/test/__snapshots__/PrintUtilities.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,76 +1,93 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`PrintUtilities printMessageInBox Handles a case where there is a word longer than the boxwidth 1`] = `
Array [
" ══════════════════════════════════════════════════",
" Annnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn",
" error occurred while pushing commits to",
" git remote. Please make sure you have",
" installed and enabled git lfs. The",
" easiest way to do that is run the",
" provided setup script:",
" ",
" common/scripts/setup.sh",
" ",
" ══════════════════════════════════════════════════",
"",
]
`;

exports[`PrintUtilities printMessageInBox prints a long message wrapped in a box using the console width 1`] = `
Array [
" ╔══════════════════════════════╗ ",
" ║ Lorem ipsum dolor sit ║ ",
" ║ amet, consectetuer ║ ",
" ║ adipiscing elit. ║ ",
" ║ Maecenas porttitor ║ ",
" ║ congue massa. Fusce ║ ",
" ║ posuere, magna sed ║ ",
" ║ pulvinar ultricies, ║ ",
" ║ purus lectus malesuada ║ ",
" ║ libero, sit amet ║ ",
" ║ commodo magna eros ║ ",
" ║ quis urna. ║ ",
" ╚══════════════════════════════╝ ",
" ╔══════════════════════════════╗",
" ║ Lorem ipsum dolor sit ║",
" ║ amet, consectetuer ║",
" ║ adipiscing elit. ║",
" ║ Maecenas porttitor ║",
" ║ congue massa. Fusce ║",
" ║ posuere, magna sed ║",
" ║ pulvinar ultricies, ║",
" ║ purus lectus malesuada ║",
" ║ libero, sit amet ║",
" ║ commodo magna eros ║",
" ║ quis urna. ║",
" ╚══════════════════════════════╝",
"",
]
`;

exports[`PrintUtilities printMessageInBox prints a long message wrapped in a wide box 1`] = `
Array [
" ╔══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗ ",
" ║ Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna. ║ ",
" ╚══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝ ",
" ╔══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗",
" ║ Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna. ║",
" ╚══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝",
"",
]
`;

exports[`PrintUtilities printMessageInBox prints a long message wrapped in narrow box 1`] = `
Array [
" ╔══════════════════╗ ",
" ║ Lorem ║ ",
" ║ ipsum ║ ",
" ║ dolor sit ║ ",
" ║ amet, ║ ",
" ║ consectetuer ║ ",
" ║ adipiscing elit. ║ ",
" ║ Maecenas ║ ",
" ║ porttitor ║ ",
" ║ congue ║ ",
" ║ massa. ║ ",
" ║ Fusce ║ ",
" ║ posuere, ║ ",
" ║ magna sed ║ ",
" ║ pulvinar ║ ",
" ║ ultricies, ║ ",
" ║ purus ║ ",
" ║ lectus ║ ",
" ║ malesuada ║ ",
" ║ libero, ║ ",
" ║ sit amet ║ ",
" ║ commodo ║ ",
" ║ magna eros ║ ",
" ║ quis urna. ║ ",
" ╚══════════════════╝ ",
" ╔══════════════════╗",
" ║ Lorem ║",
" ║ ipsum ║",
" ║ dolor sit ║",
" ║ amet, ║",
" ║ consectetuer ║",
" ║ adipiscing elit. ║",
" ║ Maecenas ║",
" ║ porttitor ║",
" ║ congue ║",
" ║ massa. ║",
" ║ Fusce ║",
" ║ posuere, ║",
" ║ magna sed ║",
" ║ pulvinar ║",
" ║ ultricies, ║",
" ║ purus ║",
" ║ lectus ║",
" ║ malesuada ║",
" ║ libero, ║",
" ║ sit amet ║",
" ║ commodo ║",
" ║ magna eros ║",
" ║ quis urna. ║",
" ╚══════════════════╝",
"",
]
`;

exports[`PrintUtilities printMessageInBox respects spaces and newlines in a pre-formatted message 1`] = `
Array [
" ╔════════════════════════════════════════════════╗ ",
" ║ An error occurred while pushing commits ║ ",
" ║ to git remote. Please make sure you have ║ ",
" ║ installed and enabled git lfs. The ║ ",
" ║ easiest way to do that is run the ║ ",
" ║ provided setup script: ║ ",
" ║ ║ ",
" ║ common/scripts/setup.sh ║ ",
" ║ ║ ",
" ╚════════════════════════════════════════════════╝ ",
" ╔════════════════════════════════════════════════╗",
" ║ An error occurred while pushing commits ║",
" ║ to git remote. Please make sure you have ║",
" ║ installed and enabled git lfs. The ║",
" ║ easiest way to do that is run the ║",
" ║ provided setup script: ║",
" ║ ║",
" ║ common/scripts/setup.sh ║",
" ║ ║",
" ╚════════════════════════════════════════════════╝",
"",
]
`;
Expand Down

0 comments on commit 02877d2

Please sign in to comment.