diff options
author | Mateusz Czapliński <czapkofan@gmail.com> | 2018-03-11 14:10:37 +0100 |
---|---|---|
committer | Mateusz Czapliński <czapkofan@gmail.com> | 2018-03-11 14:10:37 +0100 |
commit | b437f107da7b306ff5434f671e6aa0550b3c4773 (patch) | |
tree | b94581938b716d4c8e4cc64a41517a337a59e34e | |
parent | a09b7fd4c04b930abc0b6349fe4bdbd7c2a24df7 (diff) |
handle Pgup/Pgdn & add Y limit in Buf
-rw-r--r-- | up.go | 37 |
1 files changed, 32 insertions, 5 deletions
@@ -41,6 +41,7 @@ func main() { inputBuf = NewBuf() buf = inputBuf bufStyle = BufDrawing{} + bufY = 1 ) // In background, start collecting input from stdin to internal buffer of size 40 MB, then pause it @@ -62,7 +63,7 @@ main_loop: // Draw command input line editor.Draw(0, 0, true) - buf.Draw(1, bufStyle) + buf.Draw(bufY, bufStyle) termbox.Flush() // Handle events @@ -78,13 +79,22 @@ main_loop: case termbox.KeyCtrlC: // quit return + // TODO: move buf scroll handlers to Buf or BufDrawing struct case termbox.KeyArrowUp: - if bufStyle.Y > 0 { - bufStyle.Y-- - } + bufStyle.Y-- + bufStyle.NormalizeY(buf.Lines()) case termbox.KeyArrowDown: - // TODO: limit to real height of buf bufStyle.Y++ + bufStyle.NormalizeY(buf.Lines()) + case termbox.KeyPgdn: + // TODO: in top-right corner of Buf area, draw current line number & total # of lines + _, h := termbox.Size() + bufStyle.Y += h - bufY - 1 + bufStyle.NormalizeY(buf.Lines()) + case termbox.KeyPgup: + _, h := termbox.Size() + bufStyle.Y -= h - bufY - 1 + bufStyle.NormalizeY(buf.Lines()) } } } @@ -205,6 +215,14 @@ func (b *Buf) endline(x, y, screenw int) { } } +func (b *Buf) Lines() int { + b.nLock.Lock() + n := b.n + b.nLock.Unlock() + newlines := bytes.Count(b.bytes[:n], []byte{'\n'}) + return newlines + 1 +} + func (b *Buf) NewReader() io.Reader { // TODO: return EOF if input is fully buffered? i := 0 @@ -349,3 +367,12 @@ type BufDrawing struct { Y int // for pgup/pgdn scrolling) // TODO: X int (for left<->right scrolling) } + +func (b *BufDrawing) NormalizeY(nlines int) { + if b.Y >= nlines { + b.Y = nlines - 1 + } + if b.Y < 0 { + b.Y = 0 + } +} |