make change smaller

This commit is contained in:
Simon Knott 2024-10-28 12:14:46 +01:00
parent cb6b1a05b3
commit a761193a02
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC
2 changed files with 65 additions and 98 deletions

View file

@ -492,73 +492,43 @@ export function stripAnsiEscapes(str: string): string {
// Leaves enough space for the "prefix" to also fit. // Leaves enough space for the "prefix" to also fit.
export function fitToWidth(line: string, width: number, prefix?: string): string { export function fitToWidth(line: string, width: number, prefix?: string): string {
const prefixLength = prefix ? stripAnsiEscapes(prefix).length : 0; const prefixLength = prefix ? stripAnsiEscapes(prefix).length : 0;
return wrapText(line, width - prefixLength); width -= prefixLength;
} if (line.length <= width)
return line;
export function wrapText(text: string, width: number) { // Even items are plain text, odd items are control sequences.
// Handle edge cases const parts = line.split(ansiRegex);
if (!text) const taken: string[] = [];
return '';
if (width <= 0)
return text;
const lines = []; for (let i = parts.length - 1; i >= 0; i--) {
let currentLine = ''; if (i % 2) {
let currentWidth = 0; // Include all control sequences to preserve formatting.
taken.push(parts[i]);
} else {
let part = parts[i];
let partLength = 0;
for (let j = part.length - 1; j >= 0; j--) {
const char = part[j];
// Check if the character is a Chinese character
if (/[\u4e00-\u9fff]/.test(char))
partLength += 2; // Chinese characters take 2 spaces
else
partLength += 1;
const segments = text.split(' '); if (partLength > width) {
part = part.substring(j + 1);
for (const segment of segments) { if (part.length > 0) {
// Calculate segment width (Chinese characters count as 2, others as 1) // Add ellipsis if we are truncating.
const segmentWidth = segment part = '\u2026' + part.substring(1);
.split('') }
.reduce((width, char) => { break;
return width + (/[\u4e00-\u9fff]/.test(char) ? 2 : 1);
}, 0);
// Check if adding this segment would exceed the line width
if (currentWidth + segmentWidth > width && currentLine) {
// If current line isn't empty, start a new line
lines.push(currentLine.trim());
currentLine = '';
currentWidth = 0;
}
// Special handling for long segments that exceed width on their own
if (segmentWidth > width) {
if (currentLine) {
lines.push(currentLine.trim());
currentLine = '';
}
// Split long segment into chunks
let remaining = segment;
while (remaining) {
let chunk = '';
let chunkWidth = 0;
for (const char of remaining) {
const charWidth = /[\u4e00-\u9fff]/.test(char) ? 2 : 1;
if (chunkWidth + charWidth > width)
break;
chunk += char;
chunkWidth += charWidth;
} }
lines.push(chunk);
remaining = remaining.slice(chunk.length);
} }
continue; taken.push(part);
width -= partLength;
} }
// Add segment to current line
currentLine += segment;
currentWidth += segmentWidth;
} }
return taken.reverse().join('');
// Add the last line if it's not empty
if (currentLine.trim())
lines.push(currentLine.trim());
return lines.join('\n');
} }
function belongsToNodeModules(file: string) { function belongsToNodeModules(file: string) {

View file

@ -14,47 +14,44 @@
* limitations under the License. * limitations under the License.
*/ */
import { fitToWidth, wrapText } from 'packages/playwright/lib/reporters/base'; import { fitToWidth } from 'packages/playwright/lib/reporters/base';
import { test, expect } from './playwright-test-fixtures'; import { test, expect } from './playwright-test-fixtures';
test.describe('wrapText', () => { test('chinese characters', () => {
test('chinese characters', () => { expect(fitToWidth('你好', 2)).toBe('你\n好');
expect(wrapText('你好', 2)).toBe('你\n好'); expect(fitToWidth('你好你好', 4)).toBe('你好\n你好');
expect(wrapText('你好你好', 4)).toBe('你好\n你好');
});
test('mixed characters', () => {
expect(wrapText('hello你好', 5)).toBe('hello\n你好');
expect(wrapText('你好hello', 5)).toBe('你好h\nello');
});
test('special characters', () => {
expect(wrapText('hello@world', 5)).toBe('hello\n@worl\nd');
expect(wrapText('你好@世界', 3)).toBe('你\n好@\n世\n界');
});
test('long words', () => {
expect(wrapText('supercalifragilisticexpialidocious', 10)).toBe('supercalif\nragilistic\nexpialidoc\nious');
expect(wrapText('你好超级长的词', 5)).toBe('你好\n超级\n长的\n词');
});
test('empty string', () => {
expect(wrapText('', 5)).toBe('');
expect(wrapText('', 5)).toBe('');
});
test('single character', () => {
expect(wrapText('a', 1)).toBe('a');
expect(wrapText('a', 1)).toBe('a');
});
test('spaces', () => {
expect(wrapText('hello world', 5)).toBe('hello\nworld');
expect(wrapText('hello world', 5)).toBe('hello\nworld');
});
}); });
test('fitToWidth', () => { test('mixed characters', () => {
expect(fitToWidth('hello你好', 5)).toBe('hello\n你好');
expect(fitToWidth('你好hello', 5)).toBe('你好h\nello');
});
test('special characters', () => {
expect(fitToWidth('hello@world', 5)).toBe('hello\n@worl\nd');
expect(fitToWidth('你好@世界', 3)).toBe('你\n好@\n世\n界');
});
test('long words', () => {
expect(fitToWidth('supercalifragilisticexpialidocious', 10)).toBe('supercalif\nragilistic\nexpialidoc\nious');
expect(fitToWidth('你好超级长的词', 5)).toBe('你好\n超级\n长的\n词');
});
test('empty string', () => {
expect(fitToWidth('', 5)).toBe('');
expect(fitToWidth('', 5)).toBe('');
});
test('single character', () => {
expect(fitToWidth('a', 1)).toBe('a');
expect(fitToWidth('a', 1)).toBe('a');
});
test('spaces', () => {
expect(fitToWidth('hello world', 5)).toBe('hello\nworld');
expect(fitToWidth('hello world', 5)).toBe('hello\nworld');
});
test('prefix', () => {
expect(fitToWidth('hello world', 5, '~>')).toBe('hel\nlo\nwor\nld'); expect(fitToWidth('hello world', 5, '~>')).toBe('hel\nlo\nwor\nld');
}); });