added option to toggle log timestamp in the log view, closes #107

This commit is contained in:
Peter Reisinger 2017-12-08 11:35:04 +00:00
parent a135f45844
commit 0a6e6f02a4
4 changed files with 51 additions and 21 deletions

View File

@ -77,7 +77,7 @@ h | Open help dialog
s | Select container sort field s | Select container sort field
r | Reverse container sort order r | Reverse container sort order
m | Manage container (start, stop and/or remove) m | Manage container (start, stop and/or remove)
l | View container logs l | View container logs (`t` to toggle timestamp when open)
q | Quit ctop q | Quit ctop
[build]: _docs/build.md [build]: _docs/build.md

View File

@ -91,9 +91,6 @@ func (e *Single) Align() {
log.Debugf("align: width=%v left-col=%v right-col=%v", e.Width, colWidth[0], colWidth[1]) log.Debugf("align: width=%v left-col=%v right-col=%v", e.Width, colWidth[0], colWidth[1])
} }
func calcWidth(w int) {
}
func (e *Single) Buffer() ui.Buffer { func (e *Single) Buffer() ui.Buffer {
buf := ui.NewBuffer() buf := ui.NewBuffer()
if e.Width < (colWidth[0] + colWidth[1]) { if e.Width < (colWidth[0] + colWidth[1]) {

View File

@ -6,16 +6,20 @@ import (
"github.com/bcicen/ctop/widgets" "github.com/bcicen/ctop/widgets"
"github.com/bcicen/ctop/widgets/menu" "github.com/bcicen/ctop/widgets/menu"
ui "github.com/gizak/termui" ui "github.com/gizak/termui"
"time"
"fmt"
) )
var helpDialog = []menu.Item{ var helpDialog = []menu.Item{
menu.Item{"[a] - toggle display of all containers", ""}, {"[a] - toggle display of all containers", ""},
menu.Item{"[f] - filter displayed containers", ""}, {"[f] - filter displayed containers", ""},
menu.Item{"[h] - open this help dialog", ""}, {"[h] - open this help dialog", ""},
menu.Item{"[H] - toggle ctop header", ""}, {"[H] - toggle ctop header", ""},
menu.Item{"[s] - select container sort field", ""}, {"[s] - select container sort field", ""},
menu.Item{"[r] - reverse container sort order", ""}, {"[r] - reverse container sort order", ""},
menu.Item{"[q] - exit ctop", ""}, {"[m] - Manage container (start, stop and/or remove)", ""},
{"[l] - View container logs ([t] to toggle timestamp when open)", ""},
{"[q] - exit ctop", ""},
} }
func HelpMenu() { func HelpMenu() {
@ -162,7 +166,9 @@ func LogMenu() {
ui.Handle("/sys/wnd/resize", func(e ui.Event) { ui.Handle("/sys/wnd/resize", func(e ui.Event) {
m.Resize() m.Resize()
ui.Render(m) })
ui.Handle("/sys/kbd/t", func(ui.Event) {
m.Toggle()
}) })
ui.Handle("/sys/kbd/", func(ui.Event) { ui.Handle("/sys/kbd/", func(ui.Event) {
quit <- true quit <- true
@ -171,18 +177,30 @@ func LogMenu() {
ui.Loop() ui.Loop()
} }
func logReader(container *container.Container) (logs chan string, quit chan bool) { type toggleLog struct {
timestamp time.Time
message string
}
func (t *toggleLog) Toggle(on bool) string {
if on {
return fmt.Sprintf("%s %s", t.timestamp.Format("2006-01-02T15:04:05.999Z07:00"), t.message)
}
return t.message
}
func logReader(container *container.Container) (logs chan widgets.ToggleText, quit chan bool) {
logCollector := container.Logs() logCollector := container.Logs()
stream := logCollector.Stream() stream := logCollector.Stream()
logs = make(chan string) logs = make(chan widgets.ToggleText)
quit = make(chan bool) quit = make(chan bool)
go func() { go func() {
for { for {
select { select {
case log := <-stream: case log := <-stream:
logs <- log.Message logs <- &toggleLog{timestamp: log.Timestamp, message: log.Message}
case <-quit: case <-quit:
logCollector.Stop() logCollector.Stop()
close(logs) close(logs)

View File

@ -4,23 +4,29 @@ import (
ui "github.com/gizak/termui" ui "github.com/gizak/termui"
) )
type ToggleText interface {
// returns text for toggle on/off
Toggle(on bool) string
}
type TextView struct { type TextView struct {
ui.Block ui.Block
inputStream <-chan string inputStream <-chan ToggleText
render chan bool render chan bool
Text []string // all the text toggleState bool
TextOut []string // text to be displayed Text []ToggleText // all the text
TextOut []string // text to be displayed
TextFgColor ui.Attribute TextFgColor ui.Attribute
TextBgColor ui.Attribute TextBgColor ui.Attribute
padding Padding padding Padding
} }
func NewTextView(lines <-chan string) *TextView { func NewTextView(lines <-chan ToggleText) *TextView {
t := &TextView{ t := &TextView{
Block: *ui.NewBlock(), Block: *ui.NewBlock(),
inputStream: lines, inputStream: lines,
render: make(chan bool), render: make(chan bool),
Text: []string{}, Text: []ToggleText{},
TextOut: []string{}, TextOut: []string{},
TextFgColor: ui.ThemeAttr("menu.text.fg"), TextFgColor: ui.ThemeAttr("menu.text.fg"),
TextBgColor: ui.ThemeAttr("menu.text.bg"), TextBgColor: ui.ThemeAttr("menu.text.bg"),
@ -37,6 +43,8 @@ func NewTextView(lines <-chan string) *TextView {
return t return t
} }
// Adjusts text inside this view according to the window size. No need to call ui.Render(...)
// after calling this method, it is called automatically
func (t *TextView) Resize() { func (t *TextView) Resize() {
ui.Clear() ui.Clear()
t.Height = ui.TermHeight() t.Height = ui.TermHeight()
@ -44,6 +52,13 @@ func (t *TextView) Resize() {
t.render <- true t.render <- true
} }
// Toggles text inside this view. No need to call ui.Render(...) after calling this method,
// it is called automatically
func (t *TextView) Toggle() {
t.toggleState = !t.toggleState
t.render <- true
}
func (t *TextView) Buffer() ui.Buffer { func (t *TextView) Buffer() ui.Buffer {
var cell ui.Cell var cell ui.Cell
buf := t.Block.Buffer() buf := t.Block.Buffer()
@ -70,7 +85,7 @@ func (t *TextView) renderLoop() {
height := t.Height - (t.padding[1] * 2) height := t.Height - (t.padding[1] * 2)
t.TextOut = []string{} t.TextOut = []string{}
for i := len(t.Text) - 1; i >= 0; i-- { for i := len(t.Text) - 1; i >= 0; i-- {
lines := splitLine(t.Text[i], maxWidth) lines := splitLine(t.Text[i].Toggle(t.toggleState), maxWidth)
t.TextOut = append(lines, t.TextOut...) t.TextOut = append(lines, t.TextOut...)
if len(t.TextOut) > height { if len(t.TextOut) > height {
t.TextOut = t.TextOut[:height] t.TextOut = t.TextOut[:height]