diff options
-rw-r--r-- | pkg/app/app.go | 2 | ||||
-rw-r--r-- | pkg/config/app_config.go | 68 | ||||
-rw-r--r-- | pkg/gui/gui.go | 23 |
3 files changed, 65 insertions, 28 deletions
diff --git a/pkg/app/app.go b/pkg/app/app.go index e18a98dc4..98da0b0fa 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -60,7 +60,7 @@ func NewApp(config config.AppConfigurer) (*App, error) { if err != nil { return nil, err } - app.Gui, err = gui.NewGui(app.Log, app.GitCommand, app.OSCommand, app.Tr, config.GetUserConfig(), config.GetVersion()) + app.Gui, err = gui.NewGui(app.Log, app.GitCommand, app.OSCommand, app.Tr, config) if err != nil { return nil, err } diff --git a/pkg/config/app_config.go b/pkg/config/app_config.go index f34848123..c9ab1b1d0 100644 --- a/pkg/config/app_config.go +++ b/pkg/config/app_config.go @@ -28,6 +28,7 @@ type AppConfigurer interface { GetBuildDate() string GetName() string GetUserConfig() *viper.Viper + InsertToUserConfig(string, string) error } // NewAppConfig makes a new app config @@ -78,31 +79,70 @@ func (c *AppConfig) GetUserConfig() *viper.Viper { return c.UserConfig } -// LoadUserConfig gets the user's config -func LoadUserConfig() (*viper.Viper, error) { +func newViper() (*viper.Viper, error) { v := viper.New() v.SetConfigType("yaml") - defaults := getDefaultConfig() - err := v.ReadConfig(bytes.NewBuffer(defaults)) + v.SetConfigName("config") + return v, nil +} + +// LoadUserConfig gets the user's config +func LoadUserConfig() (*viper.Viper, error) { + v, err := newViper() if err != nil { + panic(err) + } + if err = LoadDefaultConfig(v); err != nil { return nil, err } - v.SetConfigName("config") + if err = LoadUserConfigFromFile(v); err != nil { + return nil, err + } + return v, nil +} +// LoadDefaultConfig loads in the defaults defined in this file +func LoadDefaultConfig(v *viper.Viper) error { + defaults := getDefaultConfig() + return v.ReadConfig(bytes.NewBuffer(defaults)) +} + +// LoadUserConfigFromFile Loads the user config from their config file, creating +// the file as an empty config if it does not exist +func LoadUserConfigFromFile(v *viper.Viper) error { // chucking my name there is not for vanity purposes, the xdg spec (and that // function) requires a vendor name. May as well line up with github configDirs := configdir.New("jesseduffield", "lazygit") folder := configDirs.QueryFolderContainsFile("config.yml") - if folder != nil { - configData, err := folder.ReadFile("config.yml") - if err != nil { - return nil, err - } - if err = v.MergeConfig(bytes.NewReader(configData)); err != nil { - return nil, err - } + if folder == nil { + // create the file as an empty config and load it + folders := configDirs.QueryFolders(configdir.Global) + folders[0].WriteFile("config.yml", []byte{}) + folder = configDirs.QueryFolderContainsFile("config.yml") } - return v, nil + v.AddConfigPath(folder.Path) + if err := v.MergeInConfig(); err != nil { + return err + } + return nil +} + +// InsertToUserConfig adds a key/value pair to the user's config and saves it +func (c *AppConfig) InsertToUserConfig(key, value string) error { + // making a new viper object so that we're not writing any defaults back + // to the user's config file + v, err := newViper() + if err != nil { + return err + } + if err = LoadUserConfigFromFile(v); err != nil { + return err + } + v.Set(key, value) + if err := v.WriteConfig(); err != nil { + return err + } + return nil } func getDefaultConfig() []byte { diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 71bfdbc48..21a7f5ebc 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -19,8 +19,8 @@ import ( "github.com/golang-collections/collections/stack" "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/commands" + "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/i18n" - "github.com/spf13/viper" ) // OverlappingEdges determines if panel edges overlap @@ -59,10 +59,9 @@ type Gui struct { Log *logrus.Logger GitCommand *commands.GitCommand OSCommand *commands.OSCommand - Version string SubProcess *exec.Cmd State guiState - Config *viper.Viper + Config config.AppConfigurer Tr *i18n.Localizer Errors SentinelErrors } @@ -79,11 +78,10 @@ type guiState struct { Conflicts []commands.Conflict EditHistory *stack.Stack Platform commands.Platform - Version string } // NewGui builds a new gui handler -func NewGui(log *logrus.Logger, gitCommand *commands.GitCommand, oSCommand *commands.OSCommand, tr *i18n.Localizer, userConfig *viper.Viper, version string) (*Gui, error) { +func NewGui(log *logrus.Logger, gitCommand *commands.GitCommand, oSCommand *commands.OSCommand, tr *i18n.Localizer, config config.AppConfigurer) (*Gui, error) { initialState := guiState{ Files: make([]commands.File, 0), PreviousView: "files", @@ -94,16 +92,14 @@ func NewGui(log *logrus.Logger, gitCommand *commands.GitCommand, oSCommand *comm Conflicts: make([]commands.Conflict, 0), EditHistory: stack.New(), Platform: *oSCommand.Platform, - Version: version, } gui := &Gui{ Log: log, GitCommand: gitCommand, OSCommand: oSCommand, - Version: version, State: initialState, - Config: userConfig, + Config: config, Tr: tr, } @@ -116,7 +112,7 @@ func (gui *Gui) scrollUpMain(g *gocui.Gui, v *gocui.View) error { mainView, _ := g.View("main") ox, oy := mainView.Origin() if oy >= 1 { - return mainView.SetOrigin(ox, oy-gui.Config.GetInt("gui.scrollHeight")) + return mainView.SetOrigin(ox, oy-gui.Config.GetUserConfig().GetInt("gui.scrollHeight")) } return nil } @@ -125,7 +121,7 @@ func (gui *Gui) scrollDownMain(g *gocui.Gui, v *gocui.View) error { mainView, _ := g.View("main") ox, oy := mainView.Origin() if oy < len(mainView.BufferLines()) { - return mainView.SetOrigin(ox, oy+gui.Config.GetInt("gui.scrollHeight")) + return mainView.SetOrigin(ox, oy+gui.Config.GetUserConfig().GetInt("gui.scrollHeight")) } return nil } @@ -153,6 +149,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { commitsStashBoundary := height - 5 // height - 5 minimumHeight := 16 minimumWidth := 10 + version := gui.Config.GetVersion() panelSpacing := 1 if OverlappingEdges { @@ -231,7 +228,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { v.FgColor = gocui.ColorWhite } - if v, err := g.SetView("options", -1, optionsTop, width-len(gui.Version)-2, optionsTop+2, 0); err != nil { + if v, err := g.SetView("options", -1, optionsTop, width-len(version)-2, optionsTop+2, 0); err != nil { if err != gocui.ErrUnknownView { return err } @@ -252,14 +249,14 @@ func (gui *Gui) layout(g *gocui.Gui) error { } } - if v, err := g.SetView("version", width-len(gui.Version)-1, optionsTop, width, optionsTop+2, 0); err != nil { + if v, err := g.SetView("version", width-len(version)-1, optionsTop, width, optionsTop+2, 0); err != nil { if err != gocui.ErrUnknownView { return err } v.BgColor = gocui.ColorDefault v.FgColor = gocui.ColorGreen v.Frame = false - gui.renderString(g, "version", gui.Version) + gui.renderString(g, "version", version) // these are only called once gui.handleFileSelect(g, filesView) |