Add ability to view old log messages on the console (#83)

master
HimbeerserverDE 2021-04-22 19:07:36 +02:00
parent 4883552ecc
commit 81b8b04e01
No known key found for this signature in database
GPG Key ID: 1A651504791E6A8B
2 changed files with 40 additions and 6 deletions

View File

@ -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])
}
}()
}

20
log.go
View File

@ -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
}