From 3d713aaf536d9a5ad3dd625db606b7525ace7342 Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Tue, 29 Oct 2024 10:03:45 +0100 Subject: [PATCH] measure with get-east-asian-width --- packages/playwright/ThirdPartyNotices.txt | 17 ++++++++++++++++- .../playwright/bundles/utils/package-lock.json | 18 ++++++++++++++++++ packages/playwright/bundles/utils/package.json | 1 + .../bundles/utils/src/utilsBundleImpl.ts | 3 +++ packages/playwright/src/reporters/base.ts | 5 +++-- packages/playwright/src/utilsBundle.ts | 1 + tests/playwright-test/fit-to-width.spec.ts | 8 +++++--- 7 files changed, 47 insertions(+), 6 deletions(-) diff --git a/packages/playwright/ThirdPartyNotices.txt b/packages/playwright/ThirdPartyNotices.txt index f2bb64d661..2931da55c9 100644 --- a/packages/playwright/ThirdPartyNotices.txt +++ b/packages/playwright/ThirdPartyNotices.txt @@ -112,6 +112,7 @@ This project incorporates components from the projects listed below. The origina - escape-string-regexp@2.0.0 (https://github.com/sindresorhus/escape-string-regexp) - fill-range@7.1.1 (https://github.com/jonschlinkert/fill-range) - gensync@1.0.0-beta.2 (https://github.com/loganfsmyth/gensync) +- get-east-asian-width@1.3.0 (https://github.com/sindresorhus/get-east-asian-width) - glob-parent@5.1.2 (https://github.com/gulpjs/glob-parent) - globals@11.12.0 (https://github.com/sindresorhus/globals) - graceful-fs@4.2.11 (https://github.com/isaacs/node-graceful-fs) @@ -3410,6 +3411,20 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI ========================================= END OF gensync@1.0.0-beta.2 AND INFORMATION +%% get-east-asian-width@1.3.0 NOTICES AND INFORMATION BEGIN HERE +========================================= +MIT License + +Copyright (c) Sindre Sorhus (https://sindresorhus.com) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +========================================= +END OF get-east-asian-width@1.3.0 AND INFORMATION + %% glob-parent@5.1.2 NOTICES AND INFORMATION BEGIN HERE ========================================= The ISC License @@ -4399,6 +4414,6 @@ END OF yallist@3.1.1 AND INFORMATION SUMMARY BEGIN HERE ========================================= -Total Packages: 151 +Total Packages: 152 ========================================= END OF SUMMARY \ No newline at end of file diff --git a/packages/playwright/bundles/utils/package-lock.json b/packages/playwright/bundles/utils/package-lock.json index fcf9f972fe..5fd1392d33 100644 --- a/packages/playwright/bundles/utils/package-lock.json +++ b/packages/playwright/bundles/utils/package-lock.json @@ -10,6 +10,7 @@ "dependencies": { "chokidar": "3.6.0", "enquirer": "2.3.6", + "get-east-asian-width": "1.3.0", "json5": "2.2.3", "pirates": "4.0.4", "source-map-support": "0.5.21", @@ -146,6 +147,18 @@ "node": "^8.16.0 || ^10.6.0 || >=11.0.0" } }, + "node_modules/get-east-asian-width": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/get-east-asian-width/-/get-east-asian-width-1.3.0.tgz", + "integrity": "sha512-vpeMIQKxczTD/0s2CdEWHcb0eeJe6TFjxb+J5xgX7hScxqrGuyjmv4c1D4A/gelKfyox0gJJwIHF+fLjeaM8kQ==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/glob-parent": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz", @@ -376,6 +389,11 @@ "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", "optional": true }, + "get-east-asian-width": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/get-east-asian-width/-/get-east-asian-width-1.3.0.tgz", + "integrity": "sha512-vpeMIQKxczTD/0s2CdEWHcb0eeJe6TFjxb+J5xgX7hScxqrGuyjmv4c1D4A/gelKfyox0gJJwIHF+fLjeaM8kQ==" + }, "glob-parent": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz", diff --git a/packages/playwright/bundles/utils/package.json b/packages/playwright/bundles/utils/package.json index 69477909c5..3fb09c1d17 100644 --- a/packages/playwright/bundles/utils/package.json +++ b/packages/playwright/bundles/utils/package.json @@ -11,6 +11,7 @@ "dependencies": { "chokidar": "3.6.0", "enquirer": "2.3.6", + "get-east-asian-width": "1.3.0", "json5": "2.2.3", "pirates": "4.0.4", "source-map-support": "0.5.21", diff --git a/packages/playwright/bundles/utils/src/utilsBundleImpl.ts b/packages/playwright/bundles/utils/src/utilsBundleImpl.ts index 7c29c301a8..6cd35e2885 100644 --- a/packages/playwright/bundles/utils/src/utilsBundleImpl.ts +++ b/packages/playwright/bundles/utils/src/utilsBundleImpl.ts @@ -31,3 +31,6 @@ export const enquirer = enquirerLibrary; import chokidarLibrary from 'chokidar'; export const chokidar = chokidarLibrary; + +import * as getEastAsianWidthLibrary from 'get-east-asian-width'; +export const getEastAsianWidth = getEastAsianWidthLibrary; diff --git a/packages/playwright/src/reporters/base.ts b/packages/playwright/src/reporters/base.ts index 1e93740403..110bc5f6b0 100644 --- a/packages/playwright/src/reporters/base.ts +++ b/packages/playwright/src/reporters/base.ts @@ -18,6 +18,7 @@ import { colors as realColors, ms as milliseconds, parseStackTraceLine } from 'p import path from 'path'; import type { FullConfig, TestCase, Suite, TestResult, TestError, FullResult, TestStep, Location } from '../../types/testReporter'; import { getPackageManagerExecCommand } from 'playwright-core/lib/utils'; +import { getEastAsianWidth } from '../utilsBundle'; import type { ReporterV2 } from './reporterV2'; import { resolveReporterOutputPath } from '../util'; export type TestResultOutput = { chunk: string | Buffer, type: 'stdout' | 'stderr' }; @@ -490,7 +491,7 @@ export function stripAnsiEscapes(str: string): string { } function characterWidth(c: string) { - return /[\u4e00-\u9fff]/.test(c) ? 2 : 1; + return getEastAsianWidth.eastAsianWidth(c.codePointAt(0)!); } function stringWidth(v: string) { @@ -530,7 +531,7 @@ export function fitToWidth(line: string, width: number, prefix?: string): string } else { let part = suffixOfWidth(parts[i], width); const wasTruncated = part.length < parts[i].length; - if (wasTruncated && part.length > 0) { + if (wasTruncated && parts[i].length > 0) { // Add ellipsis if we are truncating. part = '\u2026' + suffixOfWidth(parts[i], width - 1); } diff --git a/packages/playwright/src/utilsBundle.ts b/packages/playwright/src/utilsBundle.ts index eec0cf9a5a..d0e2a37e77 100644 --- a/packages/playwright/src/utilsBundle.ts +++ b/packages/playwright/src/utilsBundle.ts @@ -20,4 +20,5 @@ export const sourceMapSupport: typeof import('../bundles/utils/node_modules/@typ export const stoppable: typeof import('../bundles/utils/node_modules/@types/stoppable') = require('./utilsBundleImpl').stoppable; export const enquirer: typeof import('../bundles/utils/node_modules/enquirer') = require('./utilsBundleImpl').enquirer; export const chokidar: typeof import('../bundles/utils/node_modules/chokidar') = require('./utilsBundleImpl').chokidar; +export const getEastAsianWidth: typeof import('../bundles/utils/node_modules/get-east-asian-width') = require('./utilsBundleImpl').getEastAsianWidth; export type { RawSourceMap } from '../bundles/utils/node_modules/source-map'; diff --git a/tests/playwright-test/fit-to-width.spec.ts b/tests/playwright-test/fit-to-width.spec.ts index 6a45d8c64f..94e73cb381 100644 --- a/tests/playwright-test/fit-to-width.spec.ts +++ b/tests/playwright-test/fit-to-width.spec.ts @@ -23,8 +23,10 @@ test('chinese characters', () => { }); test('surrogate pairs', () => { - expect(fitToWidth('🫣🤗', 2)).toBe('🫣🤗'); + expect(fitToWidth('🫣🤗', 2)).toBe('…'); + expect(fitToWidth('🫣🤗', 3)).toBe('…🤗'); expect(fitToWidth('🚄🚄', 1)).toBe('…'); - expect(fitToWidth('🚄🚄🚄', 2)).toBe('…🚄'); - expect(fitToWidth('🚄🚄', 2)).toBe('🚄🚄'); + expect(fitToWidth('🚄🚄', 2)).toBe('…'); + expect(fitToWidth('🚄🚄', 3)).toBe('…🚄'); + expect(fitToWidth('🚄🚄', 4)).toBe('🚄🚄'); });