feat(theming): apply updates overlay theme_overrides.__active

This commit is contained in:
MDietermann
2026-04-14 13:11:34 +02:00
parent b422402ffc
commit 4fa580f226
2 changed files with 85 additions and 1 deletions

View File

@@ -140,6 +140,52 @@ func Apply(cfg config.Config, themeID string) error {
return fmt.Errorf("write %s: %w", tc.DestRel, err) return fmt.Errorf("write %s: %w", tc.DestRel, err)
} }
} }
_ = cfg // overlay update lands in Task 3 _ = cfg // overlay carries config state; overlay file update below
if err := updateOverlayActive(th.ID); err != nil {
return fmt.Errorf("update overlay: %w", err)
}
return nil return nil
} }
// updateOverlayActive rewrites ~/.config/archduino/overlay.yaml so that
// theme_overrides.__active reflects the selected theme, preserving all
// other keys via a generic map round-trip.
func updateOverlayActive(themeID string) error {
home, err := os.UserHomeDir()
if err != nil {
return err
}
path := filepath.Join(home, ".config", "archduino", "overlay.yaml")
var root map[string]any
if data, err := os.ReadFile(path); err == nil {
if err := yaml.Unmarshal(data, &root); err != nil {
return fmt.Errorf("parse overlay: %w", err)
}
}
if root == nil {
root = map[string]any{}
}
if _, ok := root["version"]; !ok {
root["version"] = 1
}
var overrides map[string]any
if raw, ok := root["theme_overrides"]; ok {
if m, ok := raw.(map[string]any); ok {
overrides = m
}
}
if overrides == nil {
overrides = map[string]any{}
}
overrides["__active"] = themeID
root["theme_overrides"] = overrides
out, err := yaml.Marshal(root)
if err != nil {
return err
}
return platform.WriteUserFile(path, out, 0o644)
}

View File

@@ -79,3 +79,41 @@ func TestApplyUnknownThemeErrors(t *testing.T) {
t.Fatal("want error on unknown theme") t.Fatal("want error on unknown theme")
} }
} }
func TestApplyUpdatesOverlayActive(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
if err := Apply(config.Config{}, "gruvbox"); err != nil {
t.Fatal(err)
}
data, err := os.ReadFile(filepath.Join(home, ".config", "archduino", "overlay.yaml"))
if err != nil {
t.Fatalf("overlay missing: %v", err)
}
if !strings.Contains(string(data), "__active: gruvbox") {
t.Fatalf("overlay missing active theme: %s", data)
}
}
func TestApplyPreservesOtherOverlayKeys(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
op := filepath.Join(home, ".config", "archduino", "overlay.yaml")
if err := os.MkdirAll(filepath.Dir(op), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(op, []byte("version: 1\nextra_packages:\n - foo\n"), 0o644); err != nil {
t.Fatal(err)
}
if err := Apply(config.Config{}, "gruvbox"); err != nil {
t.Fatal(err)
}
data, _ := os.ReadFile(op)
if !strings.Contains(string(data), "- foo") {
t.Fatalf("extra_packages lost: %s", data)
}
if !strings.Contains(string(data), "__active: gruvbox") {
t.Fatalf("active theme missing after preserve: %s", data)
}
}