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

Add View.MaxLines. #104

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ func (v *View) writeRune(x, y int, ch rune) error {
}
c.chr = ch
v.lines[y][x] = c
v.removeOldLines()
return nil
}

Expand Down Expand Up @@ -337,5 +338,6 @@ func (v *View) breakLine(x, y int) error {
copy(lines, v.lines[:y])
copy(lines[y+2:], v.lines[y+1:])
v.lines = lines
v.removeOldLines()
return nil
}
20 changes: 20 additions & 0 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

// If MaxLines > 0, only the last MaxLines lines in the buffer are preseved.
MaxLines int
}

type viewLine struct {
Expand Down Expand Up @@ -227,9 +230,26 @@ func (v *View) Write(p []byte) (n int, err error) {
}
}
}
v.removeOldLines()
return len(p), nil
}

// removeOldLines removed old lines from the buffer
func (v *View) removeOldLines() {
nl := len(v.lines)
if v.MaxLines <= 0 || nl <= v.MaxLines {
return
}
numRemoved := nl - v.MaxLines
v.lines = v.lines[numRemoved:]
if v.oy > 0 {
v.oy--
}
if v.cy > 0 {
v.cy--
}
}

// parseInput parses char by char the input written to the View. It returns nil
// while processing ESC sequences. Otherwise, it returns a cell slice that
// contains the processed data.
Expand Down