|
| 1 | +import GraphemeSplitter from 'grapheme-splitter' |
| 2 | +import { isEqual } from 'lodash' |
| 3 | +import stripAnsi from 'strip-ansi' |
| 4 | + |
| 5 | +import { |
| 6 | + type Color, |
| 7 | + type BackgroundColor, |
| 8 | + type RGB, |
| 9 | + type Modifier, |
| 10 | + ansiCode, |
| 11 | + moveCursor, |
| 12 | +} from './ansi' |
| 13 | + |
| 14 | +type $GraphemeString = string & { readonly _brand: 'GraphemeString' } |
| 15 | + |
| 16 | +export type Grapheme = { |
| 17 | + grapheme: $GraphemeString |
| 18 | + textColor?: { type: 'color'; color: Color } | { type: 'rgb'; rgb: RGB } |
| 19 | + backgroundColor?: |
| 20 | + | { type: 'color'; color: BackgroundColor } |
| 21 | + | { type: 'rgb'; rgb: RGB } |
| 22 | + textStyles?: Modifier[] |
| 23 | +} |
| 24 | + |
| 25 | +const splitter = new GraphemeSplitter() |
| 26 | + |
| 27 | +export function toGraphemeString(grapheme: string): $GraphemeString { |
| 28 | + const stripped = stripAnsi(grapheme) |
| 29 | + const numGraphemes = splitter.countGraphemes(stripped) |
| 30 | + if (numGraphemes === 0) { |
| 31 | + return ' ' as $GraphemeString |
| 32 | + } |
| 33 | + |
| 34 | + return splitter.iterateGraphemes(stripped).next().value as $GraphemeString |
| 35 | +} |
| 36 | + |
| 37 | +function equalStyles(a: Grapheme, b: Grapheme): boolean { |
| 38 | + type GraphemeStyle = Omit<Grapheme, 'grapheme'> & |
| 39 | + Partial<Pick<Grapheme, 'grapheme'>> |
| 40 | + const aStyles: GraphemeStyle = { ...a } |
| 41 | + delete aStyles.grapheme |
| 42 | + const bStyles: GraphemeStyle = { ...b } |
| 43 | + delete bStyles.grapheme |
| 44 | + return isEqual(aStyles, bStyles) |
| 45 | +} |
| 46 | + |
| 47 | +function graphemeCommands(grapheme: Grapheme): string[] { |
| 48 | + const commands: string[] = [] |
| 49 | + if (grapheme.textColor) { |
| 50 | + commands.push( |
| 51 | + ansiCode( |
| 52 | + grapheme.textColor.type === 'color' |
| 53 | + ? { |
| 54 | + type: 'style', |
| 55 | + style: grapheme.textColor.color, |
| 56 | + } |
| 57 | + : { |
| 58 | + type: 'text', |
| 59 | + rgb: grapheme.textColor.rgb, |
| 60 | + }, |
| 61 | + ), |
| 62 | + ) |
| 63 | + } |
| 64 | + if (grapheme.backgroundColor) { |
| 65 | + commands.push( |
| 66 | + ansiCode( |
| 67 | + grapheme.backgroundColor.type === 'color' |
| 68 | + ? { |
| 69 | + type: 'style', |
| 70 | + style: grapheme.backgroundColor.color, |
| 71 | + } |
| 72 | + : { |
| 73 | + type: 'text', |
| 74 | + rgb: grapheme.backgroundColor.rgb, |
| 75 | + }, |
| 76 | + ), |
| 77 | + ) |
| 78 | + } |
| 79 | + |
| 80 | + if (grapheme.textStyles) { |
| 81 | + for (const style of grapheme.textStyles) { |
| 82 | + commands.push(ansiCode({ type: 'style', style })) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + commands.push(grapheme.grapheme) |
| 87 | + |
| 88 | + return commands |
| 89 | +} |
| 90 | + |
| 91 | +function graphemeDiffCommands( |
| 92 | + prevGrapheme: Grapheme | null, |
| 93 | + newGrapheme: Grapheme, |
| 94 | +): string[] { |
| 95 | + if (!prevGrapheme) { |
| 96 | + return graphemeCommands(newGrapheme) |
| 97 | + } |
| 98 | + |
| 99 | + if (equalStyles(prevGrapheme, newGrapheme)) { |
| 100 | + return [newGrapheme.grapheme] |
| 101 | + } |
| 102 | + |
| 103 | + return [ |
| 104 | + ...ansiCode({ type: 'style', style: 'RESET' }), |
| 105 | + ...graphemeCommands(newGrapheme), |
| 106 | + ] |
| 107 | +} |
| 108 | + |
| 109 | +export type GraphemeImage = Grapheme[][] |
| 110 | + |
| 111 | +export function fullImageCommands(image: GraphemeImage): string[] { |
| 112 | + const commands: string[] = [moveCursor(0, 0)] |
| 113 | + |
| 114 | + let lastGrapheme: Grapheme | null = null |
| 115 | + for (const row of image) { |
| 116 | + for (const grapheme of row) { |
| 117 | + commands.push(...graphemeDiffCommands(lastGrapheme, grapheme)) |
| 118 | + lastGrapheme = grapheme |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return commands |
| 123 | +} |
| 124 | + |
| 125 | +export function diffImageCommands( |
| 126 | + oldImage: GraphemeImage, |
| 127 | + newImage: GraphemeImage, |
| 128 | +): string[] { |
| 129 | + if (oldImage.length !== newImage.length) { |
| 130 | + return fullImageCommands(newImage) |
| 131 | + } |
| 132 | + if (oldImage[0].length !== newImage[0].length) { |
| 133 | + return fullImageCommands(newImage) |
| 134 | + } |
| 135 | + |
| 136 | + const commands: string[] = [] |
| 137 | + let prevGrapheme: Grapheme | null = null |
| 138 | + let skipped = false |
| 139 | + for (const [r, row] of oldImage.entries()) { |
| 140 | + const oldRow = oldImage[r] |
| 141 | + for (const [c, grapheme] of row.entries()) { |
| 142 | + const oldGrapheme = oldRow[c] |
| 143 | + if (isEqual(grapheme, oldGrapheme)) { |
| 144 | + skipped = true |
| 145 | + continue |
| 146 | + } |
| 147 | + |
| 148 | + if (skipped) { |
| 149 | + commands.push(moveCursor(r, c)) |
| 150 | + skipped = false |
| 151 | + } |
| 152 | + |
| 153 | + commands.push(...graphemeDiffCommands(prevGrapheme, grapheme)) |
| 154 | + prevGrapheme = grapheme |
| 155 | + } |
| 156 | + } |
| 157 | + return commands |
| 158 | +} |
0 commit comments