mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Explore: Logs: Show ANSI colors when highlighting matched words (#40971)
* Explore: Show ANSI colors when highlighting matched words * only highlight ANSI text if needsHighlighter is true Co-authored-by: Gábor Farkas <gabor.farkas@gmail.com> * fix lint error Co-authored-by: Gábor Farkas <gabor.farkas@gmail.com>
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
import React, { PureComponent } from 'react';
|
||||
import { findHighlightChunksInText } from '@grafana/data';
|
||||
import ansicolor from 'ansicolor';
|
||||
import React, { PureComponent } from 'react';
|
||||
// @ts-ignore
|
||||
import Highlighter from 'react-highlight-words';
|
||||
|
||||
interface Style {
|
||||
[key: string]: string;
|
||||
@@ -26,6 +29,10 @@ function convertCSSToStyle(css: string): Style {
|
||||
|
||||
interface Props {
|
||||
value: string;
|
||||
highlight?: {
|
||||
searchWords: string[];
|
||||
highlightClassName: string;
|
||||
};
|
||||
}
|
||||
|
||||
interface State {
|
||||
@@ -62,14 +69,24 @@ export class LogMessageAnsi extends PureComponent<Props, State> {
|
||||
render() {
|
||||
const { chunks } = this.state;
|
||||
|
||||
return chunks.map((chunk, index) =>
|
||||
chunk.style ? (
|
||||
<span key={index} style={chunk.style} data-testid="ansiLogLine">
|
||||
{chunk.text}
|
||||
</span>
|
||||
return chunks.map((chunk, index) => {
|
||||
const chunkText = this.props.highlight?.searchWords ? (
|
||||
<Highlighter
|
||||
textToHighlight={chunk.text}
|
||||
searchWords={this.props.highlight.searchWords}
|
||||
findChunks={findHighlightChunksInText}
|
||||
highlightClassName={this.props.highlight.highlightClassName}
|
||||
/>
|
||||
) : (
|
||||
chunk.text
|
||||
)
|
||||
);
|
||||
);
|
||||
return chunk.style ? (
|
||||
<span key={index} style={chunk.style} data-testid="ansiLogLine">
|
||||
{chunkText}
|
||||
</span>
|
||||
) : (
|
||||
chunkText
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@@ -63,17 +63,19 @@ function renderLogMessage(
|
||||
) {
|
||||
const needsHighlighter =
|
||||
highlights && highlights.length > 0 && highlights[0] && highlights[0].length > 0 && entry.length < MAX_CHARACTERS;
|
||||
if (needsHighlighter) {
|
||||
const searchWords = highlights ?? [];
|
||||
if (hasAnsi) {
|
||||
const highlight = needsHighlighter ? { searchWords, highlightClassName } : undefined;
|
||||
return <LogMessageAnsi value={entry} highlight={highlight} />;
|
||||
} else if (needsHighlighter) {
|
||||
return (
|
||||
<Highlighter
|
||||
textToHighlight={entry}
|
||||
searchWords={highlights ?? []}
|
||||
searchWords={searchWords}
|
||||
findChunks={findHighlightChunksInText}
|
||||
highlightClassName={highlightClassName}
|
||||
/>
|
||||
);
|
||||
} else if (hasAnsi) {
|
||||
return <LogMessageAnsi value={entry} />;
|
||||
} else {
|
||||
return entry;
|
||||
}
|
||||
|
Reference in New Issue
Block a user