Skip to content

Commit

Permalink
Merge pull request #412 from noborus/loadrequest-async
Browse files Browse the repository at this point in the history
Make loadrequest asynchronous
  • Loading branch information
noborus authored Jul 13, 2023
2 parents 77a3b75 + fac3299 commit 807763c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
14 changes: 7 additions & 7 deletions oviewer/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,13 @@ func (m *Document) requestContinue() {

// requestLoad sends instructions to load chunks into memory.
func (m *Document) requestLoad(chunkNum int) {
sc := controlSpecifier{
request: requestLoad,
chunkNum: chunkNum,
done: make(chan bool),
}
m.ctlCh <- sc
<-sc.done
go func() {
sc := controlSpecifier{
request: requestLoad,
chunkNum: chunkNum,
}
m.ctlCh <- sc
}()
}

// requestSearch sends instructions to load chunks into memory.
Expand Down
8 changes: 8 additions & 0 deletions oviewer/control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@ func TestDocument_requestLoad(t *testing.T) {

m.requestLoad(tt.fields.chunkNum)

sc := controlSpecifier{
request: requestLoad,
chunkNum: tt.fields.chunkNum,
done: make(chan bool),
}
m.ctlCh <- sc
<-sc.done

chunkNum, cn := chunkLineNum(tt.fields.lineNum)
got, err := m.store.GetChunkLine(chunkNum, cn)
if (err != nil) != tt.wantErr {
Expand Down
20 changes: 13 additions & 7 deletions oviewer/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package oviewer

import (
"bytes"
"errors"
"fmt"
"io"
"io/fs"
Expand Down Expand Up @@ -299,11 +300,17 @@ func (m *Document) GetLine(n int) string {

// LineString returns one line from buffer.
func (m *Document) LineString(n int) string {
str, _ := m.LineStr(n)
return str
}

// LineStr returns one line from buffer.
func (m *Document) LineStr(n int) (string, error) {
s, err := m.Line(n)
if err != nil {
return gchalk.Red(err.Error())
return gchalk.Red(err.Error()), err
}
return string(s)
return string(s), nil
}

// CurrentLN returns the currently displayed line number.
Expand Down Expand Up @@ -355,8 +362,8 @@ func (m *Document) contents(lN int, tabWidth int) (contents, error) {
return nil, ErrOutOfRange
}

str := m.LineString(lN)
return parseString(str, tabWidth), nil
str, err := m.LineStr(lN)
return parseString(str, tabWidth), err
}

// getLineC returns contents from line number and tabWidth.
Expand All @@ -370,8 +377,7 @@ func (m *Document) getLineC(lN int, tabWidth int) (LineC, bool) {
}

org, err := m.contents(lN, tabWidth)
if err != nil {
// EOF
if err != nil && errors.Is(err, ErrOutOfRange) {
lc := make(contents, 1)
lc[0] = EOFContent
return LineC{
Expand All @@ -386,7 +392,7 @@ func (m *Document) getLineC(lN int, tabWidth int) (LineC, bool) {
str: str,
pos: pos,
}
if len(org) != 0 {
if err == nil {
m.cache.Add(lN, line)
}

Expand Down
6 changes: 5 additions & 1 deletion oviewer/move_vertical.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@ func (m *Document) bottomLineNum(lN int, height int) (int, int) {

// WrapMode
listX := m.leftMostX(lN)
topLX, topLN := m.numUp(listX[len(listX)-1], lN, height-1)
lX := 1
if len(listX) > 0 {
lX = listX[len(listX)-1]
}
topLX, topLN := m.numUp(lX, lN, height-1)
return topLX, topLN - m.firstLine()
}

Expand Down

0 comments on commit 807763c

Please sign in to comment.