Skip to content

Commit

Permalink
Add theme
Browse files Browse the repository at this point in the history
Define a dark and light theme with the default on the light theme
  • Loading branch information
antham committed Jul 13, 2024
1 parent c4398df commit 1d6d191
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 22 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)`
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions internal/cmd/mock_workspaceManager_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ func newRootCmd() *cobra.Command {
slog.SetLogLoggerLevel(slog.LevelDebug)
}

err = viper.BindEnv("WO_THEME")
if err != nil {
log.Fatal(err)

Check warning on line 37 in internal/cmd/root.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/root.go#L35-L37

Added lines #L35 - L37 were not covered by tests
}
if viper.GetString("WO_THEME") == "dark" {
applyDarkTheme()

Check warning on line 40 in internal/cmd/root.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/root.go#L39-L40

Added lines #L39 - L40 were not covered by tests
}

w, err := newWorkspaceManager()
if err != nil {
log.Fatal(err)
Expand Down
6 changes: 6 additions & 0 deletions internal/cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
}
45 changes: 42 additions & 3 deletions internal/cmd/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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_").
Expand All @@ -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) {
Expand All @@ -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)
Expand All @@ -136,13 +139,15 @@ 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) {
assert.NoError(t, err)
assert.Equal(t,
`alias c_front="cd /tmp/front"
alias c_test="cd /tmp/test"
export WO_THEME=light
`,
stdout.String(),
)
Expand All @@ -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) {
Expand Down
16 changes: 0 additions & 16 deletions internal/cmd/style.go

This file was deleted.

28 changes: 28 additions & 0 deletions internal/cmd/theme.go
Original file line number Diff line number Diff line change
@@ -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("---")

Check warning on line 27 in internal/cmd/theme.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/theme.go#L18-L27

Added lines #L18 - L27 were not covered by tests
}
6 changes: 3 additions & 3 deletions internal/workspace/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1d6d191

Please sign in to comment.