1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/gui/file_watching.go
Jesse Duffield f15e47bb67 add file watching for modified files
log createErrorPanel error

swallow error when adding file to watcher
2019-11-14 22:22:47 +11:00

65 lines
1.5 KiB
Go

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
}