@timmo001/groundwork-tui 0.10.11 → 0.10.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -8,11 +8,14 @@ look and keys.
8
8
  import { AppHeader, formatHelpBar, loadTheme } from "@timmo001/groundwork-tui";
9
9
  ```
10
10
 
11
- - `loadTheme` reads `GW_THEME` and `GW_MODE`. Without `GW_THEME` it's the
12
- desktop's Omarchy theme where there is one, otherwise Base and dark.
13
- `themeFor` picks a theme and mode directly, and `omarchyThemeFor` maps an
14
- Omarchy `colors.toml`. `fromRoles` takes any theme's terminal roles, such as
15
- an app's own theme solved with `runtimeTheme` and `hexModes`.
11
+ - `loadTheme` reads `GW_THEME`, `GW_MODE` and `GW_TRANSPARENT`. `GW_THEME`
12
+ is a theme's id, `omarchy`, or a path to a theme document or an Omarchy
13
+ `colors.toml`; without it it's the desktop's Omarchy theme where there is
14
+ one, otherwise Base and dark. `loadThemeWith(themes)` adds an app's own
15
+ themes by id. `themeFor` picks one of Groundwork's directly,
16
+ `themeFromDocument` places any theme document, `themeFromFile` reads one,
17
+ and `omarchyThemeFor` maps an Omarchy `colors.toml`. `fromRoles` takes any
18
+ theme's terminal roles. Each takes `{ transparent }`.
16
19
  - `AppHeader` and `PaneHeader` are one-row titles. `HeaderBlock` is the
17
20
  app's name as ASCII art over `formatHeaderBar`, a breadcrumb with a status
18
21
  dot and details that drop off when the terminal is narrow.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@timmo001/groundwork-tui",
3
- "version": "0.10.11",
3
+ "version": "0.10.12",
4
4
  "description": "Groundwork's terminal components: OpenTUI renderables on the terminal tokens",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -29,8 +29,9 @@
29
29
  "format:check": "prettier --check ."
30
30
  },
31
31
  "dependencies": {
32
+ "@effect/platform-node": "4.1.2",
32
33
  "@opentui/core": "0.8.2",
33
- "@timmo001/groundwork-tokens": "0.10.11",
34
+ "@timmo001/groundwork-tokens": "0.10.12",
34
35
  "effect": "4.1.2",
35
36
  "fuse.js": "7.5.0"
36
37
  },
package/src/index.ts CHANGED
@@ -77,12 +77,19 @@ export {
77
77
  DEFAULT_THEME,
78
78
  fromRoles,
79
79
  loadTheme,
80
+ loadThemeWith,
80
81
  omarchyThemeFor,
81
82
  resolveTheme,
83
+ ThemeFileError,
82
84
  themeFor,
85
+ themeFromDocument,
86
+ themeFromFile,
83
87
  themeOptions,
88
+ themeOptionsWith,
84
89
  type RoleName,
85
90
  type Theme,
91
+ type ThemeDocument,
92
+ type ThemeOptions,
86
93
  type ThemeOption,
87
94
  type ThemePick,
88
95
  } from "./theme.ts";
@@ -0,0 +1,104 @@
1
+ /**
2
+ * Tests loading a theme that isn't one of the generated ones: a theme file
3
+ * by path, an app's own document by id and from `GW_THEME`, and the
4
+ * transparent option.
5
+ *
6
+ * @module
7
+ */
8
+ import { afterEach, expect, test } from "bun:test";
9
+ import { fileURLToPath } from "node:url";
10
+ import { parseTheme } from "@timmo001/groundwork-tokens/runtime";
11
+ import { Effect, Result } from "effect";
12
+ import {
13
+ DEFAULT_THEME,
14
+ loadThemeWith,
15
+ resolveTheme,
16
+ themeFor,
17
+ themeFromDocument,
18
+ themeOptionsWith,
19
+ } from "./theme.ts";
20
+
21
+ const glazingFile = fileURLToPath(
22
+ import.meta.resolve("@timmo001/groundwork-tokens/themes/glazing.json"),
23
+ );
24
+
25
+ const kiln = Result.getOrThrow(
26
+ parseTheme(
27
+ JSON.stringify({
28
+ kind: "solved",
29
+ id: "kiln",
30
+ name: "Kiln",
31
+ description: "Warm clay and ember orange.",
32
+ background: "#1f1a17",
33
+ accent: "#e08a4f",
34
+ style: {
35
+ font: "Inter",
36
+ mono: "JetBrains Mono",
37
+ radius: { panel: 6, control: 4, chip: 4 },
38
+ border: { width: 1, strength: "soft" },
39
+ weights: { regular: 400, strong: 600 },
40
+ density: "compact",
41
+ capitals: false,
42
+ link: { color: "accent", underline: "thin" },
43
+ surfaceAccent: "none",
44
+ rows: "dense",
45
+ },
46
+ }),
47
+ ),
48
+ );
49
+
50
+ afterEach(() => {
51
+ delete process.env.GW_THEME;
52
+ delete process.env.GW_MODE;
53
+ delete process.env.GW_TRANSPARENT;
54
+ });
55
+
56
+ test("a theme file by path matches the generated theme", async () => {
57
+ const fromFile = await Effect.runPromise(
58
+ resolveTheme({ theme: glazingFile, mode: "light" }),
59
+ );
60
+
61
+ expect(fromFile).toEqual(themeFor("glazing", "light"));
62
+ });
63
+
64
+ test("a theme file that isn't there falls back to the default", async () => {
65
+ const theme = await Effect.runPromise(
66
+ resolveTheme({ theme: "./no-such-theme.json" }),
67
+ );
68
+
69
+ expect(theme).toEqual(DEFAULT_THEME);
70
+ });
71
+
72
+ test("an app's own theme by id, in its own mode, and transparent", async () => {
73
+ const theme = await Effect.runPromise(
74
+ resolveTheme({ theme: "kiln", transparent: true }, [kiln]),
75
+ );
76
+
77
+ expect(theme).toEqual(
78
+ themeFromDocument(kiln, undefined, { transparent: true }),
79
+ );
80
+ expect(theme.transparent).toBe(true);
81
+ expect(theme.accent).not.toBe(DEFAULT_THEME.accent);
82
+ });
83
+
84
+ test("GW_THEME takes a path and GW_TRANSPARENT the option", async () => {
85
+ process.env.GW_THEME = glazingFile;
86
+ process.env.GW_MODE = "darker";
87
+ process.env.GW_TRANSPARENT = "true";
88
+
89
+ const theme = await Effect.runPromise(loadThemeWith());
90
+
91
+ expect(theme).toEqual({
92
+ ...themeFor("glazing", "darker"),
93
+ transparent: true,
94
+ });
95
+ });
96
+
97
+ test("the picker lists the app's themes first, then the optional ones too", async () => {
98
+ const options = await Effect.runPromise(themeOptionsWith([kiln]));
99
+ const ids = options.map((option) => option.id);
100
+
101
+ expect(ids[0]).toBe("kiln");
102
+ expect(ids).toContain("catppuccin-mocha");
103
+ expect(options[0]?.modes).toEqual(["light", "dark", "darker"]);
104
+ });
package/src/theme.ts CHANGED
@@ -6,15 +6,29 @@
6
6
  *
7
7
  * @module
8
8
  */
9
+ import { NodeServices } from "@effect/platform-node";
10
+ import type { Theme as ThemeDocument } from "@timmo001/groundwork-tokens";
11
+ import {
12
+ coreThemeChoices,
13
+ optionalThemeChoices,
14
+ } from "@timmo001/groundwork-tokens/choices";
9
15
  import { readOmarchyTheme } from "@timmo001/groundwork-tokens/omarchy-current";
10
16
  import { opentuiThemes } from "@timmo001/groundwork-tokens/opentui";
11
- import { coreThemeChoices } from "@timmo001/groundwork-tokens/page";
12
17
  import {
13
18
  hexModes,
14
19
  omarchyTheme,
20
+ parseTheme,
15
21
  runtimeTheme,
16
22
  } from "@timmo001/groundwork-tokens/runtime";
17
- import { Config, Effect, Option, Result } from "effect";
23
+ import {
24
+ Config,
25
+ Console,
26
+ Effect,
27
+ FileSystem,
28
+ Option,
29
+ Result,
30
+ Schema,
31
+ } from "effect";
18
32
 
19
33
  /** Semantic color slots for the TUI */
20
34
  export interface Theme {
@@ -55,12 +69,21 @@ type ThemeId = keyof typeof opentuiThemes;
55
69
  /** A terminal role's name, such as `surface-lower` or `accent`. */
56
70
  export type RoleName = keyof (typeof opentuiThemes)["base"]["dark"];
57
71
 
72
+ /** How a theme draws, beside its colors. */
73
+ export interface ThemeOptions {
74
+ /** Panels leave their backgrounds unpainted, so the terminal's shows through. */
75
+ readonly transparent?: boolean | undefined;
76
+ }
77
+
58
78
  /**
59
79
  * Places a theme's terminal roles into the TUI's slots, such as an app's own
60
80
  * theme's from `hexModes(runtimeTheme(document).theme)` in one mode. Base's
61
81
  * dark fills any it lacks.
62
82
  */
63
- export const fromRoles = (roles: Partial<Record<RoleName, string>>): Theme => {
83
+ export const fromRoles = (
84
+ roles: Partial<Record<RoleName, string>>,
85
+ options: ThemeOptions = {},
86
+ ): Theme => {
64
87
  const role = (name: RoleName) => roles[name] ?? opentuiThemes.base.dark[name];
65
88
 
66
89
  return {
@@ -78,7 +101,7 @@ export const fromRoles = (roles: Partial<Record<RoleName, string>>): Theme => {
78
101
  green: role("success"),
79
102
  red: role("danger"),
80
103
  yellow: role("warning"),
81
- transparent: false,
104
+ transparent: options.transparent ?? false,
82
105
  };
83
106
  };
84
107
 
@@ -87,18 +110,43 @@ const isTheme = (id: string): id is ThemeId => id in opentuiThemes;
87
110
  /** Base, dark: the default when nothing is picked. */
88
111
  export const DEFAULT_THEME: Theme = fromRoles(opentuiThemes.base.dark);
89
112
 
113
+ export type { ThemeDocument };
114
+
90
115
  /**
91
- * A core theme's slots in one mode, or Base and dark when there's no such
92
- * theme or mode.
116
+ * A Groundwork theme's slots in one mode, core or optional, or Base and dark
117
+ * when there's no such theme or mode.
93
118
  */
94
- export const themeFor = (id: string, mode: string): Theme => {
95
- if (!isTheme(id)) return DEFAULT_THEME;
119
+ export const themeFor = (
120
+ id: string,
121
+ mode: string,
122
+ options: ThemeOptions = {},
123
+ ): Theme => {
124
+ const roles = isTheme(id)
125
+ ? Object.entries(opentuiThemes[id]).find(([name]) => name === mode)?.[1]
126
+ : undefined;
96
127
 
97
- const roles = Object.entries(opentuiThemes[id]).find(
98
- ([name]) => name === mode,
99
- )?.[1];
128
+ return fromRoles(roles ?? opentuiThemes.base.dark, options);
129
+ };
130
+
131
+ /**
132
+ * Any theme document's slots, such as an app's own: solved, then placed in
133
+ * `mode`, or in the theme's own mode when it doesn't have that one, then
134
+ * dark.
135
+ */
136
+ export const themeFromDocument = (
137
+ document: ThemeDocument,
138
+ mode?: string,
139
+ options: ThemeOptions = {},
140
+ ): Theme => {
141
+ const modes = hexModes(runtimeTheme(document).theme);
100
142
 
101
- return roles === undefined ? DEFAULT_THEME : fromRoles(roles);
143
+ const roles =
144
+ (mode === undefined ? undefined : modes[mode]) ??
145
+ (document.kind === "mapped" ? modes[document.mode] : undefined) ??
146
+ modes.dark ??
147
+ modes.light;
148
+
149
+ return fromRoles(roles ?? opentuiThemes.base.dark, options);
102
150
  };
103
151
 
104
152
  /**
@@ -108,19 +156,53 @@ export const themeFor = (id: string, mode: string): Theme => {
108
156
  export const omarchyThemeFor = (
109
157
  colors: string,
110
158
  mode?: string,
159
+ options: ThemeOptions = {},
111
160
  ): Theme | undefined => {
112
161
  const parsed = omarchyTheme(colors);
113
162
 
114
- if (Result.isFailure(parsed)) return undefined;
163
+ return Result.isFailure(parsed)
164
+ ? undefined
165
+ : themeFromDocument(parsed.success, mode, options);
166
+ };
115
167
 
116
- const modes = hexModes(runtimeTheme(parsed.success).theme);
168
+ export class ThemeFileError extends Schema.TaggedError<ThemeFileError>()(
169
+ "ThemeFileError",
170
+ { message: Schema.String },
171
+ ) {}
117
172
 
118
- const roles =
119
- (mode === undefined ? undefined : modes[mode]) ??
120
- modes[parsed.success.mode];
173
+ /**
174
+ * A theme file's slots: a theme document's `.json`, or an Omarchy
175
+ * `colors.toml`, placed as `themeFromDocument` places it.
176
+ */
177
+ export const themeFromFile = Effect.fn("themeFromFile")(function* (
178
+ path: string,
179
+ mode?: string,
180
+ options: ThemeOptions = {},
181
+ ) {
182
+ const fs = yield* FileSystem.FileSystem;
121
183
 
122
- return roles === undefined ? undefined : fromRoles(roles);
123
- };
184
+ const text = yield* fs
185
+ .readFileString(path)
186
+ .pipe(
187
+ Effect.mapError(
188
+ (error) => new ThemeFileError({ message: `${path}: ${error.message}` }),
189
+ ),
190
+ );
191
+
192
+ const parsed = path.endsWith(".toml") ? omarchyTheme(text) : parseTheme(text);
193
+
194
+ if (Result.isFailure(parsed)) {
195
+ return yield* new ThemeFileError({
196
+ message: `${path} isn't a theme document or an Omarchy colors.toml`,
197
+ });
198
+ }
199
+
200
+ return themeFromDocument(parsed.success, mode, options);
201
+ });
202
+
203
+ /** Whether a pick's theme names a file rather than a theme's id. */
204
+ const isThemeFile = (theme: string) =>
205
+ theme.includes("/") || theme.endsWith(".json") || theme.endsWith(".toml");
124
206
 
125
207
  /** A theme a TUI can offer in a picker, with the modes it has. */
126
208
  export interface ThemeOption {
@@ -129,23 +211,33 @@ export interface ThemeOption {
129
211
  readonly modes: readonly string[];
130
212
  }
131
213
 
214
+ const modesOf = (id: string) =>
215
+ Object.keys(isTheme(id) ? opentuiThemes[id] : {});
216
+
132
217
  /**
133
- * The themes to offer in a picker: the desktop's Omarchy theme first where
134
- * there is one, then the core themes.
218
+ * The themes to offer in a picker: the app's own `themes` first, then the
219
+ * desktop's Omarchy theme where there is one, then Groundwork's core and
220
+ * optional themes. Pass the same `themes` to `resolveTheme`.
135
221
  */
136
- export const themeOptions: Effect.Effect<readonly ThemeOption[]> = Effect.gen(
137
- function* () {
138
- const core = coreThemeChoices.map(({ id, name }) => ({
139
- id,
140
- name,
141
- modes: Object.keys(isTheme(id) ? opentuiThemes[id] : {}),
222
+ export const themeOptionsWith = (
223
+ themes: readonly ThemeDocument[] = [],
224
+ ): Effect.Effect<readonly ThemeOption[]> =>
225
+ Effect.gen(function* () {
226
+ const own = themes.map((document) => ({
227
+ id: document.id,
228
+ name: document.name,
229
+ modes: Object.keys(hexModes(runtimeTheme(document).theme)),
142
230
  }));
143
231
 
232
+ const groundwork = [...coreThemeChoices, ...optionalThemeChoices].map(
233
+ ({ id, name }) => ({ id, name, modes: modesOf(id) }),
234
+ );
235
+
144
236
  const current = yield* Effect.promise(readOmarchyTheme);
145
237
  const parsed = current === null ? undefined : omarchyTheme(current.colors);
146
238
 
147
239
  if (current === null || parsed === undefined || Result.isFailure(parsed))
148
- return core;
240
+ return [...own, ...groundwork];
149
241
 
150
242
  const omarchy = {
151
243
  id: "omarchy",
@@ -153,54 +245,95 @@ export const themeOptions: Effect.Effect<readonly ThemeOption[]> = Effect.gen(
153
245
  modes: Object.keys(hexModes(runtimeTheme(parsed.success).theme)),
154
246
  };
155
247
 
156
- return [omarchy, ...core];
157
- },
158
- );
248
+ return [...own, omarchy, ...groundwork];
249
+ });
250
+
251
+ /** `themeOptionsWith` for an app with no themes of its own. */
252
+ export const themeOptions: Effect.Effect<readonly ThemeOption[]> =
253
+ themeOptionsWith();
159
254
 
160
255
  /** A theme and mode to show; either can be left to the defaults. */
161
256
  export interface ThemePick {
162
- /** A core theme's id, or `omarchy` for the desktop's Omarchy theme */
257
+ /**
258
+ * A Groundwork theme's id, one of the app's own themes' ids, `omarchy` for
259
+ * the desktop's Omarchy theme, or a theme file's path
260
+ */
163
261
  readonly theme?: string | undefined;
164
262
  /** `light`, `dark` or `darker` */
165
263
  readonly mode?: string | undefined;
264
+ /** Panels leave their backgrounds unpainted, so the terminal's shows through */
265
+ readonly transparent?: boolean | undefined;
166
266
  }
167
267
 
168
268
  /**
169
269
  * A pick's slots. Without a theme it's the desktop's Omarchy theme where
170
- * there is one, otherwise Base; without a mode it's the Omarchy theme's own,
171
- * otherwise dark.
270
+ * there is one, otherwise Base; without a mode it's the theme's own,
271
+ * otherwise dark. `themes` are the app's own, picked by id. A theme file
272
+ * that can't be read or decoded falls back to the default, with a warning.
172
273
  */
173
- export const resolveTheme = (pick: ThemePick): Effect.Effect<Theme> =>
274
+ export const resolveTheme = (
275
+ pick: ThemePick,
276
+ themes: readonly ThemeDocument[] = [],
277
+ ): Effect.Effect<Theme> =>
174
278
  Effect.gen(function* () {
279
+ const options = { transparent: pick.transparent };
280
+ const own = themes.find((document) => document.id === pick.theme);
281
+
282
+ if (own !== undefined) return themeFromDocument(own, pick.mode, options);
283
+
284
+ if (pick.theme !== undefined && isThemeFile(pick.theme)) {
285
+ return yield* themeFromFile(pick.theme, pick.mode, options).pipe(
286
+ Effect.provide(NodeServices.layer),
287
+ Effect.catch((error) =>
288
+ Console.error(`${error.message}; using the default theme`).pipe(
289
+ Effect.as(fromRoles(opentuiThemes.base.dark, options)),
290
+ ),
291
+ ),
292
+ );
293
+ }
294
+
175
295
  if (pick.theme === undefined || pick.theme === "omarchy") {
176
296
  const current = yield* Effect.promise(readOmarchyTheme);
177
297
 
178
298
  const theme =
179
299
  current === null
180
300
  ? undefined
181
- : omarchyThemeFor(current.colors, pick.mode);
301
+ : omarchyThemeFor(current.colors, pick.mode, options);
182
302
 
183
303
  if (theme !== undefined) return theme;
184
304
  }
185
305
 
186
- return themeFor(pick.theme ?? "base", pick.mode ?? "dark");
306
+ return themeFor(pick.theme ?? "base", pick.mode ?? "dark", options);
187
307
  });
188
308
 
189
309
  /**
190
- * Loads the theme from `GW_THEME` and `GW_MODE` (`light`, `dark` or
191
- * `darker`), as `resolveTheme` picks it: `GW_THEME` is a core theme's id, or
192
- * `omarchy` for the desktop's Omarchy theme, which is also the default where
193
- * Omarchy is installed.
310
+ * Loads the theme from `GW_THEME`, `GW_MODE` (`light`, `dark` or `darker`)
311
+ * and `GW_TRANSPARENT`, as `resolveTheme` picks it: `GW_THEME` is a theme's
312
+ * id, one of `themes`' ids, a theme file's path, or `omarchy` for the
313
+ * desktop's Omarchy theme, which is also the default where Omarchy is
314
+ * installed.
194
315
  */
195
- export const loadTheme: Effect.Effect<Theme> = Effect.gen(function* () {
196
- const theme = yield* Config.option(Config.String("GW_THEME"));
316
+ export const loadThemeWith = (
317
+ themes: readonly ThemeDocument[] = [],
318
+ ): Effect.Effect<Theme> =>
319
+ Effect.gen(function* () {
320
+ const theme = yield* Config.option(Config.String("GW_THEME"));
197
321
 
198
- const mode = yield* Config.option(
199
- Config.Literals(["light", "dark", "darker"], "GW_MODE"),
200
- );
322
+ const mode = yield* Config.option(
323
+ Config.Literals(["light", "dark", "darker"], "GW_MODE"),
324
+ );
201
325
 
202
- return yield* resolveTheme({
203
- theme: Option.getOrUndefined(theme),
204
- mode: Option.getOrUndefined(mode),
205
- });
206
- }).pipe(Effect.orElseSucceed(() => DEFAULT_THEME));
326
+ const transparent = yield* Config.option(Config.Boolean("GW_TRANSPARENT"));
327
+
328
+ return yield* resolveTheme(
329
+ {
330
+ theme: Option.getOrUndefined(theme),
331
+ mode: Option.getOrUndefined(mode),
332
+ transparent: Option.getOrUndefined(transparent),
333
+ },
334
+ themes,
335
+ );
336
+ }).pipe(Effect.orElseSucceed(() => DEFAULT_THEME));
337
+
338
+ /** `loadThemeWith` for an app with no themes of its own. */
339
+ export const loadTheme: Effect.Effect<Theme> = loadThemeWith();