@ -1,8 +1,10 @@
import { findHighlightChunksInText } from '@grafana/data' ;
import { findHighlightChunksInText , GrafanaTheme2 } from '@grafana/data' ;
import ansicolor from 'ansicolor' ;
import React , { PureComponent } from 'react' ;
// @ts-ignore
import Highlighter from 'react-highlight-words' ;
import { withTheme2 } from '../../themes' ;
import { Themeable2 } from '../../types' ;
interface Style {
[ key : string ] : string ;
@ -13,13 +15,19 @@ interface ParsedChunk {
text : string ;
}
function convertCSSToStyle ( css : string ) : Style {
return css . split ( /;\s*/ ) . reduce ( ( accumulated , line ) = > {
function convertCSSToStyle ( theme : GrafanaTheme2 , css : string ) : Style {
return css . split ( /;\s*/ ) . reduce < Style > ( ( accumulated , line ) = > {
// The ansicolor package returns this color if the chunk has the ANSI dim
// style (`\e[2m`), but it is nearly unreadable in the dark theme, so we use
// GrafanaTheme2 instead to style it in a way that works across all themes.
if ( line === 'color:rgba(0,0,0,0.5)' ) {
return { color : theme.colors.text.secondary } ;
}
const match = line . match ( /([^:\s]+)\s*:\s*(.+)/ ) ;
if ( match && match [ 1 ] && match [ 2 ] ) {
const key = match [ 1 ] . replace ( /-([a-z])/g , ( _ , character ) = > character . toUpperCase ( ) ) ;
// @ts-ignore
accumulated [ key ] = match [ 2 ] ;
}
@ -27,7 +35,7 @@ function convertCSSToStyle(css: string): Style {
} , { } ) ;
}
interface Props {
interface Props extends Themeable2 {
value : string ;
highlight ? : {
searchWords : string [ ] ;
@ -40,7 +48,7 @@ interface State {
prevValue : string ;
}
export class LogMessageAnsi extends PureComponent < Props , State > {
export class UnThemed LogMessageAnsi extends PureComponent < Props , State > {
state : State = {
chunks : [ ] ,
prevValue : '' ,
@ -57,7 +65,7 @@ export class LogMessageAnsi extends PureComponent<Props, State> {
chunks : parsed.spans.map ( ( span ) = > {
return span . css
? {
style : convertCSSToStyle ( span . css ) ,
style : convertCSSToStyle ( props . theme , span . css ) ,
text : span.text ,
}
: { text : span.text } ;
@ -90,3 +98,6 @@ export class LogMessageAnsi extends PureComponent<Props, State> {
} ) ;
}
}
export const LogMessageAnsi = withTheme2 ( UnThemedLogMessageAnsi ) ;
LogMessageAnsi . displayName = 'LogMessageAnsi' ;