From b2994acdf8d45cc6daf7d39579d6801813ef9d88 Mon Sep 17 00:00:00 2001 From: Brandon Jordan Date: Sat, 11 Mar 2023 22:45:08 -0500 Subject: [PATCH] Improve Viewport Call Bell function when we reach the end and the beginning of the context we're scrolling. I wanted to add page up and page down so I moved move up and move down logic into functions, key presses for viewport will just give a smaller or greater value of how much to move up or down. --- viewport.go | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/viewport.go b/viewport.go index df73b11..1fa313f 100644 --- a/viewport.go +++ b/viewport.go @@ -83,6 +83,22 @@ func wrapString(str *string, limit int) (wrapped string) { return } +func moveUp(n int) { + if (lineIdx - 1) >= 0 { + lineIdx -= n + } else { + Bell() + } +} + +func moveDown(n int) { + if (rows + lineIdx - 1) < contentsLinesCount { + lineIdx += n + } else { + Bell() + } +} + func handleViewportKeys(key any) { select { case <-stopViewport: @@ -92,14 +108,14 @@ func handleViewportKeys(key any) { case keyboard.KeyCtrlC: StopPainting() stopViewport <- true + case keyboard.KeyPgup: + moveUp(5) + case keyboard.KeyPgdn: + moveDown(5) case keyboard.KeyArrowUp: - if (lineIdx - 1) >= 0 { - lineIdx-- - } + moveUp(1) case keyboard.KeyArrowDown: - if (rows + lineIdx - 1) < contentsLinesCount { - lineIdx++ - } + moveDown(1) } } }