Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve follow/followAll/followSection #645

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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