Optimize highlighting escape sequences

This commit is contained in:
Timothy Warren 2024-03-01 10:28:12 -05:00
parent a3a8fba6e2
commit 9458794fa3
2 changed files with 12 additions and 4 deletions

View File

@ -1,4 +1,4 @@
import Ansi, { AnsiColor } from './ansi.ts';
import Ansi from './ansi.ts';
export enum HighlightType {
None,
@ -11,6 +11,6 @@ export function highlightToColor(type: HighlightType): string {
return Ansi.color256(196);
default:
return Ansi.color(AnsiColor.FgDefault);
return Ansi.ResetFormatting;
}
}

View File

@ -179,13 +179,21 @@ export class Row {
const end = Math.min(len, this.rsize);
const start = Math.min(offset, len);
let result = '';
let currentHighlight = HighlightType.None;
for (let i = start; i < end; i++) {
result += highlightToColor(this.hl[i]);
const highlightType = this.hl[i];
if (highlightType !== currentHighlight) {
currentHighlight = highlightType;
result += highlightToColor(highlightType);
}
result += this.rchars[i];
result += Ansi.ResetFormatting;
}
result += Ansi.ResetFormatting;
return result;
}