Files

62 lines
1.3 KiB
Go

package ui
import (
"fmt"
"time"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
const maxLogLines = 1000
// logView keeps the history of everything that was reported as a popup.
type logView struct {
root *tview.TextView
app *App
follow bool
}
func newLogView(app *App) *logView {
v := &logView{app: app, follow: true}
v.root = tview.NewTextView().SetDynamicColors(true).SetScrollable(true).SetMaxLines(maxLogLines)
v.root.SetBorder(true).SetTitle(" Log ")
v.root.SetInputCapture(func(ev *tcell.EventKey) *tcell.EventKey {
switch ev.Key() {
case tcell.KeyUp, tcell.KeyPgUp, tcell.KeyHome:
v.follow = false
case tcell.KeyEnd:
v.follow = true
case tcell.KeyRune:
switch ev.Rune() {
case 'c':
v.root.Clear()
return nil
case 'k':
v.follow = false
case 'G':
v.follow = true
v.root.ScrollToEnd()
return nil
}
}
return ev
})
return v
}
// add appends an entry.
func (v *logView) add(l level, text string) {
color := map[level]string{levelInfo: "white", levelSuccess: "green", levelWarn: "yellow", levelError: "red"}[l]
fmt.Fprintf(v.root, "[gray]%s[-] [%s]%-5s[-] %s\n", time.Now().Format("15:04:05"), color, l, tview.Escape(text))
if v.follow {
v.root.ScrollToEnd()
}
}
func (v *logView) onShow() {
if v.follow {
v.root.ScrollToEnd()
}
}