feat(theming): apply writes four tool configs to $HOME
This commit is contained in:
@@ -7,9 +7,13 @@ import (
|
|||||||
"embed"
|
"embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/MDietermann/ArduinoArchSetup/internal/config"
|
||||||
|
"github.com/MDietermann/ArduinoArchSetup/internal/platform"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -95,3 +99,47 @@ func Get(id string) (Theme, bool) {
|
|||||||
}
|
}
|
||||||
return Theme{}, false
|
return Theme{}, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// toolConfig describes one tool's config file: its asset name under
|
||||||
|
// assets/<theme>/ and its user-config destination relative to $HOME.
|
||||||
|
type toolConfig struct {
|
||||||
|
Asset string // e.g. "starship.toml"
|
||||||
|
DestRel string // e.g. ".config/starship.toml"
|
||||||
|
}
|
||||||
|
|
||||||
|
var toolConfigs = []toolConfig{
|
||||||
|
{"starship.toml", ".config/starship.toml"},
|
||||||
|
{"zellij.kdl", ".config/zellij/config.kdl"},
|
||||||
|
{"yazi.toml", ".config/yazi/theme.toml"},
|
||||||
|
{"lazygit.yml", ".config/lazygit/config.yml"},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply writes each tool's theme config to the user's home directory.
|
||||||
|
// Task 3 adds overlay-file update; Task 4 adds NvChad chadrc rewrite.
|
||||||
|
func Apply(cfg config.Config, themeID string) error {
|
||||||
|
th, ok := Get(themeID)
|
||||||
|
if !ok {
|
||||||
|
ids := make([]string, 0, len(discovered))
|
||||||
|
for _, t := range discovered {
|
||||||
|
ids = append(ids, t.ID)
|
||||||
|
}
|
||||||
|
return fmt.Errorf("unknown theme %q; valid: %s", themeID, strings.Join(ids, ", "))
|
||||||
|
}
|
||||||
|
home, err := os.UserHomeDir()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range toolConfigs {
|
||||||
|
data, err := assetsFS.ReadFile(filepath.Join("assets", th.ID, tc.Asset))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("read asset %s: %w", tc.Asset, err)
|
||||||
|
}
|
||||||
|
dst := filepath.Join(home, tc.DestRel)
|
||||||
|
if err := platform.WriteUserFile(dst, data, 0o644); err != nil {
|
||||||
|
return fmt.Errorf("write %s: %w", tc.DestRel, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ = cfg // overlay update lands in Task 3
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +1,12 @@
|
|||||||
package theming
|
package theming
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/MDietermann/ArduinoArchSetup/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestListDiscoversThemes(t *testing.T) {
|
func TestListDiscoversThemes(t *testing.T) {
|
||||||
@@ -40,3 +45,37 @@ func TestListIsSortedByID(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestApplyWritesFourToolConfigs(t *testing.T) {
|
||||||
|
home := t.TempDir()
|
||||||
|
t.Setenv("HOME", home)
|
||||||
|
if err := Apply(config.Config{}, "gruvbox"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
checks := []struct{ rel, wantPrefix string }{
|
||||||
|
{".config/starship.toml", "palette"},
|
||||||
|
{".config/zellij/config.kdl", "theme"},
|
||||||
|
{".config/yazi/theme.toml", ""},
|
||||||
|
{".config/lazygit/config.yml", "gui"},
|
||||||
|
}
|
||||||
|
for _, c := range checks {
|
||||||
|
data, err := os.ReadFile(filepath.Join(home, c.rel))
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%s missing: %v", c.rel, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if len(data) == 0 {
|
||||||
|
t.Errorf("%s empty", c.rel)
|
||||||
|
}
|
||||||
|
if c.wantPrefix != "" && !strings.HasPrefix(strings.TrimSpace(string(data)), c.wantPrefix) {
|
||||||
|
t.Errorf("%s: want prefix %q, got %q", c.rel, c.wantPrefix, string(data[:min(len(data), 40)]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestApplyUnknownThemeErrors(t *testing.T) {
|
||||||
|
if err := Apply(config.Config{}, "does-not-exist"); err == nil {
|
||||||
|
t.Fatal("want error on unknown theme")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user