Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: syntax highlight for log viewer #394

Merged
merged 3 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/components/Monaco/EditorToolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { k6StudioLightBackground } from './themes/k6StudioLight'
import { useTheme } from '@/hooks/useTheme'
import { useLocalStorage } from 'react-use'
import { WordWrapIcon } from '../icons'
import { k6StudioDarkBackground } from './themes/k6StudioDark'

export type ToolbarState = {
wordWrap: 'on' | 'off'
Expand Down Expand Up @@ -34,7 +35,8 @@ export const EditorToolbar = ({ getState, actions }: EditorToolbarProps) => {
p="2"
justify="end"
style={{
backgroundColor: theme === 'dark' ? '#1e1e1e' : k6StudioLightBackground,
backgroundColor:
theme === 'dark' ? k6StudioDarkBackground : k6StudioLightBackground,
borderBottom: `1px solid ${theme === 'dark' ? 'var(--gray-6)' : 'var(--gray-4)'}`,
}}
>
Expand Down
2 changes: 1 addition & 1 deletion src/components/Monaco/ReactMonacoEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export function ReactMonacoEditor({
wordWrap: toolbarState.wordWrap,
}}
onMount={handleEditorMount}
theme={theme === 'dark' ? 'vs-dark' : 'k6-studio-light'}
theme={theme === 'dark' ? 'k6-studio-dark' : 'k6-studio-light'}
/>
</Flex>
)
Expand Down
13 changes: 13 additions & 0 deletions src/components/Monaco/languages/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as monaco from 'monaco-editor'

monaco.languages.register({ id: 'log' })
monaco.languages.setMonarchTokensProvider('log', {
tokenizer: {
root: [
[/\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{3}]/, 'timestamp'],
[/\[error]/, 'error'],
[/^\s*at.*$/, 'stackTrace'],
[/['"].*?['"]/, 'string'],
],
Comment on lines +6 to +11
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now, syntax highlighting is applied only to:

  • timestamps: [2025-01-01 00:00:00.000]
  • error keyword: [error]
  • stack trace line starting with at: at FSWatcher.emit (node:events:518:28)
  • strings: 'string' or "string"

},
})
18 changes: 18 additions & 0 deletions src/components/Monaco/themes/k6StudioDark.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as monaco from 'monaco-editor'
import '../languages/log'

export const k6StudioDarkBackground = '#1E1E1E'

monaco.editor.defineTheme('k6-studio-dark', {
base: 'vs-dark',
inherit: true,
rules: [
{ token: 'error.log', foreground: '#CE9178', fontStyle: 'bold' },
{ token: 'stackTrace.log', foreground: '#CE9178', fontStyle: 'italic' },
{ token: 'timestamp.log', foreground: '#368F2E' },
{ token: 'string.log', foreground: '#CE9178' },
],
colors: {
'editor.background': k6StudioDarkBackground,
},
})
8 changes: 7 additions & 1 deletion src/components/Monaco/themes/k6StudioLight.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import * as monaco from 'monaco-editor'
import '../languages/log'

export const k6StudioLightBackground = '#fafafa'

monaco.editor.defineTheme('k6-studio-light', {
base: 'vs',
inherit: true,
rules: [],
rules: [
{ token: 'error.log', foreground: '#A6201E', fontStyle: 'bold' },
{ token: 'stackTrace.log', foreground: '#A6201E', fontStyle: 'italic' },
{ token: 'timestamp.log', foreground: '#368F2E' },
{ token: 'string.log', foreground: '#A6201E' },
],
colors: {
'editor.background': k6StudioLightBackground,
},
Expand Down
3 changes: 2 additions & 1 deletion src/components/WebLogView/ResponseDetails/Preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ReactJson from '@microlink/react-json-view'
import { Font } from './Font'
import { useTheme } from '@/hooks/useTheme'
import { k6StudioLightBackground } from '@/components/Monaco/themes/k6StudioLight'
import { k6StudioDarkBackground } from '@/components/Monaco/themes/k6StudioDark'

interface PreviewProps {
content: string
Expand Down Expand Up @@ -78,7 +79,7 @@ const reactJsonStyles = {

const reactJsonDarkStyles = {
...reactJsonStyles,
background: '#1e1e1e',
background: k6StudioDarkBackground,
}

const reactJsonLightStyles = {
Expand Down
Loading