herdr-gentle-shell 1.8.0 → 1.8.1

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.
@@ -2092,10 +2092,13 @@ const CUSTOMIZE_OFF = "Off";
2092
2092
  // unpinnable, because selecting it would read as "clear the pin".
2093
2093
  const CUSTOMIZE_THEME_UNASSIGN = "Off (no theme assigned)";
2094
2094
 
2095
- // The row id `lib/customize-panel.js` gives the theme value row. It is not an
2096
- // On/Off pair: the row opens the host's theme list, so the apply path keys the
2097
- // list on this id instead of on a value it could toggle.
2098
- const CUSTOMIZE_THEME_SELECT_ROW_ID = "select";
2095
+ // The theme setting's STRUCTURAL option ids from `lib/customize-panel.js`. They
2096
+ // are never labels: `none` is the "no theme assigned" choice and `theme:<name>`
2097
+ // a real theme, so an installed theme literally called `None` or `Off` keeps its
2098
+ // own option and stays pinnable.
2099
+ const CUSTOMIZE_THEME_SETTING_ID = "theme";
2100
+ const CUSTOMIZE_THEME_NONE_OPTION_ID = "none";
2101
+ const CUSTOMIZE_THEME_OPTION_PREFIX = "theme:";
2099
2102
  const THEME_SYNC_ON_TEXT = "Theme sync on; Herdr's palette follows the theme active in Pi.";
2100
2103
  const THEME_SYNC_OFF_TEXT = "Theme sync off; Herdr keeps the palette it was last given.";
2101
2104
  // The refusal keeps S4's promise: the pinned theme is only available while the
@@ -2169,13 +2172,57 @@ function readCustomizeThemeState() {
2169
2172
  }
2170
2173
  }
2171
2174
 
2172
- function customizePanelModel() {
2175
+ // S2: the panel lists Pi's own themes, so the extension reads them from the
2176
+ // host instead of shipping a copy. `null` means "this host exposes no list" and
2177
+ // the panel says so with one note instead of showing an empty picker.
2178
+ function readHostThemeNames(ctx) {
2179
+ const listThemes = ctx?.ui?.getAllThemes;
2180
+ if (typeof listThemes !== "function") return null;
2181
+ try {
2182
+ return themeNames(listThemes.call(ctx.ui));
2183
+ } catch {
2184
+ return null;
2185
+ }
2186
+ }
2187
+
2188
+ // S6: a theme's preview is a swatch in THAT theme's colours, taken from the
2189
+ // theme object the host hands back. The raw SGR is deliberate and lives only
2190
+ // here: the panel is a plain CommonJS module with no host colour API, and
2191
+ // falling back to the active theme would show a palette the user did not pick.
2192
+ // An unreadable theme throws, and the panel renders `Preview unavailable`.
2193
+ function themePreviewFactory(ctx) {
2194
+ const getTheme = ctx?.ui?.getTheme;
2195
+ if (typeof getTheme !== "function") return null;
2196
+ return (name) => {
2197
+ let theme;
2198
+ try {
2199
+ theme = getTheme.call(ctx.ui, name);
2200
+ } catch {
2201
+ theme = null;
2202
+ }
2203
+ if (!theme) throw new Error(`Theme ${name} is unavailable.`);
2204
+ if (typeof theme.name === "string" && theme.name !== name) {
2205
+ throw new Error(`Theme ${name} is unavailable.`);
2206
+ }
2207
+ const accent = typeof theme.getBgAnsi === "function" ? theme.getBgAnsi("accent") : undefined;
2208
+ const text = typeof theme.getFgAnsi === "function" ? theme.getFgAnsi("text") : undefined;
2209
+ if (typeof accent !== "string" || typeof text !== "string") {
2210
+ throw new Error(`Theme ${name} exposes no palette escapes.`);
2211
+ }
2212
+ // The upstream sample: a background block then the sample text.
2213
+ return { title: "source palette", sample: `${accent} \x1b[0m ${text}Aa sample text\x1b[0m` };
2214
+ };
2215
+ }
2216
+
2217
+ function customizePanelModel(ctx) {
2173
2218
  const panel = customizePanelModule();
2174
2219
  if (!panel) return null;
2175
2220
  return panel.customizePanelModel({
2176
2221
  sidebar: sidebarRowsState().state,
2177
2222
  moshi: moshiFlagEnabled(moshiFlagPath()) ? "on" : "off",
2178
2223
  theme: readCustomizeThemeState(),
2224
+ themeNames: readHostThemeNames(ctx),
2225
+ themePreview: themePreviewFactory(ctx),
2179
2226
  });
2180
2227
  }
2181
2228
 
@@ -2569,7 +2616,7 @@ function applyMoshiNotifications(want) {
2569
2616
  // only when the Moshi settings entry actually changed. The theme switch never
2570
2617
  // reloads: the palette it writes belongs to Herdr, so a Pi reload would only
2571
2618
  // restart the extension for nothing.
2572
- async function applyCustomizeSwitch(id, value, ctx) {
2619
+ async function applyCustomizeSwitch(id, value, ctx, options = {}) {
2573
2620
  const want = value === "on";
2574
2621
  const result = id === "sidebar"
2575
2622
  ? await applySidebarRows(want)
@@ -2580,7 +2627,11 @@ async function applyCustomizeSwitch(id, value, ctx) {
2580
2627
  if (typeof notify === "function") {
2581
2628
  notify(`${result.text}${result.needsReload ? " Reloading now to apply the change." : ""}`, result.level);
2582
2629
  }
2583
- if (result.needsReload && typeof ctx?.reload === "function") await ctx.reload();
2630
+ // The overlay defers the reload (`deferReload`): reloading while the card is
2631
+ // still mounted restarts Pi behind an open overlay, so the panel closes first
2632
+ // and the caller reloads after `ctx.ui.custom` resolved.
2633
+ if (result.needsReload && options.deferReload !== true && typeof ctx?.reload === "function") await ctx.reload();
2634
+ return result;
2584
2635
  }
2585
2636
 
2586
2637
  // The one-line spelling of the theme state, shared by `status` and the menu
@@ -2632,7 +2683,9 @@ function applyThemeSync(want) {
2632
2683
 
2633
2684
  // `ctx.ui.getAllThemes()` documents `{ name, path }` records; a plain string list
2634
2685
  // is accepted too. A malformed entry is skipped instead of failing the whole
2635
- // list, and the result is deduplicated and sorted so the prompt is stable.
2686
+ // list. S2 keeps Pi's OWN order (the list the user sees in /settings), drops the
2687
+ // names that are paths rather than themes (`/` in the name, as upstream does) and
2688
+ // deduplicates, so one theme is never offered twice.
2636
2689
  function themeNames(themes) {
2637
2690
  if (!Array.isArray(themes)) return null;
2638
2691
  const names = [];
@@ -2640,9 +2693,10 @@ function themeNames(themes) {
2640
2693
  const name = typeof entry === "string" ? entry : isRecord(entry) ? entry.name : null;
2641
2694
  if (typeof name !== "string") continue;
2642
2695
  const clean = name.trim();
2643
- if (clean.length > 0) names.push(clean);
2696
+ if (clean.length === 0 || clean.includes("/")) continue;
2697
+ names.push(clean);
2644
2698
  }
2645
- return [...new Set(names)].sort();
2699
+ return [...new Set(names)];
2646
2700
  }
2647
2701
 
2648
2702
  // The picker's entries plus the ONE entry that means "unassign". A theme really
@@ -2658,9 +2712,41 @@ function themeSelectionOptions(names) {
2658
2712
  return { options: [...names, unassign], unassign };
2659
2713
  }
2660
2714
 
2661
- // S4: pin one of Pi's themes while the sync is off. The refusal, the missing
2662
- // theme list and an empty answer all write NOTHING, so the stored document only
2663
- // ever changes to a value the user actually picked.
2715
+ // One write for the pinned theme, shared by the inline panel setting and the
2716
+ // `ctx.ui.select` fallback: the refusal, the missing store and a failure all
2717
+ // write NOTHING, so the stored document only ever changes to a value the user
2718
+ // actually picked (S7).
2719
+ function writeThemeChoice(ctx, name) {
2720
+ const notify = ctx?.ui?.notify;
2721
+ const store = themeStoreModule();
2722
+ if (!store || typeof store.writeThemeState !== "function") {
2723
+ if (typeof notify === "function") notify(THEME_STORE_UNAVAILABLE_TEXT, "warning");
2724
+ return false;
2725
+ }
2726
+ if (readCustomizeThemeState().sync === true) {
2727
+ if (typeof notify === "function") notify(THEME_SYNC_REFUSED_TEXT, "warning");
2728
+ return false;
2729
+ }
2730
+ try {
2731
+ store.writeThemeState(process.env, { sync: false, theme: name });
2732
+ } catch (error) {
2733
+ if (typeof notify === "function") notify(themeStoreWriteError(error), "error");
2734
+ return false;
2735
+ }
2736
+ if (typeof notify === "function") {
2737
+ notify(
2738
+ name === null
2739
+ ? "Gentle Shell theme set to Off; Herdr keeps the palette it was last given."
2740
+ : `Gentle Shell theme set to ${name}.`,
2741
+ "info",
2742
+ );
2743
+ }
2744
+ return true;
2745
+ }
2746
+
2747
+ // S4: pin one of Pi's themes while the sync is off, through the host's own
2748
+ // picker. The refusal, the missing theme list and an empty answer all write
2749
+ // NOTHING, so the stored document only ever changes to a value the user picked.
2664
2750
  async function applyThemeSelection(ctx) {
2665
2751
  const notify = ctx?.ui?.notify;
2666
2752
  const store = themeStoreModule();
@@ -2702,47 +2788,50 @@ async function applyThemeSelection(ctx) {
2702
2788
  }
2703
2789
  // Esc/cancel and an empty answer write nothing at all.
2704
2790
  if (typeof picked !== "string" || picked.length === 0) return;
2705
- const next = picked === picker.unassign ? null : picked;
2706
- try {
2707
- store.writeThemeState(process.env, { sync: false, theme: next });
2708
- } catch (error) {
2709
- if (typeof notify === "function") notify(themeStoreWriteError(error), "error");
2710
- return;
2711
- }
2712
- if (typeof notify === "function") {
2713
- notify(
2714
- next === null
2715
- ? "Gentle Shell theme set to Off; Herdr keeps the palette it was last given."
2716
- : `Gentle Shell theme set to ${next}.`,
2717
- "info",
2718
- );
2719
- }
2791
+ writeThemeChoice(ctx, picked === picker.unassign ? null : picked);
2720
2792
  }
2721
2793
 
2722
- // The single apply path shared by the overlay and the menu fallback. The choice
2723
- // the panel resolves names the switch it drives (`switchId`) and the value to
2724
- // apply; that switch id is the only thing read here, so a row can never apply a
2725
- // switch its category does not own. An inconclusive sidebar verdict is never a
2726
- // refusal: choosing On still attempts the repair and reports the apply step's
2727
- // own error, exactly like the baseline menu did. The theme value is the one row
2728
- // that is not a toggle: it carries the `select` row id, and the refusal for a
2729
- // stored sync lives in `applyThemeSelection` so both entry points share it.
2730
- const CUSTOMIZE_SWITCH_IDS = ["sidebar", "moshi", "theme-sync", "theme"];
2794
+ // The apply path of the menu fallback. The choice names the switch it drives
2795
+ // (`switchId`) and the value to apply; that switch id is the only thing read
2796
+ // here, so a row can never apply a switch its category does not own. An
2797
+ // inconclusive sidebar verdict is never a refusal: choosing On still attempts
2798
+ // the repair and reports the apply step's own error, exactly like the baseline
2799
+ // menu did. The fallback has no inline theme list, so its theme setting keeps
2800
+ // opening the host's picker through `applyThemeSelection`.
2801
+ const CUSTOMIZE_SWITCH_IDS = ["sidebar", "moshi", "theme-sync"];
2731
2802
 
2732
2803
  async function applyCustomizeChoice(choice, ctx) {
2733
2804
  if (!isRecord(choice)) return;
2734
2805
  const switchId = choice.switchId;
2735
2806
  const valueId = choice.valueId;
2736
2807
  if (!CUSTOMIZE_SWITCH_IDS.includes(switchId)) return;
2737
- if (switchId === "theme") {
2738
- if (valueId !== CUSTOMIZE_THEME_SELECT_ROW_ID) return;
2739
- await applyThemeSelection(ctx);
2740
- return;
2741
- }
2742
2808
  if (valueId !== "on" && valueId !== "off") return;
2743
2809
  await applyCustomizeSwitch(switchId, valueId, ctx);
2744
2810
  }
2745
2811
 
2812
+ // The overlay's apply path: it receives the structural ids of the setting and
2813
+ // the option the user confirmed, writes through the same functions the menu
2814
+ // uses, and answers with a freshly read model so the panel shows the real state
2815
+ // instead of guessing it. `close` is only true when Pi must reload: the caller
2816
+ // closes the overlay first and reloads after it resolved.
2817
+ async function applyCustomizePanel(choice, ctx) {
2818
+ const model = () => customizePanelModel(ctx);
2819
+ if (!isRecord(choice)) return { model: model(), close: false, reload: false };
2820
+ const { settingId, optionId } = choice;
2821
+ if (settingId === CUSTOMIZE_THEME_SETTING_ID) {
2822
+ if (optionId === CUSTOMIZE_THEME_NONE_OPTION_ID) writeThemeChoice(ctx, null);
2823
+ else if (typeof optionId === "string" && optionId.startsWith(CUSTOMIZE_THEME_OPTION_PREFIX)) {
2824
+ writeThemeChoice(ctx, optionId.slice(CUSTOMIZE_THEME_OPTION_PREFIX.length));
2825
+ }
2826
+ return { model: model(), close: false, reload: false };
2827
+ }
2828
+ if (!CUSTOMIZE_SWITCH_IDS.includes(settingId)) return { model: model(), close: false, reload: false };
2829
+ if (optionId !== "on" && optionId !== "off") return { model: model(), close: false, reload: false };
2830
+ const result = await applyCustomizeSwitch(settingId, optionId, ctx, { deferReload: true });
2831
+ const reload = Boolean(result && result.needsReload);
2832
+ return { model: model(), close: reload, reload };
2833
+ }
2834
+
2746
2835
  // The former plain menu, kept as the fallback when the TUI helpers or
2747
2836
  // `ctx.ui.custom` are unavailable. It reads the same model and applies
2748
2837
  // through the same path as the overlay, and it offers the theme rows too, so
@@ -2826,11 +2915,11 @@ async function executeCustomize(rawArgs, ctx) {
2826
2915
  }
2827
2916
  if (helpers && ctx?.ui && typeof ctx.ui.custom === "function") {
2828
2917
  const panel = customizePanelModule();
2829
- const model = customizePanelModel();
2830
- let choice = null;
2918
+ const model = customizePanelModel(ctx);
2919
+ let reloadPending = false;
2831
2920
  if (panel && model) {
2832
2921
  try {
2833
- choice = await ctx.ui.custom(
2922
+ await ctx.ui.custom(
2834
2923
  // Theme and terminal rows travel through the real factory arguments:
2835
2924
  // the card paints with the host theme and clips to the live height.
2836
2925
  (hostTui, theme, _keybindings, done) => panel.createCustomizePanel({
@@ -2848,6 +2937,15 @@ async function executeCustomize(rawArgs, ctx) {
2848
2937
  }
2849
2938
  },
2850
2939
  model,
2940
+ // Enter applies in place behind the panel's busy guard: the panel
2941
+ // swaps in the model this reads back and stays open, except when a
2942
+ // reload is needed — then it closes so `ctx.reload()` runs here,
2943
+ // never behind a mounted card.
2944
+ apply: async (request) => {
2945
+ const outcome = await applyCustomizePanel(request, ctx);
2946
+ if (outcome.reload) reloadPending = true;
2947
+ return { model: outcome.model, close: outcome.close };
2948
+ },
2851
2949
  done,
2852
2950
  }),
2853
2951
  CUSTOMIZE_OVERLAY_OPTIONS,
@@ -2857,14 +2955,10 @@ async function executeCustomize(rawArgs, ctx) {
2857
2955
  await runCustomizeFallback(ctx);
2858
2956
  return;
2859
2957
  }
2860
- } else {
2861
- await runCustomizeFallback(ctx);
2958
+ if (reloadPending && typeof ctx.reload === "function") await ctx.reload();
2862
2959
  return;
2863
2960
  }
2864
- // Esc (null) does nothing at all: no notification, no write, no reload.
2865
- // Anything else runs the single apply path once the overlay is disposed.
2866
- if (!choice || typeof choice !== "object") return;
2867
- await applyCustomizeChoice(choice, ctx);
2961
+ await runCustomizeFallback(ctx);
2868
2962
  return;
2869
2963
  }
2870
2964
  await runCustomizeFallback(ctx);