preview files asynchronously

Mentioned in #5 and #92.
This commit is contained in:
Gokcehan 2018-01-28 20:13:28 +03:00
parent 59b2001253
commit 80f43fe39c
2 changed files with 82 additions and 52 deletions

9
app.go
View File

@ -106,6 +106,15 @@ func (app *app) loop() {
}
}
app.ui.draw(app.nav)
case p := <-app.ui.prevChan:
curr, err := app.nav.currFile()
if err == nil {
if p.path == curr.path {
app.ui.regPrev = p.lines
}
}
app.ui.draw(app.nav)
case e := <-clientChan:
for i := 0; i < e.count; i++ {

45
ui.go
View File

@ -381,6 +381,7 @@ type ui struct {
msg string
regPrev []string
dirPrev *dir
prevChan chan preview
keyChan chan string
evChan chan termbox.Event
menuBuf *bytes.Buffer
@ -444,6 +445,7 @@ func newUI() *ui {
pwdWin: newWin(wtot, 1, 0, 0),
msgWin: newWin(wtot, 1, 0, htot-1),
menuWin: newWin(wtot, 1, 0, htot-2),
prevChan: make(chan preview),
keyChan: make(chan string, 1000),
evChan: evChan,
}
@ -473,20 +475,17 @@ func (ui *ui) printf(format string, a ...interface{}) {
ui.print(fmt.Sprintf(format, a...))
}
func (ui *ui) loadFile(nav *nav) {
type preview struct {
path string
lines []string
}
func (ui *ui) preview(nav *nav) {
curr, err := nav.currFile()
if err != nil {
return
}
if !gOpts.preview {
return
}
if curr.IsDir() {
dir := nav.load(curr.path)
ui.dirPrev = dir
} else if curr.Mode().IsRegular() {
var reader io.Reader
if len(gOpts.previewer) != 0 {
@ -514,23 +513,45 @@ func (ui *ui) loadFile(nav *nav) {
reader = f
}
ui.regPrev = nil
var prev preview
prev.path = curr.path
buf := bufio.NewScanner(reader)
for i := 0; i < nav.height && buf.Scan(); i++ {
for _, r := range buf.Text() {
if r == 0 {
ui.regPrev = []string{"\033[1mbinary\033[0m"}
prev.lines = []string{"\033[1mbinary\033[0m"}
ui.prevChan <- prev
return
}
}
ui.regPrev = append(ui.regPrev, buf.Text())
prev.lines = append(prev.lines, buf.Text())
}
if buf.Err() != nil {
ui.printf("loading file: %s", buf.Err())
}
ui.prevChan <- prev
}
func (ui *ui) loadFile(nav *nav) {
curr, err := nav.currFile()
if err != nil {
return
}
if !gOpts.preview {
return
}
if curr.IsDir() {
ui.dirPrev = nav.load(curr.path)
} else if curr.Mode().IsRegular() {
ui.regPrev = []string{"\033[1mloading...\033[0m"}
go ui.preview(nav)
}
}