add multi-line scrolling support, timestamps to error view

This commit is contained in:
Bradley Cicenas 2019-05-22 17:38:01 +00:00
parent 98fcfe8b6f
commit 0a5a4c9062
3 changed files with 35 additions and 12 deletions

View File

@ -68,7 +68,7 @@ func (cs *ConnectorSuper) loop() {
conn, err := cs.connFn()
if err != nil {
cs.setError(fmt.Errorf("%s\n\nattempting to reconnect...", err))
cs.setError(err)
log.Errorf("failed to initialize connector: %s (%T)", err, err)
log.Errorf("retrying in %ds", interval)
time.Sleep(interval * time.Second)

View File

@ -12,7 +12,8 @@ func ShowConnError(err error) (exit bool) {
defer ui.DefaultEvtStream.ResetHandlers()
setErr := func(err error) {
errView.Text = err.Error()
errView.Append(err.Error())
errView.Append("attempting to reconnect...")
ui.Render(errView)
}
@ -21,7 +22,7 @@ func ShowConnError(err error) (exit bool) {
ui.StopLoop()
})
ui.Handle("/timer/1s", func(ui.Event) {
ui.Handle("/timer/2s", func(ui.Event) {
_, err := cursor.RefreshContainers()
if err == nil {
ui.StopLoop()

View File

@ -2,37 +2,59 @@ package widgets
import (
"fmt"
"strings"
"time"
ui "github.com/gizak/termui"
)
type ErrorView struct {
*ui.Par
lines []string
}
func NewErrorView() *ErrorView {
const yPad = 1
const xPad = 2
p := ui.NewPar("")
p.X = xPad
p.Y = yPad
p.Border = true
p.Height = 10
p.Width = 20
p.PaddingTop = 1
p.PaddingBottom = 1
p.PaddingLeft = 2
p.PaddingRight = 2
p.PaddingTop = yPad
p.PaddingBottom = yPad
p.PaddingLeft = xPad
p.PaddingRight = xPad
p.BorderLabel = " ctop - error "
p.Bg = ui.ThemeAttr("bg")
p.TextFgColor = ui.ThemeAttr("status.warn")
p.TextBgColor = ui.ThemeAttr("menu.text.bg")
p.BorderFg = ui.ThemeAttr("status.warn")
p.BorderLabelFg = ui.ThemeAttr("status.warn")
return &ErrorView{p}
return &ErrorView{p, make([]string, 0, 50)}
}
func (w *ErrorView) Append(s string) {
if len(w.lines)+2 >= cap(w.lines) {
w.lines = append(w.lines[:0], w.lines[2:]...)
}
ts := time.Now().Local().Format("15:04:05 MST")
w.lines = append(w.lines, fmt.Sprintf("[%s] %s", ts, s))
w.lines = append(w.lines, "")
}
func (w *ErrorView) Buffer() ui.Buffer {
w.BorderLabel = fmt.Sprintf(" %s ", timeStr())
offset := len(w.lines) - w.InnerHeight()
if offset < 0 {
offset = 0
}
w.Text = strings.Join(w.lines[offset:len(w.lines)], "\n")
return w.Par.Buffer()
}
func (w *ErrorView) Resize() {
w.SetX(ui.TermWidth() / 12)
w.SetY(ui.TermHeight() / 3)
w.SetWidth(w.X * 10)
w.Height = ui.TermHeight() - (w.PaddingTop + w.PaddingBottom)
w.SetWidth(ui.TermWidth() - (w.PaddingLeft + w.PaddingRight))
}