From 65399a37e56a793c79254e648a7369ec2fd6f4f8 Mon Sep 17 00:00:00 2001 From: Bradley Cicenas Date: Thu, 29 Jun 2017 14:02:52 +0000 Subject: [PATCH] add log panel to expanded widgets --- cwidgets/expanded/logs.go | 80 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 cwidgets/expanded/logs.go diff --git a/cwidgets/expanded/logs.go b/cwidgets/expanded/logs.go new file mode 100644 index 0000000..356d225 --- /dev/null +++ b/cwidgets/expanded/logs.go @@ -0,0 +1,80 @@ +package expanded + +import ( + ui "github.com/gizak/termui" +) + +type LogLines struct { + ts []string + data []string +} + +func NewLogLines(max int) *LogLines { + ll := &LogLines{ + ts: make([]string, max), + data: make([]string, max), + } + return ll +} + +func (ll *LogLines) tail(n int) []string { + lines := make([]string, n) + for i := 0; i < n; i++ { + lines = append(lines, ll.data[len(ll.data)-i]) + } + return lines +} +func (ll *LogLines) getLines(start, end int) []string { + if end < 0 { + return ll.data[start:] + } + return ll.data[start:end] +} + +func (ll *LogLines) add(s string) { + if len(ll.data) == cap(ll.data) { + ll.data = append(ll.data[:0], ll.data[1:]...) + ll.ts = append(ll.ts[:0], ll.ts[1:]...) + } + ll.ts = append(ll.ts, "timestamp") + ll.data = append(ll.data, s) + log.Debugf("recorded log line: %v", s) +} + +type Logs struct { + *ui.List + lines *LogLines +} + +func NewLogs(stream chan string) *Logs { + p := ui.NewList() + p.Y = ui.TermHeight() / 2 + p.X = 0 + p.Height = ui.TermHeight() - p.Y + p.Width = ui.TermWidth() + //p.Overflow = "wrap" + p.ItemFgColor = ui.ThemeAttr("par.text.fg") + i := &Logs{p, NewLogLines(4098)} + go func() { + for line := range stream { + i.lines.add(line) + ui.Render(i) + } + }() + return i +} + +func (w *Logs) Align() { + w.X = colWidth[0] + w.List.Align() +} + +func (w *Logs) Buffer() ui.Buffer { + maxLines := w.Height - 2 + offset := len(w.lines.data) - maxLines + w.Items = w.lines.getLines(offset, -1) + return w.List.Buffer() +} + +// number of rows a line will occupy at current panel width +func (w *Logs) lineHeight(s string) int { return (len(s) / w.InnerWidth()) + 1 }