-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add character length dictionary for string length computation in…
… console (#431)
- Loading branch information
Showing
9 changed files
with
96 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,27 @@ | ||
import { wcswidth } from 'simple-wcswidth'; | ||
import { CharLengthDict } from '../models/common'; | ||
|
||
/* eslint-disable no-control-regex */ | ||
const colorRegex = /\x1b\[\d{1,3}m/g; // \x1b[30m \x1b[305m | ||
|
||
const stripAnsi = (str: string): string => str.replace(colorRegex, ''); | ||
const findWidthInConsole = (str: string): number => wcswidth(stripAnsi(str)); | ||
|
||
export const findWidthInConsole = ( | ||
str: string, | ||
charLength?: CharLengthDict | ||
): number => { | ||
let strLen = 0; | ||
str = stripAnsi(str); | ||
if (charLength) { | ||
Object.entries(charLength).forEach(([key, value]) => { | ||
// count appearance of the key in the string and remove from original string | ||
let regex = new RegExp(key, 'g'); | ||
strLen += (str.match(regex) || []).length * value; | ||
str = str.replace(key, ''); | ||
}); | ||
} | ||
strLen += wcswidth(str); | ||
return strLen; | ||
}; | ||
|
||
export default findWidthInConsole; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters