From 9458794fa3d2a851b0031485aaabc7072c8e7003 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Fri, 1 Mar 2024 10:28:12 -0500 Subject: [PATCH] Optimize highlighting escape sequences --- src/common/highlight.ts | 4 ++-- src/common/row.ts | 12 ++++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/common/highlight.ts b/src/common/highlight.ts index 3c9c58f..af29ece 100644 --- a/src/common/highlight.ts +++ b/src/common/highlight.ts @@ -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; } } diff --git a/src/common/row.ts b/src/common/row.ts index 48113a7..1b2a65b 100644 --- a/src/common/row.ts +++ b/src/common/row.ts @@ -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; }