From 06dcf69cae808f47e61bd65fc05342dd485ed63e Mon Sep 17 00:00:00 2001 From: Noboru Saito Date: Thu, 31 Oct 2024 14:16:50 +0900 Subject: [PATCH] Improve follow/followAll/followSection Fix followAll for omission of doc.height update. Reduce branching of follow* to make it clearer. --- oviewer/action.go | 34 ++++++++++++++++++++++++---------- oviewer/action_test.go | 2 +- oviewer/event.go | 8 ++++++-- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/oviewer/action.go b/oviewer/action.go index c75c82c8..d716e0a1 100644 --- a/oviewer/action.go +++ b/oviewer/action.go @@ -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 @@ -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 } @@ -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. diff --git a/oviewer/action_test.go b/oviewer/action_test.go index e903fd57..cf0e0c63 100644 --- a/oviewer/action_test.go +++ b/oviewer/action_test.go @@ -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) } diff --git a/oviewer/event.go b/oviewer/event.go index 3dbc9c04..460eafb7 100644 --- a/oviewer/event.go +++ b/oviewer/event.go @@ -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 {