From 7acc85f81c4eca92f05894ac2052154a6627bccb Mon Sep 17 00:00:00 2001 From: Gokcehan Date: Thu, 7 Jun 2018 22:49:53 +0300 Subject: [PATCH] reload modified files when loading cache Related #92 --- nav.go | 15 +++++++++++++-- ui.go | 12 ++++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/nav.go b/nav.go index c3b507d..78009b6 100644 --- a/nav.go +++ b/nav.go @@ -370,7 +370,7 @@ func (nav *nav) preview() { reader = f } - reg := ®{path: curr.path} + reg := ®{loadTime: time.Now(), path: curr.path} buf := bufio.NewScanner(reader) @@ -396,10 +396,21 @@ func (nav *nav) loadReg(ui *ui, path string) *reg { r, ok := nav.regCache[path] if !ok { go nav.preview() - r := ®{path: path, lines: []string{"\033[1mloading...\033[0m"}} + r := ®{loading: true, path: path} nav.regCache[path] = r return r } + + s, err := os.Stat(r.path) + if err != nil { + return r + } + + if s.ModTime().After(r.loadTime) { + r.loadTime = time.Now() + go nav.preview() + } + return r } diff --git a/ui.go b/ui.go index ea3d826..e73dca3 100644 --- a/ui.go +++ b/ui.go @@ -181,6 +181,12 @@ func (win *win) printReg(reg *reg) { fg, bg := termbox.ColorDefault, termbox.ColorDefault + if reg.loading { + fg = termbox.AttrBold + win.print(2, 0, fg, bg, "loading...") + return + } + for i, l := range reg.lines { fg, bg = win.print(2, i, fg, bg, l) } @@ -462,8 +468,10 @@ func (ui *ui) printf(format string, a ...interface{}) { } type reg struct { - path string - lines []string + loading bool + loadTime time.Time + path string + lines []string } func (ui *ui) loadFile(nav *nav) {