Skip to content

Commit

Permalink
fix: fit to terminal width by calculating string width
Browse files Browse the repository at this point in the history
  • Loading branch information
PondWader committed Jul 2, 2024
1 parent 060394d commit e87f2a0
Show file tree
Hide file tree
Showing 13 changed files with 1,360 additions and 188 deletions.
18 changes: 9 additions & 9 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"bracketSpacing": false,
"printWidth": 80,
"semi": true,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "none",
"useTabs": false,
"arrowParens": "always"
}
"bracketSpacing": false,
"printWidth": 320,
"semi": true,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "none",
"useTabs": false,
"arrowParens": "always"
}
51 changes: 51 additions & 0 deletions bench/string-width.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {Bench} from 'tinybench';
import {getStringWidth} from '../dist/string-width.js';
import fastStringTruncatedWidth from 'fast-string-truncated-width';

const fastStringTruncatedWidthOptions = {limit: Infinity, ellipsis: ''};

const suites = [
{
name: 'Basic ASCII',
input: 'Hello'
},
{
name: 'ANSI + ASCII',
input: '\x1b[31mabc\x1b[31m'
},
{
name: 'Emoji',
input: '😀🤪🙂‍↔️🥶☝🏿☝🏿'
},
{
name: 'Emoji+ASCII',
input: '😀hello . 🤪🙂‍↔️🥶was☝🏿☝🏿'
},
{
name: 'CJK',
input: '古池や'
}
];

for (const suite of suites) {
const bench = new Bench();

bench
.add('picospinner getStringWidth()', () => {
for (const suite of suites) {
getStringWidth(suite.input);
}
})
.add('fast-string-truncated-width', () => {
for (const suite of suites) {
fastStringTruncatedWidth(suite.input, fastStringTruncatedWidthOptions, {}).width;
}
});

console.log('Benchmark:', suite.name);

await bench.warmup();
await bench.run();

console.table(bench.table());
}
17 changes: 13 additions & 4 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import eslint from '@eslint/js';
import {configs as tseslintConfigs} from 'typescript-eslint';
import globals from 'globals';

const {configs: eslintConfigs} = eslint;

export default [
{
files: ['src/**/*.ts']
},
eslintConfigs.recommended,
...tseslintConfigs.strict
...tseslintConfigs.strict,
{
files: ['src/**/*.ts', '**/*.js'],
rules: {
'no-control-regex': 'off'
},
languageOptions: {
globals: {
...globals.node
}
}
}
];
Loading

0 comments on commit e87f2a0

Please sign in to comment.