mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
81 lines
1.6 KiB
Go
81 lines
1.6 KiB
Go
|
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 }
|