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

Support maximum number of lines for a view #223

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 12 additions & 2 deletions view.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ type View struct {
// If Mask is true, the View will display the mask instead of the real
// content
Mask rune

// Set the maximum number of lines to store
MaxLines int
}

type viewLine struct {
Expand Down Expand Up @@ -215,7 +218,7 @@ func (v *View) Write(p []byte) (n int, err error) {
}
default:
cells := v.parseInput(ch)
if cells == nil {
if cells == nil || len(cells) <= 0 {
continue
}

Expand All @@ -227,6 +230,13 @@ func (v *View) Write(p []byte) (n int, err error) {
}
}
}

// Cap the maximum number of lines in the view
if v.MaxLines > 0 && len(v.lines) > v.MaxLines {
cut := len(v.lines) - v.MaxLines
v.lines = v.lines[cut:]
}

return len(p), nil
}

Expand Down Expand Up @@ -322,7 +332,7 @@ func (v *View) draw() error {
}

if v.Autoscroll && len(v.viewLines) > maxY {
v.oy = len(v.viewLines) - maxY
v.oy = len(v.viewLines) - maxY - 1
}
y := 0
for i, vline := range v.viewLines {
Expand Down