Skip to content

Commit

Permalink
Merge pull request #671 from noborus/fix-follow-and-follow-all
Browse files Browse the repository at this point in the history
Fix the first position of Follow and Followall
  • Loading branch information
noborus authored Dec 23, 2024
2 parents 2e12310 + 5c00767 commit 8368efc
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 167 deletions.
11 changes: 11 additions & 0 deletions oviewer/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,17 @@ func (m *Document) requestLoad(chunkNum int) {
}()
}

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

// requestSearch sends instructions to load chunks into memory.
func (m *Document) requestSearch(chunkNum int, searcher Searcher) bool {
sc := controlSpecifier{
Expand Down
2 changes: 1 addition & 1 deletion oviewer/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func docHelper(t *testing.T, str string) *Document {
if err != nil {
t.Fatal(err)
}
if err := m.ReadAll(bytes.NewBufferString(str)); err != nil {
if err := m.ControlReader(bytes.NewBufferString(str), nil); err != nil {
t.Fatal(err)
}
for !m.BufEOF() {
Expand Down
4 changes: 2 additions & 2 deletions oviewer/draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,8 @@ func (root *Root) applyMarkStyle(lN int, y int, width int) {
// applyStyleToRange applies the style from the start to the end of the physical line.
func (root *Root) applyStyleToRange(y int, s OVStyle, start int, end int) {
for x := start; x < end; x++ {
r, c, ts, _ := root.GetContent(x, y)
root.Screen.SetContent(x, y, r, c, applyStyle(ts, s))
mainc, combc, style, _ := root.GetContent(x, y)
root.Screen.SetContent(x, y, mainc, combc, applyStyle(style, s))
}
}

Expand Down
4 changes: 3 additions & 1 deletion oviewer/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ func (root *Root) everyUpdate(ctx context.Context) {
// This process is executed when temporary read is switched to normal read.
if n := atomic.SwapInt32(&root.Doc.tmpLN, 0); n > 0 {
tmpN := int(n) - root.Doc.topLN
root.Doc.topLN = root.Doc.BufEndNum() - tmpN
if tmpN > 0 {
root.Doc.topLN = root.Doc.BufEndNum() - tmpN
}
}

root.Doc.width = root.scr.vWidth - root.scr.startX
Expand Down
4 changes: 2 additions & 2 deletions oviewer/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func ExampleNewOviewer() {
panic(err)
}
s := "Hello, World!"
if err := doc.ReadAll(bytes.NewBufferString(s)); err != nil {
if err := doc.ControlReader(bytes.NewBufferString(s), nil); err != nil {
panic(err)
}

Expand Down Expand Up @@ -65,7 +65,7 @@ func ExampleSearch() {
panic(err)
}
s := "Hello, World!"
if err := doc.ReadAll(bytes.NewBufferString(s)); err != nil {
if err := doc.ControlReader(bytes.NewBufferString(s), nil); err != nil {
panic(err)
}

Expand Down
12 changes: 8 additions & 4 deletions oviewer/move_vertical.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ func (m *Document) moveBottom() {
m.requestBottom()
}

m.topLX, m.topLN = m.bottomLineNum(m.BufEndNum()-1, m.height-lastLineMargin)
lN := m.BufEndNum() - 1
height := m.height - lastLineMargin
chunkNum, _ := chunkLineNum(lN - height)
m.requestLoadSync(chunkNum)
m.topLX, m.topLN = m.bottomLineNum(lN, height)
}

// Move to the nth wrapping line of the specified line.
Expand Down Expand Up @@ -101,9 +105,9 @@ func (m *Document) limitMoveDown(lX int, lN int) {
}
}

// numOfWrap returns the number of wrap from lX and lY.
func (m *Document) numOfWrap(lX int, lY int) int {
listX := m.leftMostX(lY)
// numOfWrap returns the number of wrap from lX and lN.
func (m *Document) numOfWrap(lX int, lN int) int {
listX := m.leftMostX(lN)
if len(listX) == 0 {
return 0
}
Expand Down
70 changes: 0 additions & 70 deletions oviewer/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package oviewer

import (
"bufio"
"context"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -404,72 +403,3 @@ func (m *Document) close() error {
atomic.StoreInt32(&m.store.changed, 1)
return nil
}

// ReadFile reads file.
// If the file name is empty, read from standard input.
//
// Deprecated: Use ControlFile instead.
func (m *Document) ReadFile(fileName string) error {
r, err := m.openFileReader(fileName)
if err != nil {
return err
}
return m.ReadAll(r)
}

// ContinueReadAll continues to read even if it reaches EOF.
//
// Deprecated: Use ControlFile instead.
func (m *Document) ContinueReadAll(_ context.Context, r io.Reader) error {
return m.ReadAll(r)
}

// ReadReader reads reader.
// A wrapper for ReadAll.
//
// Deprecated: Use ControlReader instead.
func (m *Document) ReadReader(r io.Reader) error {
return m.ReadAll(r)
}

// ReadAll reads all from the reader.
// And store it in the lines of the Document.
// ReadAll needs to be notified on eofCh.
//
// Deprecated: Use ControlReader instead.
func (m *Document) ReadAll(r io.Reader) error {
reader := bufio.NewReader(r)
go func() {
if m.checkClose() {
return
}

if err := m.readAll(reader); err != nil {
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrClosedPipe) || errors.Is(err, os.ErrClosed) {
m.store.offset = m.store.size
atomic.StoreInt32(&m.store.eof, 1)
return
}
log.Printf("error: %v\n", err)
return
}
}()
return nil
}

// readAll reads to the end.
// The read lines are stored in the lines of the Document.
func (m *Document) readAll(reader *bufio.Reader) error {
chunk := m.store.chunkForAdd(m.seekable, m.store.size)
start := len(chunk.lines)
for {
if err := m.store.readLines(chunk, reader, start, ChunkSize, true); err != nil {
return err
}
chunk = NewChunk(m.store.size)
m.store.mu.Lock()
m.store.chunks = append(m.store.chunks, chunk)
m.store.mu.Unlock()
start = 0
}
}
87 changes: 0 additions & 87 deletions oviewer/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,98 +3,11 @@ package oviewer
import (
"bufio"
"bytes"
"io"
"path/filepath"
"strings"
"sync/atomic"
"testing"
)

func TestDocument_ReadFile(t *testing.T) {
t.Parallel()
type args struct {
args string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "testNoFile",
args: args{
"noFile",
},
wantErr: true,
},
{
name: "testSTDIN",
args: args{
"",
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
m, err := NewDocument()
if err != nil {
t.Fatal(err)
}
if err := m.ReadFile(tt.args.args); (err != nil) != tt.wantErr {
t.Errorf("Document.ReadFile() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestDocument_ReadAll(t *testing.T) {
t.Parallel()
type args struct {
r io.Reader
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "test1",
args: args{
r: bytes.NewBufferString("foo\nbar\n"),
},
wantErr: false,
},
{
name: "testOverChunkSize",
args: args{
r: bytes.NewBufferString(strings.Repeat("a\n", ChunkSize+1)),
},
wantErr: false,
},
{
name: "testLongLine",
args: args{
r: bytes.NewBufferString(strings.Repeat("a", 4097)),
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
m, err := NewDocument()
if err != nil {
t.Fatal(err)
}
if err := m.ReadAll(tt.args.r); (err != nil) != tt.wantErr {
t.Errorf("Document.ReadAll() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestDocument_reset(t *testing.T) {
t.Parallel()
type fields struct {
Expand Down

0 comments on commit 8368efc

Please sign in to comment.