-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparams.ts
More file actions
118 lines (103 loc) · 3.48 KB
/
params.ts
File metadata and controls
118 lines (103 loc) · 3.48 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
import { defaultTheme } from "./theme.ts";
interface Params {
title: string;
topLineTexts: string[];
topLineTextColor: string;
topLineItemColors: string[];
topLineBgText: string | null;
topLineBgColor: string;
bgColor: string;
promptColor: string;
commandColor: string;
titleColor: string;
cursorColor: string;
bottomLineTexts: string[];
bottomLineTextColor: string;
bottomLineItemColors: string[];
bottomLineBgText: string;
bottomLineBgColor: string;
}
type ColorParams = Pick<Params, "cursorColor" | "promptColor" | "bgColor" | "titleColor" | "commandColor">;
type StatusLineColorParams = Pick<
Params,
| "topLineBgColor"
| "topLineTextColor"
| "bottomLineBgColor"
| "bottomLineTextColor"
| "topLineItemColors"
| "bottomLineItemColors"
>;
const isValidColorCode = (str: string) => {
return /^([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/.test(str);
};
const getColor = (url: URL, key: string) => {
const param = url.searchParams.get(key) ?? "";
return isValidColorCode(param) ? `#${param}` : null;
};
const getColors = (url: URL, key: string) => {
const param = url.searchParams.get(key) ?? null;
if (!param) {
return null;
}
return param.split(",").filter((p) => isValidColorCode(p)).map((p) => `#${p}`);
};
const snakeToCamel = (s: string) => {
return s.replace(/(_\w)/g, function (m) {
return m[1].toUpperCase();
});
};
const getParams = (url: URL): Params => {
const bottomLineTextsParam = url.searchParams.get("bottom_line_texts");
const topLineTextsParam = url.searchParams.get("top_line_texts");
const bottomLineTexts = bottomLineTextsParam ? bottomLineTextsParam.split(",") : [];
const topLineTexts = topLineTextsParam ? topLineTextsParam.split(",") : ["swfz", "til"];
const colorParams = ["cursor_color", "prompt_color", "bg_color", "title_color", "command_color"].reduce(
(acc, key) => {
const k = snakeToCamel(key);
if (k === "cursorColor" || k === "promptColor" || k === "bgColor" || k === "titleColor" || k === "commandColor") {
acc[k] = getColor(url, key) ?? defaultTheme[k];
}
return acc;
},
{} as ColorParams,
);
const statusLineColorParams = [
"top_line_bg_color",
"top_line_text_color",
"bottom_line_bg_color",
"bottom_line_text_color",
"top_line_item_colors",
"bottom_line_item_colors",
].reduce((acc, key) => {
const statusLine = key.includes("top_") ? "topStatusLine" : "bottomStatusLine";
if (key.endsWith("_colors")) {
const k = snakeToCamel(key);
if (k === "topLineItemColors" || k === "bottomLineItemColors") {
acc[k] = getColors(url, key) ?? defaultTheme[statusLine].itemColors;
}
} else {
const k = snakeToCamel(key.split("_").slice(2).join("_"));
if (k === "textColor" || k === "bgColor") {
const paramKey = snakeToCamel(key);
if (
paramKey === "topLineBgColor" || paramKey === "bottomLineBgColor" || paramKey === "topLineTextColor" ||
paramKey === "bottomLineTextColor"
) {
acc[paramKey] = getColor(url, key) ?? defaultTheme[statusLine][k];
}
}
}
return acc;
}, {} as StatusLineColorParams);
return {
topLineTexts,
bottomLineTexts,
...colorParams,
...statusLineColorParams,
title: url.searchParams.get("title") ?? "No Title",
bottomLineBgText: url.searchParams.get("bottom_line_bg_text") ?? "Tags",
topLineBgText: url.searchParams.get("top_line_bg_text") ?? null,
};
};
export { getParams };
export type { Params };