virtualization: strip ansi color codes for measurement

This commit is contained in:
Matias Chomicki
2025-07-01 10:45:25 +02:00
parent afabb77d8e
commit 1385e4b33a
2 changed files with 7 additions and 6 deletions
@@ -1,3 +1,4 @@
import ansicolor from 'ansicolor';
import Prism, { Grammar } from 'prismjs';
import { DataFrame, dateTimeFormat, Labels, LogLevel, LogRowModel, LogsSortOrder } from '@grafana/data';
@@ -132,9 +133,9 @@ export class LogListModel implements LogRowModel {
return checkLogsSampled(this);
}
getDisplayedFieldValue(fieldName: string): string {
getDisplayedFieldValue(fieldName: string, stripAnsi = false): string {
if (fieldName === LOG_LINE_BODY_FIELD_NAME) {
return this.body;
return stripAnsi ? ansicolor.strip(this.body) : this.body;
}
let fieldValue = '';
if (this.labels[fieldName] != null) {
@@ -155,8 +156,8 @@ export class LogListModel implements LogRowModel {
updateCollapsedState(displayedFields: string[], container: HTMLDivElement | null) {
const lineLength =
displayedFields.length > 0
? displayedFields.map((field) => this.getDisplayedFieldValue(field)).join('').length
: this.raw.length;
? displayedFields.map((field) => this.getDisplayedFieldValue(field, true)).join('').length
: this.entry.length;
const collapsed =
lineLength >= (this._virtualization?.getTruncationLength(container) ?? TRUNCATION_DEFAULT_LENGTH)
? true
@@ -193,7 +193,7 @@ export class LogLineVirtualization {
levelWidth = Math.round(width);
}
for (const field of displayedFields) {
width = this.measureTextWidth(logs[i].getDisplayedFieldValue(field));
width = this.measureTextWidth(logs[i].getDisplayedFieldValue(field, true));
fieldWidths[field] = !fieldWidths[field] || width > fieldWidths[field] ? Math.round(width) : fieldWidths[field];
}
}
@@ -288,7 +288,7 @@ export function getLogLineSize(
textToMeasure += logs[index].displayLevel ?? '';
}
for (const field of displayedFields) {
textToMeasure = logs[index].getDisplayedFieldValue(field) + textToMeasure;
textToMeasure = logs[index].getDisplayedFieldValue(field, true) + textToMeasure;
}
if (!displayedFields.length) {
textToMeasure += ansicolor.strip(logs[index].body);