forked from gajus/table
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: wrap word support ansi (gajus#155)
* Wrap word support ansi * Refactor * Implement split ansi string Co-authored-by: Nam Hoang Le <[email protected]>
- Loading branch information
1 parent
574c64a
commit 509fa46
Showing
6 changed files
with
165 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,48 @@ | ||
import slice from 'slice-ansi'; | ||
import stringWidth from 'string-width'; | ||
import stripAnsi from 'strip-ansi'; | ||
|
||
export default (input: string, size: number): string[] => { | ||
let subject = input; | ||
const calculateStringLengths = (input: string, size: number): Array<[Length:number, Offset: number]> => { | ||
let subject = stripAnsi(input); | ||
|
||
const chunks = []; | ||
const chunks: Array<[number, number]> = []; | ||
|
||
// https://regex101.com/r/gY5kZ1/1 | ||
const re = new RegExp('(^.{1,' + String(size) + '}(\\s+|$))|(^.{1,' + String(size - 1) + '}(\\\\|/|_|\\.|,|;|-))'); | ||
|
||
do { | ||
let chunk; | ||
let chunk: string; | ||
|
||
const match = re.exec(subject); | ||
|
||
chunk = re.exec(subject); | ||
if (match) { | ||
chunk = match[0]; | ||
|
||
if (chunk) { | ||
chunk = chunk[0]; | ||
subject = subject.slice(chunk.length); | ||
|
||
subject = slice(subject, stringWidth(chunk)); | ||
const trimmedLength = chunk.trim().length; | ||
const offset = chunk.length - trimmedLength; | ||
|
||
chunk = chunk.trim(); | ||
chunks.push([trimmedLength, offset]); | ||
} else { | ||
chunk = slice(subject, 0, size); | ||
subject = slice(subject, size); | ||
} | ||
chunk = subject.slice(0, size); | ||
subject = subject.slice(size); | ||
|
||
chunks.push(chunk); | ||
} while (stringWidth(subject)); | ||
chunks.push([chunk.length, 0]); | ||
} | ||
} while (subject.length); | ||
|
||
return chunks; | ||
}; | ||
|
||
export default (input: string, size: number): string[] => { | ||
const result: string[] = []; | ||
|
||
let startIndex = 0; | ||
calculateStringLengths(input, size).forEach(([length, offset]) => { | ||
result.push(slice(input, startIndex, startIndex + length)); | ||
|
||
startIndex += length + offset; | ||
}); | ||
|
||
return result; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export const openRed = '\u001b[31m'; | ||
export const closeRed = '\u001b[39m'; | ||
|
||
export const stringToRed = (string: string) => { | ||
return openRed + string + closeRed; | ||
}; | ||
|
||
export const arrayToRed = (array: string[]) => { | ||
return array.map((string) => { | ||
return string === '' ? '' : stringToRed(string); | ||
}); | ||
}; |
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