Skip to content

Commit d2ea5de

Browse files
committed
Add RBG formatting
1 parent f5ba3e6 commit d2ea5de

File tree

3 files changed

+77
-44
lines changed

3 files changed

+77
-44
lines changed

src/logs/formatProvider.ts

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,13 @@
1-
import { types } from "util";
2-
import * as vscode from "vscode";
3-
import {LogInfo} from "./model";
1+
import * as vscode from "vscode"
2+
import {LogInfo} from "./model"
3+
import {Parser, ColorToHex} from './parser'
44

55
const timestampRE = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{7}Z/;
66

77
const timestampDecorationType = vscode.window.createTextEditorDecorationType({
88
color: "#99999959"
99
});
1010

11-
const background = {
12-
"40": "#0c0c0c",
13-
"41": "#e74856",
14-
"42": "#16c60c",
15-
"43": "#f9f1a5",
16-
"44": "#0037da",
17-
"45": "#881798",
18-
"46": "#3a96dd",
19-
"47": "#cccccc",
20-
"100": "#767676"
21-
} as {[key: string]: string};
22-
23-
const foreground = {
24-
"30": "#0c0c0c",
25-
"31": "#e74856",
26-
"32": "#16c60c",
27-
"33": "#f9f1a5",
28-
"34": "#0037da",
29-
"35": "#881798",
30-
"36": "#3a96dd",
31-
"37": "#cccccc",
32-
"90": "#767676"
33-
} as {[key: string]: string};
34-
3511
export function updateDecorations(activeEditor: vscode.TextEditor, logInfo: LogInfo) {
3612
if (!activeEditor) {
3713
return;
@@ -56,7 +32,6 @@ export function updateDecorations(activeEditor: vscode.TextEditor, logInfo: LogI
5632
[key: string]: {type: vscode.TextEditorDecorationType; ranges: vscode.Range[]};
5733
} = {};
5834

59-
6035
for (let lineNo = 0; lineNo < logInfo.updatedLogLines.length; lineNo++) {
6136
// .filter() preserves the order of the array
6237
const lineStyles = logInfo.styleFormats.filter(style => style.line == lineNo)
@@ -67,21 +42,49 @@ export function updateDecorations(activeEditor: vscode.TextEditor, logInfo: LogI
6742
const range = new vscode.Range(lineNo, pos, lineNo, endPos);
6843
pos = endPos
6944

70-
// TODO build key by concatenating styles... or using style hash?
71-
const key = `mykey`
72-
if (!ctypes[key]) {
73-
ctypes[key] = {
74-
type: vscode.window.createTextEditorDecorationType({
75-
color: style.style?.fg,
76-
backgroundColor: style.style?.bg,
77-
fontWeight: style.style?.bold ? "bold" : "normal",
78-
fontStyle: style.style?.italic ? "italic" : "normal",
79-
textDecoration: style.style?.underline ? "underline" : ""
80-
}),
81-
ranges: [range]
82-
};
83-
} else {
84-
ctypes[key].ranges.push(range);
45+
if (style.style) {
46+
const key = Parser.styleKey(style.style)
47+
let fgHex = ""
48+
let bgHex = ""
49+
50+
// Convert to hex colors if RGB-formatted, or use lookup for predefined colors
51+
if (style.style.isFgRGB) {
52+
const rgbValues = style.style.fg.split(',')
53+
if (rgbValues.length == 3) {
54+
fgHex = "#"
55+
for (let i = 0; i < 3; i++) {
56+
fgHex.concat(parseInt(rgbValues[i]).toString(16))
57+
}
58+
}
59+
} else {
60+
fgHex = ColorToHex[style.style.fg]
61+
}
62+
if (style.style.isBgRGB) {
63+
const rgbValues = style.style.bg.split(',')
64+
if (rgbValues.length == 3) {
65+
bgHex = "#"
66+
for (let i = 0; i < 3; i++) {
67+
bgHex.concat(parseInt(rgbValues[i]).toString(16))
68+
}
69+
}
70+
} else {
71+
bgHex = ColorToHex[style.style.bg]
72+
}
73+
74+
if (!ctypes[key]) {
75+
ctypes[key] = {
76+
type: vscode.window.createTextEditorDecorationType({
77+
color: fgHex,
78+
backgroundColor: bgHex,
79+
fontWeight: style.style.bold ? "bold" : "normal",
80+
fontStyle: style.style.italic ? "italic" : "normal",
81+
textDecoration: style.style.underline ? "underline" : ""
82+
}),
83+
ranges: [range]
84+
};
85+
} else {
86+
ctypes[key].ranges.push(range);
87+
}
8588
}
8689
}
8790
}

src/logs/model.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const ansiColorRE = /\u001b\[(\d+;?)+m/gm;
12
const groupMarker = '##[group]';
23

34
import { Parser, IStyle } from './parser'
@@ -79,6 +80,9 @@ export function parseLog(log: string): LogInfo {
7980
});
8081
}
8182

83+
// Remove all other codes from the output, we don't support those
84+
lines[lineIdx] = line.replace(ansiColorRE, "");
85+
8286
++lineIdx;
8387
}
8488

src/logs/parser.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
21
// From azure pipelines UI.
32
// Class names have been changed to work with Primer styles
43
// Source: https://github.com/microsoft/azure-devops-ui/blob/22b5ae5969d405f4459caf9b020019e95bbded38/packages/azure-pipelines-ui/src/Utilities/Parser.ts#L1
@@ -112,6 +111,18 @@ const base8BitColors = {
112111
'7': 'w'
113112
} as Record<string, string>
114113

114+
export const ColorToHex = {
115+
'b': '#0c0c0c', // '30/40',
116+
'r': '#e74856', // '31/41',
117+
'g': '#16c60c', // '32/42',
118+
'y': '#f9f1a5', // '33/43',
119+
'bl': '#0037da', // '34/44',
120+
'm': '#881798', // '35/45',
121+
'c': '#3a96dd', // '36/46',
122+
'w': '#cccccc', // '37/47',
123+
'gr': '#767676' // '90/100'
124+
} as Record<string, string>
125+
115126
//0-255 in 6 increments, used to generate 216 equally incrementing colors
116127
const colorIncrements216 = {
117128
0: 0,
@@ -384,6 +395,21 @@ export class Parser {
384395
return result
385396
}
386397

398+
/**
399+
* Gets a unique key for each style
400+
* @param style style to get key for
401+
* @returns a string that is guaranteed to be unique for every different style
402+
*/
403+
public static styleKey(style: IStyle): string {
404+
const fg = style.fg ?? "-"
405+
const bg = style.bg ?? "-"
406+
const bold = style.bold ? "b" : "n"
407+
const ital = style.italic ? "i" : "n"
408+
const underline = style.underline ? "u" : "n"
409+
// May produce two different strings for the same style if using an RBG that matches a predefined color code
410+
return fg + bg + bold + ital + underline
411+
}
412+
387413
/**
388414
* With 8 bit colors, from 16-256, rgb color combinations are used
389415
* 16-231 (216 colors) is a 6 x 6 x 6 color cube

0 commit comments

Comments
 (0)