-
Notifications
You must be signed in to change notification settings - Fork 29
/
config.go
126 lines (106 loc) · 4.03 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package config
import (
"log"
"os"
"github.com/knipferrc/fm/internal/constants"
"github.com/spf13/viper"
)
// SettingsConfig struct represents the config for the settings.
type SettingsConfig struct {
StartDir string `mapstructure:"start_dir"`
ShowIcons bool `mapstructure:"show_icons"`
RoundedPanes bool `mapstructure:"rounded_panes"`
EnableLogging bool `mapstructure:"enable_logging"`
EnableMouseWheel bool `mapstructure:"enable_mousewheel"`
PrettyMarkdown bool `mapstructure:"pretty_markdown"`
}
//DirTreeColors struct represents the colors for the dirtree.
type DirTreeColors struct {
SelectedItem string `mapstructure:"selected_item"`
UnselectedItem string `mapstructure:"unselected_item"`
}
// PaneColors represents the colors for a pane.
type PaneColors struct {
ActiveBorderColor string `mapstructure:"active_border_color"`
InactiveBorderColor string `mapstructure:"inactive_border_color"`
}
// ColorVariant struct represents a color.
type ColorVariant struct {
Foreground string `mapstructure:"foreground"`
Background string `mapstructure:"background"`
}
// StatusBarColors represents the colors for the status bar.
type StatusBarColors struct {
SelectedFile ColorVariant `mapstructure:"selected_file"`
Bar ColorVariant `mapstructure:"bar"`
TotalFiles ColorVariant `mapstructure:"total_files"`
Logo ColorVariant `mapstructure:"logo"`
}
// ColorsConfig struct represets the colors of the UI.
type ColorsConfig struct {
DirTree DirTreeColors `mapstructure:"dir_tree"`
Pane PaneColors `mapstructure:"pane"`
Spinner string `mapstructure:"spinner"`
StatusBar StatusBarColors `mapstructure:"status_bar"`
}
// Config represents the main config for the application.
type Config struct {
Settings SettingsConfig `mapstructure:"settings"`
Colors ColorsConfig `mapstructure:"colors"`
}
// LoadConfig loads a users config and creates the config if it does not exist
// located at ~/.fm.yml.
func LoadConfig() {
viper.AddConfigPath("$HOME")
viper.SetConfigName(".fm")
viper.SetConfigType("yml")
if err := viper.SafeWriteConfig(); err != nil {
if os.IsNotExist(err) {
err = viper.WriteConfig()
if err != nil {
log.Fatal(err)
}
}
}
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
log.Fatal(err)
} else {
log.Fatal(err)
}
}
}
// GetConfig returns the users config.
func GetConfig() (config Config) {
if err := viper.Unmarshal(&config); err != nil {
log.Fatal("Error parsing config", err)
}
return
}
// SetDefaults sets default values for the config.
func SetDefaults() {
// App Settings.
viper.SetDefault("settings.start_dir", ".")
viper.SetDefault("settings.show_icons", true)
viper.SetDefault("settings.rounded_panes", false)
viper.SetDefault("settings.enable_logging", false)
viper.SetDefault("settings.enable_mousewheel", true)
viper.SetDefault("settings.pretty_markdown", true)
// DirTree colors.
viper.SetDefault("colors.dir_tree.selected_item", constants.Colors.Pink)
viper.SetDefault("colors.dir_tree.unselected_item", constants.Colors.White)
// Pane colors.
viper.SetDefault("colors.pane.active_border_color", constants.Colors.Pink)
viper.SetDefault("colors.pane.inactive_border_color", constants.Colors.White)
// Spinner colors.
viper.SetDefault("colors.spinner", constants.Colors.Pink)
// StatusBar colors.
viper.SetDefault("colors.status_bar.selected_file.foreground", constants.Colors.White)
viper.SetDefault("colors.status_bar.selected_file.background", constants.Colors.Pink)
viper.SetDefault("colors.status_bar.bar.foreground", constants.Colors.White)
viper.SetDefault("colors.status_bar.bar.background", constants.Colors.DarkGray)
viper.SetDefault("colors.status_bar.total_files.foreground", constants.Colors.White)
viper.SetDefault("colors.status_bar.total_files.background", constants.Colors.LightPurple)
viper.SetDefault("colors.status_bar.logo.foreground", constants.Colors.White)
viper.SetDefault("colors.status_bar.logo.background", constants.Colors.DarkPurple)
}