From 1d6d1918a29e523b008a4f40de29c51b9083de3f Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Sat, 13 Jul 2024 17:17:28 +0200 Subject: [PATCH] Add theme Define a dark and light theme with the default on the light theme --- README.md | 2 + internal/cmd/interface.go | 1 + internal/cmd/mock_workspaceManager_test.go | 18 +++++++++ internal/cmd/root.go | 8 ++++ internal/cmd/setup.go | 6 +++ internal/cmd/setup_test.go | 45 ++++++++++++++++++++-- internal/cmd/style.go | 16 -------- internal/cmd/theme.go | 28 ++++++++++++++ internal/workspace/workspace.go | 6 +-- 9 files changed, 108 insertions(+), 22 deletions(-) delete mode 100644 internal/cmd/style.go create mode 100644 internal/cmd/theme.go diff --git a/README.md b/README.md index df72b15..e14815d 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,8 @@ Add the following command to your shell init file according to your shell. You can customize how the aliases are generated (see below in usage what is the goal of those aliases), the default is to prefix them with `c_`, you can change this behaviour with the `-p` flag on the setup command. +You can set the theme with the `-t` flag, it could be either `dark` or `light`, the default is the `light` theme. + ### Bash `source <(wo setup bash)` diff --git a/internal/cmd/interface.go b/internal/cmd/interface.go index c69d15b..f87d54b 100644 --- a/internal/cmd/interface.go +++ b/internal/cmd/interface.go @@ -6,6 +6,7 @@ import ( ) type workspaceManager interface { + CreateEnvVariableStatement(string, string) string BuildAliases(string) ([]string, error) Get(string) (workspace.Workspace, error) Create(string, string) error diff --git a/internal/cmd/mock_workspaceManager_test.go b/internal/cmd/mock_workspaceManager_test.go index 910ee35..18f089c 100644 --- a/internal/cmd/mock_workspaceManager_test.go +++ b/internal/cmd/mock_workspaceManager_test.go @@ -78,6 +78,24 @@ func (_m *mockWorkspaceManager) CreateEnv(_a0 string, _a1 string) error { return r0 } +// CreateEnvVariableStatement provides a mock function with given fields: _a0, _a1 +func (_m *mockWorkspaceManager) CreateEnvVariableStatement(_a0 string, _a1 string) string { + ret := _m.Called(_a0, _a1) + + if len(ret) == 0 { + panic("no return value specified for CreateEnvVariableStatement") + } + + var r0 string + if rf, ok := ret.Get(0).(func(string, string) string); ok { + r0 = rf(_a0, _a1) + } else { + r0 = ret.Get(0).(string) + } + + return r0 +} + // Edit provides a mock function with given fields: _a0 func (_m *mockWorkspaceManager) Edit(_a0 string) error { ret := _m.Called(_a0) diff --git a/internal/cmd/root.go b/internal/cmd/root.go index e8f0d5f..75f5c43 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -32,6 +32,14 @@ func newRootCmd() *cobra.Command { slog.SetLogLoggerLevel(slog.LevelDebug) } + err = viper.BindEnv("WO_THEME") + if err != nil { + log.Fatal(err) + } + if viper.GetString("WO_THEME") == "dark" { + applyDarkTheme() + } + w, err := newWorkspaceManager() if err != nil { log.Fatal(err) diff --git a/internal/cmd/setup.go b/internal/cmd/setup.go index 831887a..1241a36 100644 --- a/internal/cmd/setup.go +++ b/internal/cmd/setup.go @@ -9,6 +9,7 @@ import ( func newSetupCmd(workspaceManager workspaceManager) *cobra.Command { var prefix string + var theme string cmd := &cobra.Command{ Use: "setup shell", Short: "Command to setup wo in the shell", @@ -45,9 +46,14 @@ func newSetupCmd(workspaceManager workspaceManager) *cobra.Command { for _, alias := range aliases { cmd.Println(alias) } + if !slices.Contains([]string{"dark", "light"}, theme) { + return fmt.Errorf(`"%s" theme is not supported, must be either "light" or "dark"`, theme) + } + cmd.Println(workspaceManager.CreateEnvVariableStatement("WO_THEME", theme)) return nil }, } cmd.Flags().StringVarP(&prefix, "prefix", "p", "c_", "Prefix name to use for the aliases") + cmd.Flags().StringVarP(&theme, "theme", "t", "light", "Theme to use") return cmd } diff --git a/internal/cmd/setup_test.go b/internal/cmd/setup_test.go index ce55f78..429d20c 100644 --- a/internal/cmd/setup_test.go +++ b/internal/cmd/setup_test.go @@ -52,6 +52,7 @@ func TestNewSetupCmd(t *testing.T) { }, nil, ) + w.Mock.On("CreateEnvVariableStatement", "WO_THEME", "light").Return("set -x -g WO_THEME light") return w }, func(t *testing.T, stdout *bytes.Buffer, stderr *bytes.Buffer, err error) { @@ -80,6 +81,7 @@ alias c_test="cd /tmp/test"`, }, nil, ) + w.Mock.On("CreateEnvVariableStatement", "WO_THEME", "light").Return("export WO_THEME=light") return w }, func(t *testing.T, stdout *bytes.Buffer, stderr *bytes.Buffer, err error) { @@ -97,7 +99,7 @@ alias c_test="cd /tmp/test"`, }, { "We get the autocompletion for zsh and aliases", - []string{"bash"}, + []string{"zsh"}, func(t *testing.T) workspaceManager { w := newMockWorkspaceManager(t) w.Mock.On("BuildAliases", "c_"). @@ -108,6 +110,7 @@ alias c_test="cd /tmp/test"`, }, nil, ) + w.Mock.On("CreateEnvVariableStatement", "WO_THEME", "light").Return("export WO_THEME=light") return w }, func(t *testing.T, stdout *bytes.Buffer, stderr *bytes.Buffer, err error) { @@ -119,12 +122,12 @@ alias c_test="cd /tmp/test"`, ) assert.Contains(t, stdout.String(), - `__wo_init_completion()`, + `zsh completion for wo`, ) }, }, { - "We get the aliases only for sh", + "We do not get the completion for sh", []string{"sh"}, func(t *testing.T) workspaceManager { w := newMockWorkspaceManager(t) @@ -136,6 +139,7 @@ alias c_test="cd /tmp/test"`, }, nil, ) + w.Mock.On("CreateEnvVariableStatement", "WO_THEME", "light").Return("export WO_THEME=light") return w }, func(t *testing.T, stdout *bytes.Buffer, stderr *bytes.Buffer, err error) { @@ -143,6 +147,7 @@ alias c_test="cd /tmp/test"`, assert.Equal(t, `alias c_front="cd /tmp/front" alias c_test="cd /tmp/test" +export WO_THEME=light `, stdout.String(), ) @@ -158,6 +163,40 @@ alias c_test="cd /tmp/test" []string{}, nil, ) + w.Mock.On("CreateEnvVariableStatement", "WO_THEME", "light").Return("set -x -g WO_THEME light") + return w + }, + func(t *testing.T, stdout *bytes.Buffer, stderr *bytes.Buffer, err error) { + assert.NoError(t, err) + }, + }, + { + "Set an unsupported theme", + []string{"fish", "-t", "whatever"}, + func(t *testing.T) workspaceManager { + w := newMockWorkspaceManager(t) + w.Mock.On("BuildAliases", "c_"). + Return( + []string{}, + nil, + ) + return w + }, + func(t *testing.T, stdout *bytes.Buffer, stderr *bytes.Buffer, err error) { + assert.Error(t, err) + }, + }, + { + "We get the dark theme", + []string{"fish", "-t", "dark"}, + func(t *testing.T) workspaceManager { + w := newMockWorkspaceManager(t) + w.Mock.On("BuildAliases", "c_"). + Return( + []string{}, + nil, + ) + w.Mock.On("CreateEnvVariableStatement", "WO_THEME", "dark").Return("set -x -g WO_THEME dark") return w }, func(t *testing.T, stdout *bytes.Buffer, stderr *bytes.Buffer, err error) { diff --git a/internal/cmd/style.go b/internal/cmd/style.go deleted file mode 100644 index 4b0153d..0000000 --- a/internal/cmd/style.go +++ /dev/null @@ -1,16 +0,0 @@ -package cmd - -import "github.com/charmbracelet/lipgloss" - -var regularStyle = lipgloss.NewStyle(). - Foreground(lipgloss.Color("#8ECDDD")) - -var highlightedStyle = lipgloss.NewStyle(). - Foreground(lipgloss.Color("#FFFADD")) - -var titleStyle = lipgloss.NewStyle(). - Foreground(lipgloss.Color("#FFCC70")) - -var separator = lipgloss.NewStyle(). - Foreground(lipgloss.Color("#22668D")). - Render("---") diff --git a/internal/cmd/theme.go b/internal/cmd/theme.go new file mode 100644 index 0000000..8c963e5 --- /dev/null +++ b/internal/cmd/theme.go @@ -0,0 +1,28 @@ +package cmd + +import "github.com/charmbracelet/lipgloss" + +var regularStyle = lipgloss.NewStyle(). + Foreground(lipgloss.Color("#3C3744")) + +var highlightedStyle = lipgloss.NewStyle(). + Foreground(lipgloss.Color("#3D52D5")) + +var titleStyle = lipgloss.NewStyle(). + Foreground(lipgloss.Color("#090C9B")) + +var separator = lipgloss.NewStyle(). + Foreground(lipgloss.Color("#B4C5E4")). + Render("---") + +func applyDarkTheme() { + regularStyle = lipgloss.NewStyle(). + Foreground(lipgloss.Color("#8ECDDD")) + highlightedStyle = lipgloss.NewStyle(). + Foreground(lipgloss.Color("#FFFADD")) + titleStyle = lipgloss.NewStyle(). + Foreground(lipgloss.Color("#FFCC70")) + separator = lipgloss.NewStyle(). + Foreground(lipgloss.Color("#22668D")). + Render("---") +} diff --git a/internal/workspace/workspace.go b/internal/workspace/workspace.go index d4172c6..cd3b1fd 100644 --- a/internal/workspace/workspace.go +++ b/internal/workspace/workspace.go @@ -271,8 +271,8 @@ func (s WorkspaceManager) GetConfigDir() string { func (s WorkspaceManager) appendLoadStatement(name string, env string, functionAndArgs []string) []string { data := []string{} - data = append(data, s.createEnvVariableStatement(fmt.Sprintf("%s_NAME", envVariablePrefix), name)) - data = append(data, s.createEnvVariableStatement(fmt.Sprintf("%s_ENV", envVariablePrefix), env)) + data = append(data, s.CreateEnvVariableStatement(fmt.Sprintf("%s_NAME", envVariablePrefix), name)) + data = append(data, s.CreateEnvVariableStatement(fmt.Sprintf("%s_ENV", envVariablePrefix), env)) envFile := s.resolveEnvFile(name, env) _, eerr := os.Stat(envFile) if eerr == nil { @@ -379,7 +379,7 @@ func (s WorkspaceManager) getViper(name string) *viper.Viper { return v } -func (s WorkspaceManager) createEnvVariableStatement(name string, value string) string { +func (s WorkspaceManager) CreateEnvVariableStatement(name string, value string) string { switch s.shell { case bash, sh, zsh: return fmt.Sprintf("export %s=%s", name, value)