1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-04 23:37:41 +02:00

Reload changed user config files on terminal focus

This commit is contained in:
Stefan Haller 2024-07-15 13:44:31 +02:00
parent 18ad975573
commit ce50533689
2 changed files with 53 additions and 5 deletions

View File

@ -6,6 +6,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"time"
"github.com/adrg/xdg" "github.com/adrg/xdg"
"github.com/jesseduffield/lazygit/pkg/utils/yaml_utils" "github.com/jesseduffield/lazygit/pkg/utils/yaml_utils"
@ -40,6 +41,7 @@ type AppConfigurer interface {
GetUserConfigPaths() []string GetUserConfigPaths() []string
GetUserConfigDir() string GetUserConfigDir() string
ReloadUserConfigForRepo(repoConfigFiles []*ConfigFile) error ReloadUserConfigForRepo(repoConfigFiles []*ConfigFile) error
ReloadChangedUserConfigFiles() (error, bool)
GetTempDir() string GetTempDir() string
GetAppState() *AppState GetAppState() *AppState
@ -55,9 +57,10 @@ const (
) )
type ConfigFile struct { type ConfigFile struct {
Path string Path string
Policy ConfigFilePolicy Policy ConfigFilePolicy
exists bool modDate time.Time
exists bool
} }
// NewAppConfig makes a new app config // NewAppConfig makes a new app config
@ -146,9 +149,10 @@ func loadUserConfigWithDefaults(configFiles []*ConfigFile) (*UserConfig, error)
func loadUserConfig(configFiles []*ConfigFile, base *UserConfig) (*UserConfig, error) { func loadUserConfig(configFiles []*ConfigFile, base *UserConfig) (*UserConfig, error) {
for _, configFile := range configFiles { for _, configFile := range configFiles {
path := configFile.Path path := configFile.Path
_, err := os.Stat(path) statInfo, err := os.Stat(path)
if err == nil { if err == nil {
configFile.exists = true configFile.exists = true
configFile.modDate = statInfo.ModTime()
} else { } else {
if !os.IsNotExist(err) { if !os.IsNotExist(err) {
return nil, err return nil, err
@ -174,6 +178,11 @@ func loadUserConfig(configFiles []*ConfigFile, base *UserConfig) (*UserConfig, e
file.Close() file.Close()
configFile.exists = true configFile.exists = true
statInfo, err := os.Stat(configFile.Path)
if err != nil {
return nil, err
}
configFile.modDate = statInfo.ModTime()
} }
} }
@ -295,6 +304,30 @@ func (c *AppConfig) ReloadUserConfigForRepo(repoConfigFiles []*ConfigFile) error
return nil return nil
} }
func (c *AppConfig) ReloadChangedUserConfigFiles() (error, bool) {
fileHasChanged := func(f *ConfigFile) bool {
info, err := os.Stat(f.Path)
if err != nil && !os.IsNotExist(err) {
// If we can't stat the file, assume it hasn't changed
return false
}
exists := err == nil
return exists != f.exists || (exists && info.ModTime() != f.modDate)
}
if lo.NoneBy(c.userConfigFiles, fileHasChanged) {
return nil, false
}
userConfig, err := loadUserConfigWithDefaults(c.userConfigFiles)
if err != nil {
return err, false
}
c.userConfig = userConfig
return nil, true
}
func (c *AppConfig) GetTempDir() string { func (c *AppConfig) GetTempDir() string {
return c.tempDir return c.tempDir
} }

View File

@ -331,8 +331,23 @@ func (gui *Gui) onNewRepo(startArgs appTypes.StartArgs, contextKey types.Context
gui.g.SetFocusHandler(func(Focused bool) error { gui.g.SetFocusHandler(func(Focused bool) error {
if Focused { if Focused {
reloadErr, didChange := gui.Config.ReloadChangedUserConfigFiles()
if didChange && reloadErr == nil {
gui.c.Log.Info("User config changed - reloading")
reloadErr = gui.onUserConfigLoaded()
if err := gui.resetKeybindings(); err != nil {
return err
}
}
gui.c.Log.Info("Receiving focus - refreshing") gui.c.Log.Info("Receiving focus - refreshing")
return gui.helpers.Refresh.Refresh(types.RefreshOptions{Mode: types.ASYNC}) refreshErr := gui.helpers.Refresh.Refresh(types.RefreshOptions{Mode: types.ASYNC})
if reloadErr != nil {
// An error from reloading the config is the more important one
// to report to the user
return reloadErr
}
return refreshErr
} }
return nil return nil