2019-11-12 13:19:20 +02:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2020-01-08 12:02:01 +02:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2019-11-12 13:19:20 +02:00
|
|
|
"github.com/fsnotify/fsnotify"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands"
|
2020-01-08 12:02:01 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-11-12 13:19:20 +02:00
|
|
|
)
|
|
|
|
|
2020-01-08 12:02:01 +02:00
|
|
|
// macs for some bizarre reason cap the number of watchable files to 256.
|
|
|
|
// there's no obvious platform agonstic way to check the situation of the user's
|
|
|
|
// computer so we're just arbitrarily capping at 200. This isn't so bad because
|
|
|
|
// file watching is only really an added bonus for faster refreshing.
|
2020-01-08 12:41:39 +02:00
|
|
|
const MAX_WATCHED_FILES = 50
|
2020-01-08 12:02:01 +02:00
|
|
|
|
|
|
|
type fileWatcher struct {
|
|
|
|
Watcher *fsnotify.Watcher
|
|
|
|
WatchedFilenames []string
|
|
|
|
Log *logrus.Entry
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFileWatcher(log *logrus.Entry) *fileWatcher {
|
|
|
|
watcher, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &fileWatcher{
|
|
|
|
Watcher: watcher,
|
|
|
|
Log: log,
|
|
|
|
WatchedFilenames: make([]string, 0, MAX_WATCHED_FILES),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *fileWatcher) watchingFilename(filename string) bool {
|
|
|
|
for _, watchedFilename := range w.WatchedFilenames {
|
|
|
|
if watchedFilename == filename {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *fileWatcher) popOldestFilename() {
|
|
|
|
// shift the last off the array to make way for this one
|
|
|
|
oldestFilename := w.WatchedFilenames[0]
|
|
|
|
w.WatchedFilenames = w.WatchedFilenames[1:]
|
|
|
|
if err := w.Watcher.Remove(oldestFilename); err != nil {
|
|
|
|
// swallowing errors here because it doesn't really matter if we can't unwatch a file
|
|
|
|
w.Log.Warn(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *fileWatcher) watchFilename(filename string) {
|
|
|
|
w.Log.Warn(filename)
|
|
|
|
if err := w.Watcher.Add(filename); err != nil {
|
|
|
|
// swallowing errors here because it doesn't really matter if we can't watch a file
|
|
|
|
w.Log.Warn(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// assume we're watching it now to be safe
|
|
|
|
w.WatchedFilenames = append(w.WatchedFilenames, filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *fileWatcher) addFilesToFileWatcher(files []*commands.File) error {
|
|
|
|
// watch the files for changes
|
|
|
|
dirName, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
filename := filepath.Join(dirName, file.Name)
|
|
|
|
if w.watchingFilename(filename) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(w.WatchedFilenames) > MAX_WATCHED_FILES {
|
|
|
|
w.popOldestFilename()
|
|
|
|
}
|
|
|
|
|
|
|
|
w.watchFilename(filename)
|
|
|
|
w.Log.Warn(spew.Sdump(w.WatchedFilenames))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-12 13:19:20 +02:00
|
|
|
// 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() {
|
2020-01-08 12:02:01 +02:00
|
|
|
gui.fileWatcher = NewFileWatcher(gui.Log)
|
|
|
|
if gui.fileWatcher == nil {
|
2019-11-12 13:19:20 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// watch for events
|
2020-01-08 12:02:01 +02:00
|
|
|
case event := <-gui.fileWatcher.Watcher.Events:
|
2019-11-12 13:19:20 +02:00
|
|
|
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
|
2020-01-08 12:02:01 +02:00
|
|
|
case err := <-gui.fileWatcher.Watcher.Errors:
|
2019-11-12 13:19:20 +02:00
|
|
|
if err != nil {
|
|
|
|
gui.Log.Warn(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|