diff --git a/console.go b/console.go index 5de51da..970a17e 100644 --- a/console.go +++ b/console.go @@ -112,6 +112,24 @@ func initCurses(l *Logger) { consoleInput = h.Next() case 4: consoleInput = h.Prev(consoleInput) + case 5: + rows, _ := gocurses.Getmaxyx() + start := len(l.lines) - rows + 1 - l.offset + if start < 0 { + start = 0 + } + + if start > 0 { + l.offset += 1 + if l.offset > len(l.lines)-1 { + l.offset = len(l.lines) - 1 + } + } + case 6: + l.offset -= 1 + if l.offset < 0 { + l.offset = 0 + } case '\b': if len(consoleInput) > 0 { consoleInput = consoleInput[:len(consoleInput)-1] @@ -142,7 +160,13 @@ func initCurses(l *Logger) { consoleInput = append(consoleInput, ch) } - draw(l.visible) + rows, _ := gocurses.Getmaxyx() + start := len(l.lines) - rows + 1 - l.offset + if start < 0 { + start = 0 + } + + draw(l.lines[start : len(l.lines)-l.offset]) } }() } diff --git a/log.go b/log.go index 98c4e6d..164f8d2 100644 --- a/log.go +++ b/log.go @@ -10,6 +10,8 @@ import ( "github.com/tncardoso/gocurses" ) +const MaxLogMSGs = 1024 + var logReady chan struct{} func appendPop(max int, a []string, v ...string) []string { @@ -26,8 +28,9 @@ func appendPop(max int, a []string, v ...string) []string { } type Logger struct { - visible []string - all []byte + lines []string + all []byte + offset int } func newLogger() *Logger { @@ -46,13 +49,20 @@ func (l *Logger) Write(p []byte) (int, error) { line = date + " " + line } - l.visible = appendPop(rows-1, l.visible, line) + + l.lines = appendPop(MaxLogMSGs, l.lines, line) + if l.offset > 0 { + l.offset += 1 + } } - draw(l.visible) + start := len(l.lines) - rows + 1 - l.offset + if start < 0 { + start = 0 + } + draw(l.lines[start : len(l.lines)-l.offset]) l.all = append(l.all, p...) - return len(p), nil }