From 32f2a75c4413044702ca21b15398bee8d736f83a Mon Sep 17 00:00:00 2001 From: Oliver Frye <11541660+oliverfrye@users.noreply.github.com> Date: Fri, 3 Dec 2021 21:35:16 +1000 Subject: [PATCH] Explore: Fix ANSI dim style being unreadable in dark mode (#41226) * Explore: Fix ANSI dim style being unreadable in dark mode * use GrafanaTheme2 instead of opacity * tweak code and comment * fix existing tests and add new test for ANSI dim code * fix failing test --- .../components/Logs/LogMessageAnsi.test.tsx | 27 +++++++++++++------ .../src/components/Logs/LogMessageAnsi.tsx | 25 ++++++++++++----- 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/packages/grafana-ui/src/components/Logs/LogMessageAnsi.test.tsx b/packages/grafana-ui/src/components/Logs/LogMessageAnsi.test.tsx index 4764c87abe6..4dac18ce2e8 100644 --- a/packages/grafana-ui/src/components/Logs/LogMessageAnsi.test.tsx +++ b/packages/grafana-ui/src/components/Logs/LogMessageAnsi.test.tsx @@ -1,19 +1,18 @@ -import React from 'react'; +import { createTheme } from '@grafana/data'; import { shallow } from 'enzyme'; - -import { LogMessageAnsi } from './LogMessageAnsi'; +import React from 'react'; +import { UnThemedLogMessageAnsi as LogMessageAnsi } from './LogMessageAnsi'; describe('', () => { it('renders string without ANSI codes', () => { - const wrapper = shallow(); + const wrapper = shallow(); expect(wrapper.find('span').exists()).toBe(false); expect(wrapper.text()).toBe('Lorem ipsum'); }); - it('renders string with ANSI codes', () => { const value = 'Lorem \u001B[31mipsum\u001B[0m et dolor'; - const wrapper = shallow(); + const wrapper = shallow(); expect(wrapper.find('span')).toHaveLength(1); expect(wrapper.find('span').first().prop('style')).toMatchObject( @@ -24,8 +23,8 @@ describe('', () => { expect(wrapper.find('span').first().text()).toBe('ipsum'); }); it('renders string with ANSI codes with correctly converted css classnames', () => { - const value = 'Lorem Ipsum'; - const wrapper = shallow(); + const value = 'Lorem \u001B[1;32mIpsum'; + const wrapper = shallow(); expect(wrapper.find('span')).toHaveLength(1); expect(wrapper.find('span').first().prop('style')).toMatchObject( @@ -34,4 +33,16 @@ describe('', () => { }) ); }); + it('renders string with ANSI dim code with appropriate themed color', () => { + const value = 'Lorem \u001B[1;2mIpsum'; + const theme = createTheme(); + const wrapper = shallow(); + + expect(wrapper.find('span')).toHaveLength(1); + expect(wrapper.find('span').first().prop('style')).toMatchObject( + expect.objectContaining({ + color: theme.colors.text.secondary, + }) + ); + }); }); diff --git a/packages/grafana-ui/src/components/Logs/LogMessageAnsi.tsx b/packages/grafana-ui/src/components/Logs/LogMessageAnsi.tsx index ccf5267bffa..478b4f68236 100644 --- a/packages/grafana-ui/src/components/Logs/LogMessageAnsi.tsx +++ b/packages/grafana-ui/src/components/Logs/LogMessageAnsi.tsx @@ -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