mirror of
https://github.com/charmbracelet/glow.git
synced 2026-08-09 09:49:09 +02:00
fix(watch): watch for dir instead of file to work on all scenarios (#734)
Co-authored-by: Andrey Nering <andreynering@users.noreply.github.com>
This commit is contained in:
parent
50a763ef94
commit
05872a506a
1 changed files with 18 additions and 15 deletions
33
ui/pager.go
33
ui/pager.go
|
|
@ -3,7 +3,6 @@ package ui
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -477,44 +476,48 @@ func (m *pagerModel) initWatcher() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *pagerModel) watchFile() tea.Msg {
|
func (m *pagerModel) watchFile() tea.Msg {
|
||||||
path := m.relFilePath()
|
dir := m.localDir()
|
||||||
|
|
||||||
if err := m.watcher.Add(path); err != nil {
|
if err := m.watcher.Add(dir); err != nil {
|
||||||
log.Error("error adding file to fsnotify watcher", "error", err)
|
log.Error("error adding dir to fsnotify watcher", "error", err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Info("fsnotify watching file", "file", path)
|
log.Info("fsnotify watching dir", "dir", dir)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case event, ok := <-m.watcher.Events:
|
case event, ok := <-m.watcher.Events:
|
||||||
if !ok || !event.Has(fsnotify.Write) {
|
if !ok || event.Name != m.currentDocument.localPath {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
log.Debug("fsnotify event", "file", path, "event", event.Op)
|
|
||||||
|
if !event.Has(fsnotify.Write) && !event.Has(fsnotify.Create) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debug("fsnotify event", "file", event.Name, "event", event.Op)
|
||||||
return reloadMsg{}
|
return reloadMsg{}
|
||||||
case err, ok := <-m.watcher.Errors:
|
case err, ok := <-m.watcher.Errors:
|
||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
log.Debug("fsnotify error", "file", path, "error", err)
|
log.Debug("fsnotify error", "dir", dir, "error", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *pagerModel) unwatchFile() {
|
func (m *pagerModel) unwatchFile() {
|
||||||
path := m.relFilePath()
|
dir := m.localDir()
|
||||||
|
|
||||||
err := m.watcher.Remove(path)
|
err := m.watcher.Remove(dir)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
log.Debug("fsnotify file unwatched", "file", path)
|
log.Debug("fsnotify dir unwatched", "dir", dir)
|
||||||
} else {
|
} else {
|
||||||
log.Error("fsnotify fail to unwatch file", "file", path, "error", err)
|
log.Error("fsnotify fail to unwatch dir", "dir", dir, "error", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *pagerModel) relFilePath() string {
|
func (m *pagerModel) localDir() string {
|
||||||
wd, _ := os.Getwd()
|
return filepath.Dir(m.currentDocument.localPath)
|
||||||
return stripAbsolutePath(m.currentDocument.localPath, wd)
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue