reload modified files when loading cache

Related #92
This commit is contained in:
Gokcehan 2018-06-07 22:49:53 +03:00
parent 4846a74320
commit 7acc85f81c
2 changed files with 23 additions and 4 deletions

15
nav.go
View File

@ -370,7 +370,7 @@ func (nav *nav) preview() {
reader = f reader = f
} }
reg := &reg{path: curr.path} reg := &reg{loadTime: time.Now(), path: curr.path}
buf := bufio.NewScanner(reader) buf := bufio.NewScanner(reader)
@ -396,10 +396,21 @@ func (nav *nav) loadReg(ui *ui, path string) *reg {
r, ok := nav.regCache[path] r, ok := nav.regCache[path]
if !ok { if !ok {
go nav.preview() go nav.preview()
r := &reg{path: path, lines: []string{"\033[1mloading...\033[0m"}} r := &reg{loading: true, path: path}
nav.regCache[path] = r nav.regCache[path] = r
return 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 return r
} }

12
ui.go
View File

@ -181,6 +181,12 @@ func (win *win) printReg(reg *reg) {
fg, bg := termbox.ColorDefault, termbox.ColorDefault 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 { for i, l := range reg.lines {
fg, bg = win.print(2, i, fg, bg, l) fg, bg = win.print(2, i, fg, bg, l)
} }
@ -462,8 +468,10 @@ func (ui *ui) printf(format string, a ...interface{}) {
} }
type reg struct { type reg struct {
path string loading bool
lines []string loadTime time.Time
path string
lines []string
} }
func (ui *ui) loadFile(nav *nav) { func (ui *ui) loadFile(nav *nav) {