This repository has been archived by the owner on Aug 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfig.go
66 lines (57 loc) · 1.37 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package history
import (
"fmt"
"os"
"path/filepath"
"runtime"
"github.com/BurntSushi/toml"
)
type config struct {
Prompt string `toml:"prompt"`
InitQuery string `toml:"init_query"`
InitCursor string `toml:"init_cursor"`
ScreenColumns []string `toml:"screen_columns"`
VimModePrompt string `toml:"vim_mode_prompt"`
IgnoreWords []string `toml:"ignore_words"`
}
const tomlDir = "zhist"
func (cfg *config) load() error {
var dir string
if runtime.GOOS == "windows" {
base := os.Getenv("APPDATA")
if base == "" {
base = filepath.Join(os.Getenv("USERPROFILE"), "Application Data")
}
dir = filepath.Join(base, tomlDir)
} else {
dir = filepath.Join(os.Getenv("HOME"), ".config", tomlDir)
}
if err := os.MkdirAll(dir, 0700); err != nil {
return fmt.Errorf("cannot create directory: %v", err)
}
tomlFile := filepath.Join(dir, "config.toml")
_, err := os.Stat(tomlFile)
if err == nil {
_, err := toml.DecodeFile(tomlFile, cfg)
if err != nil {
return err
}
return nil
}
if !os.IsNotExist(err) {
return err
}
f, err := os.Create(tomlFile)
if err != nil {
return err
}
defer f.Close()
// Set default value
cfg.InitQuery = DefaultQuery
cfg.InitCursor = Wildcard
cfg.Prompt = Prompt
cfg.ScreenColumns = []string{"command"}
cfg.VimModePrompt = "VIM-MODE"
cfg.IgnoreWords = []string{}
return toml.NewEncoder(f).Encode(cfg)
}