Add ability to view old log messages on the console (#83)
parent
4883552ecc
commit
81b8b04e01
26
console.go
26
console.go
|
@ -112,6 +112,24 @@ func initCurses(l *Logger) {
|
||||||
consoleInput = h.Next()
|
consoleInput = h.Next()
|
||||||
case 4:
|
case 4:
|
||||||
consoleInput = h.Prev(consoleInput)
|
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':
|
case '\b':
|
||||||
if len(consoleInput) > 0 {
|
if len(consoleInput) > 0 {
|
||||||
consoleInput = consoleInput[:len(consoleInput)-1]
|
consoleInput = consoleInput[:len(consoleInput)-1]
|
||||||
|
@ -142,7 +160,13 @@ func initCurses(l *Logger) {
|
||||||
consoleInput = append(consoleInput, ch)
|
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
20
log.go
|
@ -10,6 +10,8 @@ import (
|
||||||
"github.com/tncardoso/gocurses"
|
"github.com/tncardoso/gocurses"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const MaxLogMSGs = 1024
|
||||||
|
|
||||||
var logReady chan struct{}
|
var logReady chan struct{}
|
||||||
|
|
||||||
func appendPop(max int, a []string, v ...string) []string {
|
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 {
|
type Logger struct {
|
||||||
visible []string
|
lines []string
|
||||||
all []byte
|
all []byte
|
||||||
|
offset int
|
||||||
}
|
}
|
||||||
|
|
||||||
func newLogger() *Logger {
|
func newLogger() *Logger {
|
||||||
|
@ -46,13 +49,20 @@ func (l *Logger) Write(p []byte) (int, error) {
|
||||||
|
|
||||||
line = date + " " + line
|
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...)
|
l.all = append(l.all, p...)
|
||||||
|
|
||||||
return len(p), nil
|
return len(p), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue