Skip to content

Commit

Permalink
Improve follow/followAll/followSection
Browse files Browse the repository at this point in the history
Fix followAll for omission of doc.height update.
Reduce branching of follow* to make it clearer.
  • Loading branch information
noborus committed Oct 31, 2024
1 parent 567af05 commit 06dcf69
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
34 changes: 24 additions & 10 deletions oviewer/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,22 +744,23 @@ func (root *Root) updateEndNum() {
root.Screen.Sync()
}

// follow updates the document in follow mode.
func (root *Root) follow(ctx context.Context) {
if root.General.FollowAll {
root.followAll(ctx)
}
// updateLatestNum updates the last line number in follow mode.
func (root *Root) updateLatestNum() bool {
num := root.Doc.BufEndNum()
if root.Doc.latestNum == num {
return
return false
}
root.skipDraw = false
if root.Doc.FollowSection {
root.tailSection(ctx)
} else {
root.Doc.latestNum = num
return true
}

// follow monitors and switches the document update
// in follow mode.
func (root *Root) follow(ctx context.Context) {
if root.updateLatestNum() {
root.TailSync(ctx)
}
root.Doc.latestNum = num
}

// followAll monitors and switches all document updates
Expand All @@ -772,6 +773,8 @@ func (root *Root) followAll(ctx context.Context) {
current := root.CurrentDoc
root.mu.RLock()
for n, doc := range root.DocList {
doc.width = root.scr.vWidth - root.scr.startX
doc.height = doc.statusPos - doc.headerHeight
if doc.latestNum != doc.BufEndNum() {
current = n
}
Expand All @@ -781,6 +784,17 @@ func (root *Root) followAll(ctx context.Context) {
if root.CurrentDoc != current {
root.switchDocument(ctx, current)
}
if root.updateLatestNum() {
root.TailSync(ctx)
}
}

// followSection monitors and switches the document update
// in follow section mode.
func (root *Root) followSection(ctx context.Context) {
if root.updateLatestNum() {
root.tailSection(ctx)
}
}

// Cancel follow mode and follow all mode.
Expand Down
2 changes: 1 addition & 1 deletion oviewer/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1378,7 +1378,7 @@ func TestRoot_followAll(t *testing.T) {
ctx := context.Background()
root.prepareScreen()
root.everyUpdate(ctx)
root.follow(ctx)
root.followAll(ctx)
if root.Doc.topLN != tt.want {
t.Errorf("follow() topLN = %v, want %v", root.Doc.topLN, tt.want)
}
Expand Down
8 changes: 6 additions & 2 deletions oviewer/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,13 @@ func (root *Root) everyUpdate(ctx context.Context) {

root.Doc.width = root.scr.vWidth - root.scr.startX
root.Doc.height = root.Doc.statusPos - root.Doc.headerHeight

if root.General.FollowAll || root.Doc.FollowMode || root.Doc.FollowSection {
switch {
case root.General.FollowAll:
root.followAll(ctx)
case root.Doc.FollowMode:
root.follow(ctx)
case root.Doc.FollowSection:
root.followSection(ctx)
}

if !root.skipDraw && root.Doc.height > 0 {
Expand Down

0 comments on commit 06dcf69

Please sign in to comment.