From 002d4e40af34fbe5b8eb6c00061b3384308a7ac9 Mon Sep 17 00:00:00 2001 From: Johan Walles Date: Sun, 30 May 2021 11:36:07 +0200 Subject: [PATCH] Modify input lines rendering API Make the rendering function return both the lines and the clipped first-line-number value. --- m/pager.go | 10 +++------- m/screenLines.go | 17 +++++++++-------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/m/pager.go b/m/pager.go index fc8e48e..7daf839 100644 --- a/m/pager.go +++ b/m/pager.go @@ -164,14 +164,10 @@ func (p *Pager) _Redraw(spinner string) { wrapLongLines: p.WrapLongLines, } - // If we're asking for past-the-end lines, the Reader will clip for us, - // and we should adapt to that. Otherwise if you scroll 100 lines past - // the end, you'll then have to scroll 100 lines up again before the - // display starts scrolling visibly. - p.firstLineOneBased = screenLines.firstLineOneBased() - var screenLineNumber int - for lineNumber, row := range screenLines.getScreenLines(p.searchPattern) { + var renderedScreenLines [][]twin.Cell + renderedScreenLines, p.firstLineOneBased = screenLines.renderScreenLines(p.searchPattern) + for lineNumber, row := range renderedScreenLines { screenLineNumber = lineNumber for column, cell := range row { p.screen.SetCell(column, screenLineNumber, cell) diff --git a/m/screenLines.go b/m/screenLines.go index 6bac186..8cb1c74 100644 --- a/m/screenLines.go +++ b/m/screenLines.go @@ -19,7 +19,11 @@ type ScreenLines struct { wrapLongLines bool } -func (sl *ScreenLines) getScreenLines(searchPattern *regexp.Regexp) [][]twin.Cell { +// Render screen lines into an array of lines consisting of Cells. +// +// The second return value is the same as firstInputLineOneBased, but clipped if +// needed so that the end of the input is visible. +func (sl *ScreenLines) renderScreenLines(searchPattern *regexp.Regexp) ([][]twin.Cell, int) { // Count the length of the last line number // // Offsets figured out through trial-and-error... @@ -39,7 +43,7 @@ func (sl *ScreenLines) getScreenLines(searchPattern *regexp.Regexp) [][]twin.Cel returnLines := make([][]twin.Cell, 0, sl.height) screenFull := false for lineIndex, line := range sl.inputLines.lines { - lineNumber := sl.firstLineOneBased() + lineIndex + lineNumber := sl.inputLines.firstLineOneBased + lineIndex highlighted := line.HighlightedTokens(searchPattern) var wrapped [][]twin.Cell @@ -71,7 +75,9 @@ func (sl *ScreenLines) getScreenLines(searchPattern *regexp.Regexp) [][]twin.Cel } } - return returnLines + // FIXME: We can't just use firstInputLineOneBased, in the presence of + // wrapped lines that number can be too low. + return returnLines, sl.firstInputLineOneBased } func (sl *ScreenLines) createScreenLine(lineNumberToShow *int, numberPrefixLength int, contents []twin.Cell) []twin.Cell { @@ -145,8 +151,3 @@ func createLineNumberPrefix(fileLineNumber *int, numberPrefixLength int) []twin. return lineNumberPrefix } - -func (sl *ScreenLines) firstLineOneBased() int { - // FIXME: This is wrong when wrapping is enabled - return sl.inputLines.firstLineOneBased -}