-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy paththemes.ts
More file actions
196 lines (169 loc) · 5.09 KB
/
themes.ts
File metadata and controls
196 lines (169 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import * as assert from 'assert';
import * as path from 'path';
import * as fs from 'fs';
import * as os from 'os';
import * as shiki from 'shiki';
/**
* Colors are always specified as hex strings
*/
type Color = string;
export type ColorModifier =
| 'reset'
| 'bold'
| 'dim'
| 'italic'
| 'underline'
| 'inverse'
| 'hidden'
| 'strikethrough'
| 'visible';
export type ColorDefinition = {
color?: Color;
backgroundColor?: Color;
modifiers?: ColorModifier[];
};
export enum ThemeColorName {
DEFAULT_COLOR = 'DEFAULT_COLOR',
COMMIT_HEADER_LABEL_COLOR = 'COMMIT_HEADER_LABEL_COLOR',
COMMIT_HEADER_COLOR = 'COMMIT_HEADER_COLOR',
COMMIT_SHA_COLOR = 'COMMIT_SHA_COLOR',
COMMIT_AUTHOR_COLOR = 'COMMIT_AUTHOR_COLOR',
COMMIT_DATE_COLOR = 'COMMIT_DATE_COLOR',
COMMIT_TITLE_COLOR = 'COMMIT_TITLE_COLOR',
COMMIT_MESSAGE_COLOR = 'COMMIT_MESSAGE_COLOR',
BORDER_COLOR = 'BORDER_COLOR',
FILE_NAME_COLOR = 'FILE_NAME_COLOR',
HUNK_HEADER_COLOR = 'HUNK_HEADER_COLOR',
DELETED_WORD_COLOR = 'DELETED_WORD_COLOR',
DELETED_LINE_COLOR = 'DELETED_LINE_COLOR',
DELETED_LINE_NO_COLOR = 'DELETED_LINE_NO_COLOR',
INSERTED_WORD_COLOR = 'INSERTED_WORD_COLOR',
INSERTED_LINE_COLOR = 'INSERTED_LINE_COLOR',
INSERTED_LINE_NO_COLOR = 'INSERTED_LINE_NO_COLOR',
UNMODIFIED_LINE_COLOR = 'UNMODIFIED_LINE_COLOR',
UNMODIFIED_LINE_NO_COLOR = 'UNMODIFIED_LINE_NO_COLOR',
MISSING_LINE_COLOR = 'MISSING_LINE_COLOR',
}
export type ThemeDefinition = {
SYNTAX_HIGHLIGHTING_THEME?: shiki.BundledTheme;
} & {
[key in ThemeColorName]: ColorDefinition;
};
type ColorRgba = {
r: number;
g: number;
b: number;
a: number;
};
export type ThemeColor = {
color?: ColorRgba;
backgroundColor?: ColorRgba;
modifiers?: ColorModifier[];
};
/**
* The hex string is of the format #rrggbb(aa)
*/
function hexToRgba(hex: string): ColorRgba {
assert.ok(hex.length === 7 || hex.length === 9 || hex.length === 4, hex);
hex = hex.slice(1);
let hexNo = parseInt(hex, 16);
const bits = hex.length === 3 ? 4 : 8;
let a = 255;
if (hex.length === 8) {
a = hexNo & 0xff;
hexNo >>>= bits;
}
const b = hexNo & 0xff;
hexNo >>>= bits;
const g = hexNo & 0xff;
hexNo >>>= bits;
const r = hexNo & 0xff;
return { r, g, b, a };
}
export function mergeColors(
a?: ColorRgba,
b?: ColorRgba
): ColorRgba | undefined {
if (!b || b.a === 0) {
return a;
}
if (!a) {
return b;
}
const t = 1.0 - b.a / 255.0;
return {
r: b.r * (1 - t) + a.r * t,
g: b.g * (1 - t) + a.g * t,
b: b.b * (1 - t) + a.b * t,
a: b.a * (1 - t) + a.a * t,
};
}
function mergeModifiers(
a: ColorModifier[] | undefined,
b: ColorModifier[] | undefined
): ColorModifier[] | undefined {
if (a && b) {
return a.concat(b);
}
return a ?? b;
}
function mergeThemeColors(a: ThemeColor, b: ThemeColor): ThemeColor {
return {
color: mergeColors(a.color, b.color),
backgroundColor: mergeColors(a.backgroundColor, b.backgroundColor),
modifiers: mergeModifiers(a.modifiers, b.modifiers),
};
}
export function reduceThemeColors(colors: ThemeColor[]): ThemeColor {
// Colors are applied in reverse, which allows us to do apply specific
// formatting (like syntax highlighting) first and apply more generic colors
// (like line colors) last
let themeColor: ThemeColor = {};
for (let i = colors.length - 1; i >= 0; i--) {
themeColor = mergeThemeColors(themeColor, colors[i]);
}
return themeColor;
}
export type Theme = {
SYNTAX_HIGHLIGHTING_THEME?: shiki.BundledTheme;
} & {
[key in ThemeColorName]: ThemeColor;
};
export function parseColorDefinition(definition: ColorDefinition): ThemeColor {
return {
color: definition.color ? hexToRgba(definition.color) : undefined,
backgroundColor: definition.backgroundColor
? hexToRgba(definition.backgroundColor)
: undefined,
modifiers: definition.modifiers,
};
}
function expandHomeDir(path: string) {
const homeDir = os.homedir()
return path.replace('~', homeDir).replace('$HOME', homeDir)
}
function loadThemeDefinition(
themesDir: string,
themeName: string
): ThemeDefinition {
return JSON.parse(
fs.readFileSync(
expandHomeDir(path.join(themesDir, `${themeName}.json`))
).toString()
) as ThemeDefinition;
}
export function loadTheme(themesDir: string, themeName: string): Theme {
const themeDefinition = loadThemeDefinition(themesDir, themeName);
const theme: Partial<Theme> = {
SYNTAX_HIGHLIGHTING_THEME: themeDefinition.SYNTAX_HIGHLIGHTING_THEME,
};
const themeColorNames = Object.keys(ThemeColorName) as ThemeColorName[];
for (const variableName of themeColorNames) {
const value = themeDefinition[variableName];
if (!value) {
assert.fail(`${variableName} is missing in theme`);
}
theme[variableName] = parseColorDefinition(value);
}
return theme as Theme;
}