measure with get-east-asian-width

This commit is contained in:
Simon Knott 2024-10-29 10:03:45 +01:00
parent f778ddb6c5
commit 3d713aaf53
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC
7 changed files with 47 additions and 6 deletions

View file

@ -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) - escape-string-regexp@2.0.0 (https://github.com/sindresorhus/escape-string-regexp)
- fill-range@7.1.1 (https://github.com/jonschlinkert/fill-range) - fill-range@7.1.1 (https://github.com/jonschlinkert/fill-range)
- gensync@1.0.0-beta.2 (https://github.com/loganfsmyth/gensync) - 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) - glob-parent@5.1.2 (https://github.com/gulpjs/glob-parent)
- globals@11.12.0 (https://github.com/sindresorhus/globals) - globals@11.12.0 (https://github.com/sindresorhus/globals)
- graceful-fs@4.2.11 (https://github.com/isaacs/node-graceful-fs) - 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 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 <sindresorhus@gmail.com> (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 %% glob-parent@5.1.2 NOTICES AND INFORMATION BEGIN HERE
========================================= =========================================
The ISC License The ISC License
@ -4399,6 +4414,6 @@ END OF yallist@3.1.1 AND INFORMATION
SUMMARY BEGIN HERE SUMMARY BEGIN HERE
========================================= =========================================
Total Packages: 151 Total Packages: 152
========================================= =========================================
END OF SUMMARY END OF SUMMARY

View file

@ -10,6 +10,7 @@
"dependencies": { "dependencies": {
"chokidar": "3.6.0", "chokidar": "3.6.0",
"enquirer": "2.3.6", "enquirer": "2.3.6",
"get-east-asian-width": "1.3.0",
"json5": "2.2.3", "json5": "2.2.3",
"pirates": "4.0.4", "pirates": "4.0.4",
"source-map-support": "0.5.21", "source-map-support": "0.5.21",
@ -146,6 +147,18 @@
"node": "^8.16.0 || ^10.6.0 || >=11.0.0" "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": { "node_modules/glob-parent": {
"version": "5.1.2", "version": "5.1.2",
"resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz", "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz",
@ -376,6 +389,11 @@
"integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==",
"optional": true "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": { "glob-parent": {
"version": "5.1.2", "version": "5.1.2",
"resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz", "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz",

View file

@ -11,6 +11,7 @@
"dependencies": { "dependencies": {
"chokidar": "3.6.0", "chokidar": "3.6.0",
"enquirer": "2.3.6", "enquirer": "2.3.6",
"get-east-asian-width": "1.3.0",
"json5": "2.2.3", "json5": "2.2.3",
"pirates": "4.0.4", "pirates": "4.0.4",
"source-map-support": "0.5.21", "source-map-support": "0.5.21",

View file

@ -31,3 +31,6 @@ export const enquirer = enquirerLibrary;
import chokidarLibrary from 'chokidar'; import chokidarLibrary from 'chokidar';
export const chokidar = chokidarLibrary; export const chokidar = chokidarLibrary;
import * as getEastAsianWidthLibrary from 'get-east-asian-width';
export const getEastAsianWidth = getEastAsianWidthLibrary;

View file

@ -18,6 +18,7 @@ import { colors as realColors, ms as milliseconds, parseStackTraceLine } from 'p
import path from 'path'; import path from 'path';
import type { FullConfig, TestCase, Suite, TestResult, TestError, FullResult, TestStep, Location } from '../../types/testReporter'; import type { FullConfig, TestCase, Suite, TestResult, TestError, FullResult, TestStep, Location } from '../../types/testReporter';
import { getPackageManagerExecCommand } from 'playwright-core/lib/utils'; import { getPackageManagerExecCommand } from 'playwright-core/lib/utils';
import { getEastAsianWidth } from '../utilsBundle';
import type { ReporterV2 } from './reporterV2'; import type { ReporterV2 } from './reporterV2';
import { resolveReporterOutputPath } from '../util'; import { resolveReporterOutputPath } from '../util';
export type TestResultOutput = { chunk: string | Buffer, type: 'stdout' | 'stderr' }; export type TestResultOutput = { chunk: string | Buffer, type: 'stdout' | 'stderr' };
@ -490,7 +491,7 @@ export function stripAnsiEscapes(str: string): string {
} }
function characterWidth(c: string) { function characterWidth(c: string) {
return /[\u4e00-\u9fff]/.test(c) ? 2 : 1; return getEastAsianWidth.eastAsianWidth(c.codePointAt(0)!);
} }
function stringWidth(v: string) { function stringWidth(v: string) {
@ -530,7 +531,7 @@ export function fitToWidth(line: string, width: number, prefix?: string): string
} else { } else {
let part = suffixOfWidth(parts[i], width); let part = suffixOfWidth(parts[i], width);
const wasTruncated = part.length < parts[i].length; const wasTruncated = part.length < parts[i].length;
if (wasTruncated && part.length > 0) { if (wasTruncated && parts[i].length > 0) {
// Add ellipsis if we are truncating. // Add ellipsis if we are truncating.
part = '\u2026' + suffixOfWidth(parts[i], width - 1); part = '\u2026' + suffixOfWidth(parts[i], width - 1);
} }

View file

@ -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 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 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 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'; export type { RawSourceMap } from '../bundles/utils/node_modules/source-map';

View file

@ -23,8 +23,10 @@ test('chinese characters', () => {
}); });
test('surrogate pairs', () => { test('surrogate pairs', () => {
expect(fitToWidth('🫣🤗', 2)).toBe('🫣🤗'); expect(fitToWidth('🫣🤗', 2)).toBe('…');
expect(fitToWidth('🫣🤗', 3)).toBe('…🤗');
expect(fitToWidth('🚄🚄', 1)).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('🚄🚄');
}); });