summaryrefslogtreecommitdiffstats
path: root/pkg/gui/file_watching.go
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2019-11-12 22:19:20 +1100
committerJesse Duffield <jessedduffield@gmail.com>2019-11-14 22:22:47 +1100
commitf15e47bb67f39904c624846cc8392f3ad14358a9 (patch)
tree2203a588fbd990e53a6eab534e39a78b8d127941 /pkg/gui/file_watching.go
parent7995d56a858fdeb399ed884a6983008aa2089cfb (diff)
add file watching for modified files
log createErrorPanel error swallow error when adding file to watcher
Diffstat (limited to 'pkg/gui/file_watching.go')
-rw-r--r--pkg/gui/file_watching.go64
1 files changed, 64 insertions, 0 deletions
diff --git a/pkg/gui/file_watching.go b/pkg/gui/file_watching.go
new file mode 100644
index 000000000..90fb32639
--- /dev/null
+++ b/pkg/gui/file_watching.go
@@ -0,0 +1,64 @@
+package gui
+
+import (
+ "os"
+ "path/filepath"
+
+ "github.com/fsnotify/fsnotify"
+ "github.com/jesseduffield/lazygit/pkg/commands"
+)
+
+// NOTE: given that we often edit files ourselves, this may make us end up refreshing files too often
+// TODO: consider watching the whole directory recursively (could be more expensive)
+func (gui *Gui) watchFilesForChanges() {
+ var err error
+ gui.fileWatcher, err = fsnotify.NewWatcher()
+ if err != nil {
+ gui.Log.Error(err)
+ return
+ }
+ go func() {
+ for {
+ select {
+ // watch for events
+ case event := <-gui.fileWatcher.Events:
+ if event.Op == fsnotify.Chmod {
+ // for some reason we pick up chmod events when they don't actually happen
+ continue
+ }
+ // only refresh if we're not already
+ if !gui.State.IsRefreshingFiles {
+ if err := gui.refreshFiles(); err != nil {
+ err = gui.createErrorPanel(gui.g, err.Error())
+ if err != nil {
+ gui.Log.Error(err)
+ }
+ }
+ }
+
+ // watch for errors
+ case err := <-gui.fileWatcher.Errors:
+ if err != nil {
+ gui.Log.Warn(err)
+ }
+ }
+ }
+ }()
+}
+
+func (gui *Gui) addFilesToFileWatcher(files []*commands.File) error {
+ // watch the files for changes
+ dirName, err := os.Getwd()
+ if err != nil {
+ return err
+ }
+
+ for _, file := range files {
+ if err := gui.fileWatcher.Add(filepath.Join(dirName, file.Name)); err != nil {
+ // swallowing errors here because it doesn't really matter if we can't watch a file
+ gui.Log.Warn(err)
+ }
+ }
+
+ return nil
+}