Skip to content

Commit

Permalink
feat: added env variables to config
Browse files Browse the repository at this point in the history
  • Loading branch information
tsukinoko-kun committed Oct 2, 2024
1 parent 5fe8dd7 commit acc3900
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ There are some bugs on Windows because I don't use Windows. Feel free to open PR
- `completion_text`: text color of completion list
- `completion_selected_bg`: background color of selected completion entry
- `on_start`: List of commands to run on shell start
- `env`: Map of environment variables

## Install

Expand Down
27 changes: 21 additions & 6 deletions internal/env/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"

"github.com/BurntSushi/toml"
Expand Down Expand Up @@ -44,6 +45,11 @@ func init() {
} else {
Alias = make(map[string][]string)
}
if Config.Environment != nil {
for key, val := range Config.Environment {
os.Setenv(key, val)
}
}
}

type (
Expand All @@ -52,11 +58,12 @@ type (
CompletionSelectedBg string `toml:"completion_selected_bg"`
}
smashConfig struct {
InteractivePrompt string `toml:"ps1"`
LogPrompt string `toml:"ps2"`
Alias map[string]any `toml:"alias"`
Color smashColor `toml:"color"`
OnStart []string `toml:"on_start"`
InteractivePrompt string `toml:"ps1"`
LogPrompt string `toml:"ps2"`
Alias map[string]any `toml:"alias"`
Color smashColor `toml:"color"`
OnStart []string `toml:"on_start"`
Environment map[string]string `toml:"env"`
}
)

Expand Down Expand Up @@ -85,7 +92,8 @@ func getConfigDir() string {
func getConfigFile() *smashConfig {
p := filepath.Join(getConfigDir(), "config.toml")
c := &smashConfig{
Alias: make(map[string]any),
Alias: make(map[string]any),
Environment: make(map[string]string),
Color: smashColor{
CompletionText: "8",
CompletionSelectedBg: "4",
Expand All @@ -104,6 +112,13 @@ func getConfigFile() *smashConfig {
c.InteractivePrompt = "${Color.FgHiBlack}$USER@$PWD\t$DEV${Color.Reset}\n${Color.FgBlue}❯${Color.Reset} "
c.LogPrompt = "${Color.FgHiBlack}$PWD${Color.Reset} "
c.Alias["l"] = []string{"ls", "-l"}
if vim, err := exec.LookPath("nvim"); err == nil {
c.Environment["EDITOR"] = vim
c.Environment["GIT_EDITOR"] = vim
} else if vim, err := exec.LookPath("vim"); err == nil {
c.Environment["EDITOR"] = vim
c.Environment["GIT_EDITOR"] = vim
}
c.OnStart = []string{"smashfetch", "sleep 500ms"}
e := toml.NewEncoder(f)
if err := e.Encode(c); err != nil {
Expand Down

0 comments on commit acc3900

Please sign in to comment.