11import * as vscode from "vscode" ;
22import { LogInfo } from "./model" ;
3+ import { Parser , VSCodeDefaultColors } from "./parser" ;
34
45const timestampRE = / \d { 4 } - \d { 2 } - \d { 2 } T \d { 2 } : \d { 2 } : \d { 2 } .\d { 7 } Z / ;
56
67const timestampDecorationType = vscode . window . createTextEditorDecorationType ( {
78 color : "#99999959"
89} ) ;
910
10- const background = {
11- "40" : "#0c0c0c" ,
12- "41" : "#e74856" ,
13- "42" : "#16c60c" ,
14- "43" : "#f9f1a5" ,
15- "44" : "#0037da" ,
16- "45" : "#881798" ,
17- "46" : "#3a96dd" ,
18- "47" : "#cccccc" ,
19- "100" : "#767676"
20- } as { [ key : string ] : string } ;
21-
22- const foreground = {
23- "30" : "#0c0c0c" ,
24- "31" : "#e74856" ,
25- "32" : "#16c60c" ,
26- "33" : "#f9f1a5" ,
27- "34" : "#0037da" ,
28- "35" : "#881798" ,
29- "36" : "#3a96dd" ,
30- "37" : "#cccccc" ,
31- "90" : "#767676"
32- } as { [ key : string ] : string } ;
33-
3411export function updateDecorations ( activeEditor : vscode . TextEditor , logInfo : LogInfo ) {
3512 if ( ! activeEditor ) {
3613 return ;
@@ -50,30 +27,70 @@ export function updateDecorations(activeEditor: vscode.TextEditor, logInfo: LogI
5027 } ) )
5128 ) ;
5229
53- // Custom colors
54- const ctypes : {
30+ // Custom decorations
31+ const decoratorTypes : {
5532 [ key : string ] : { type : vscode . TextEditorDecorationType ; ranges : vscode . Range [ ] } ;
5633 } = { } ;
5734
58- for ( const colorFormat of logInfo . colorFormats ) {
59- const range = new vscode . Range ( colorFormat . line , colorFormat . start , colorFormat . line , colorFormat . end ) ;
35+ for ( let lineNo = 0 ; lineNo < logInfo . updatedLogLines . length ; lineNo ++ ) {
36+ // .filter() preserves the order of the array
37+ const lineStyles = logInfo . styleFormats . filter ( style => style . line == lineNo ) ;
38+ let pos = 0 ;
39+ for ( let styleNo = 0 ; styleNo < lineStyles . length ; styleNo ++ ) {
40+ const styleInfo = lineStyles [ styleNo ] ;
41+ const endPos = pos + styleInfo . content . length ;
42+ const range = new vscode . Range ( lineNo , pos , lineNo , endPos ) ;
43+ pos = endPos ;
44+
45+ if ( styleInfo . style ) {
46+ const key = Parser . styleKey ( styleInfo . style ) ;
47+ let fgHex = "" ;
48+ let bgHex = "" ;
49+
50+ // Convert to hex colors if RGB-formatted, or use lookup for predefined colors
51+ if ( styleInfo . style . isFgRGB ) {
52+ const rgbValues = styleInfo . style . fg . split ( "," ) ;
53+ fgHex = rgbToHex ( rgbValues ) ;
54+ } else {
55+ fgHex = VSCodeDefaultColors [ styleInfo . style . fg ] ?? "" ;
56+ }
57+ if ( styleInfo . style . isBgRGB ) {
58+ const rgbValues = styleInfo . style . bg . split ( "," ) ;
59+ bgHex = rgbToHex ( rgbValues ) ;
60+ } else {
61+ bgHex = VSCodeDefaultColors [ styleInfo . style . bg ] ?? "" ;
62+ }
6063
61- // TODO default to real colors
62- const key = `${ colorFormat . color . foreground || "" } -${ colorFormat . color . background || "" } ` ;
63- if ( ! ctypes [ key ] ) {
64- ctypes [ key ] = {
65- type : vscode . window . createTextEditorDecorationType ( {
66- color : colorFormat . color . foreground && foreground [ colorFormat . color . foreground ] ,
67- backgroundColor : colorFormat . color . background && background [ colorFormat . color . background ]
68- } ) ,
69- ranges : [ range ]
70- } ;
71- } else {
72- ctypes [ key ] . ranges . push ( range ) ;
64+ if ( ! decoratorTypes [ key ] ) {
65+ decoratorTypes [ key ] = {
66+ type : vscode . window . createTextEditorDecorationType ( {
67+ color : fgHex ,
68+ backgroundColor : bgHex ,
69+ fontWeight : styleInfo . style . bold ? "bold" : "normal" ,
70+ fontStyle : styleInfo . style . italic ? "italic" : "normal" ,
71+ textDecoration : styleInfo . style . underline ? "underline" : ""
72+ } ) ,
73+ ranges : [ range ]
74+ } ;
75+ } else {
76+ decoratorTypes [ key ] . ranges . push ( range ) ;
77+ }
78+ }
7379 }
7480 }
7581
76- for ( const ctype of Object . values ( ctypes ) ) {
77- activeEditor . setDecorations ( ctype . type , ctype . ranges ) ;
82+ for ( const decoratorType of Object . values ( decoratorTypes ) ) {
83+ activeEditor . setDecorations ( decoratorType . type , decoratorType . ranges ) ;
84+ }
85+ }
86+
87+ function rgbToHex ( rgbValues : string [ ] ) {
88+ let hex = "" ;
89+ if ( rgbValues . length == 3 ) {
90+ hex = "#" ;
91+ for ( let i = 0 ; i < 3 ; i ++ ) {
92+ hex = hex . concat ( parseInt ( rgbValues [ i ] ) . toString ( 16 ) . padStart ( 2 , "0" ) ) ;
93+ }
7894 }
95+ return hex ;
7996}
0 commit comments