2016-12-22 16:15:22 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-02-07 03:33:09 +00:00
|
|
|
"github.com/bcicen/ctop/config"
|
2017-08-05 11:28:20 +00:00
|
|
|
"github.com/bcicen/ctop/cwidgets/single"
|
2016-12-22 16:15:22 +00:00
|
|
|
ui "github.com/gizak/termui"
|
|
|
|
)
|
|
|
|
|
2019-05-22 16:58:55 +00:00
|
|
|
func ShowConnError(err error) (exit bool) {
|
|
|
|
ui.Clear()
|
|
|
|
ui.DefaultEvtStream.ResetHandlers()
|
|
|
|
defer ui.DefaultEvtStream.ResetHandlers()
|
|
|
|
|
|
|
|
setErr := func(err error) {
|
2019-05-22 17:38:01 +00:00
|
|
|
errView.Append(err.Error())
|
|
|
|
errView.Append("attempting to reconnect...")
|
2019-05-22 16:58:55 +00:00
|
|
|
ui.Render(errView)
|
|
|
|
}
|
|
|
|
|
|
|
|
HandleKeys("exit", func() {
|
|
|
|
exit = true
|
|
|
|
ui.StopLoop()
|
|
|
|
})
|
|
|
|
|
2019-05-22 17:39:37 +00:00
|
|
|
ui.Handle("/timer/1s", func(ui.Event) {
|
2019-05-22 16:58:55 +00:00
|
|
|
_, err := cursor.RefreshContainers()
|
|
|
|
if err == nil {
|
|
|
|
ui.StopLoop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
setErr(err)
|
|
|
|
})
|
|
|
|
|
|
|
|
ui.Handle("/sys/wnd/resize", func(e ui.Event) {
|
|
|
|
errView.Resize()
|
|
|
|
ui.Clear()
|
|
|
|
ui.Render(errView)
|
|
|
|
log.Infof("RESIZE")
|
|
|
|
})
|
|
|
|
|
|
|
|
errView.Resize()
|
|
|
|
setErr(err)
|
|
|
|
ui.Loop()
|
|
|
|
return exit
|
|
|
|
}
|
|
|
|
|
2017-03-08 00:10:38 +00:00
|
|
|
func RedrawRows(clr bool) {
|
2016-12-30 22:10:49 +00:00
|
|
|
// reinit body rows
|
2017-02-26 22:04:24 +00:00
|
|
|
cGrid.Clear()
|
2017-01-02 20:14:35 +00:00
|
|
|
|
2016-12-30 22:10:49 +00:00
|
|
|
// build layout
|
2017-02-26 06:22:50 +00:00
|
|
|
y := 1
|
2017-02-16 04:06:05 +00:00
|
|
|
if config.GetSwitchVal("enableHeader") {
|
2017-03-06 03:03:05 +00:00
|
|
|
header.SetCount(cursor.Len())
|
2017-03-06 02:00:30 +00:00
|
|
|
header.SetFilter(config.GetVal("filterStr"))
|
|
|
|
y += header.Height()
|
2017-01-06 13:49:22 +00:00
|
|
|
}
|
2018-01-11 18:19:01 +00:00
|
|
|
|
2017-02-26 07:31:23 +00:00
|
|
|
cGrid.SetY(y)
|
2017-02-24 09:10:14 +00:00
|
|
|
|
2017-03-08 07:45:31 +00:00
|
|
|
for _, c := range cursor.filtered {
|
2017-03-03 07:57:26 +00:00
|
|
|
cGrid.AddRows(c.Widgets)
|
2017-02-24 09:10:14 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 00:10:38 +00:00
|
|
|
if clr {
|
|
|
|
ui.Clear()
|
|
|
|
log.Debugf("screen cleared")
|
|
|
|
}
|
2017-02-26 07:31:23 +00:00
|
|
|
if config.GetSwitchVal("enableHeader") {
|
2017-03-09 08:33:25 +00:00
|
|
|
ui.Render(header)
|
2017-02-26 07:31:23 +00:00
|
|
|
}
|
|
|
|
cGrid.Align()
|
2017-02-26 06:22:50 +00:00
|
|
|
ui.Render(cGrid)
|
2016-12-22 16:15:22 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 14:23:28 +00:00
|
|
|
func SingleView() MenuFn {
|
|
|
|
c := cursor.Selected()
|
|
|
|
if c == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-06 08:51:50 +00:00
|
|
|
ui.Clear()
|
|
|
|
ui.DefaultEvtStream.ResetHandlers()
|
|
|
|
defer ui.DefaultEvtStream.ResetHandlers()
|
|
|
|
|
2020-11-03 12:11:58 +00:00
|
|
|
ex := single.NewSingle()
|
2017-03-06 22:05:04 +00:00
|
|
|
c.SetUpdater(ex)
|
2017-03-06 08:51:50 +00:00
|
|
|
|
2017-03-06 22:05:04 +00:00
|
|
|
ex.Align()
|
|
|
|
ui.Render(ex)
|
2017-03-14 22:34:09 +00:00
|
|
|
|
|
|
|
HandleKeys("up", ex.Up)
|
|
|
|
HandleKeys("down", ex.Down)
|
|
|
|
ui.Handle("/sys/kbd/", func(ui.Event) { ui.StopLoop() })
|
|
|
|
|
|
|
|
ui.Handle("/timer/1s", func(ui.Event) { ui.Render(ex) })
|
2017-03-06 22:05:04 +00:00
|
|
|
ui.Handle("/sys/wnd/resize", func(e ui.Event) {
|
|
|
|
ex.SetWidth(ui.TermWidth())
|
|
|
|
ex.Align()
|
2017-03-08 00:52:31 +00:00
|
|
|
log.Infof("resize: width=%v max-rows=%v", ex.Width, cGrid.MaxRows())
|
2017-03-06 08:51:50 +00:00
|
|
|
})
|
|
|
|
|
2017-03-14 22:34:09 +00:00
|
|
|
ui.Loop()
|
2017-03-06 08:51:50 +00:00
|
|
|
c.SetUpdater(c.Widgets)
|
2018-01-11 14:23:28 +00:00
|
|
|
return nil
|
2017-03-06 08:51:50 +00:00
|
|
|
}
|
2017-01-06 19:46:30 +00:00
|
|
|
|
2019-05-22 16:58:55 +00:00
|
|
|
func RefreshDisplay() error {
|
2017-06-12 13:52:45 +00:00
|
|
|
// skip display refresh during scroll
|
|
|
|
if !cursor.isScrolling {
|
2019-05-22 16:58:55 +00:00
|
|
|
needsClear, err := cursor.RefreshContainers()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-12 13:52:45 +00:00
|
|
|
RedrawRows(needsClear)
|
|
|
|
}
|
2019-05-22 16:58:55 +00:00
|
|
|
return nil
|
2017-03-08 00:10:38 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 03:03:05 +00:00
|
|
|
func Display() bool {
|
2018-01-11 14:23:28 +00:00
|
|
|
var menu MenuFn
|
2019-05-22 16:58:55 +00:00
|
|
|
var connErr error
|
2017-01-01 22:42:13 +00:00
|
|
|
|
2017-02-26 06:22:50 +00:00
|
|
|
cGrid.SetWidth(ui.TermWidth())
|
2017-02-05 00:56:45 +00:00
|
|
|
ui.DefaultEvtStream.Hook(logEvent)
|
|
|
|
|
2017-02-18 03:31:50 +00:00
|
|
|
// initial draw
|
2017-03-06 02:00:30 +00:00
|
|
|
header.Align()
|
2018-01-11 18:19:01 +00:00
|
|
|
status.Align()
|
2017-03-06 03:03:05 +00:00
|
|
|
cursor.RefreshContainers()
|
2017-03-08 00:10:38 +00:00
|
|
|
RedrawRows(true)
|
2016-12-22 16:15:22 +00:00
|
|
|
|
2017-03-14 22:34:09 +00:00
|
|
|
HandleKeys("up", cursor.Up)
|
|
|
|
HandleKeys("down", cursor.Down)
|
2017-03-24 02:31:08 +00:00
|
|
|
|
|
|
|
HandleKeys("pgup", cursor.PgUp)
|
|
|
|
HandleKeys("pgdown", cursor.PgDown)
|
|
|
|
|
2017-03-14 22:34:09 +00:00
|
|
|
HandleKeys("exit", ui.StopLoop)
|
|
|
|
HandleKeys("help", func() {
|
|
|
|
menu = HelpMenu
|
|
|
|
ui.StopLoop()
|
|
|
|
})
|
|
|
|
|
2018-01-10 18:45:14 +00:00
|
|
|
ui.Handle("/sys/kbd/<enter>", func(ui.Event) {
|
2017-11-20 11:09:36 +00:00
|
|
|
menu = ContainerMenu
|
|
|
|
ui.StopLoop()
|
|
|
|
})
|
2018-06-28 08:40:26 +00:00
|
|
|
ui.Handle("/sys/kbd/<left>", func(ui.Event) {
|
|
|
|
menu = LogMenu
|
|
|
|
ui.StopLoop()
|
|
|
|
})
|
|
|
|
ui.Handle("/sys/kbd/<right>", func(ui.Event) {
|
|
|
|
menu = SingleView
|
|
|
|
ui.StopLoop()
|
|
|
|
})
|
2017-11-25 18:30:50 +00:00
|
|
|
ui.Handle("/sys/kbd/l", func(ui.Event) {
|
|
|
|
menu = LogMenu
|
|
|
|
ui.StopLoop()
|
|
|
|
})
|
2018-10-12 07:03:27 +00:00
|
|
|
ui.Handle("/sys/kbd/e", func(ui.Event) {
|
2018-10-25 18:52:59 +00:00
|
|
|
menu = ExecShell
|
2018-10-12 07:03:27 +00:00
|
|
|
ui.StopLoop()
|
|
|
|
})
|
2020-12-12 19:21:53 +00:00
|
|
|
ui.Handle("/sys/kbd/w", func(ui.Event) {
|
|
|
|
menu = OpenInBrowser()
|
|
|
|
})
|
2018-01-10 18:45:14 +00:00
|
|
|
ui.Handle("/sys/kbd/o", func(ui.Event) {
|
2018-01-11 14:23:28 +00:00
|
|
|
menu = SingleView
|
2017-03-06 08:51:50 +00:00
|
|
|
ui.StopLoop()
|
2017-01-06 19:46:30 +00:00
|
|
|
})
|
2017-02-16 00:00:31 +00:00
|
|
|
ui.Handle("/sys/kbd/a", func(ui.Event) {
|
|
|
|
config.Toggle("allContainers")
|
2019-05-22 16:58:55 +00:00
|
|
|
connErr = RefreshDisplay()
|
|
|
|
if connErr != nil {
|
|
|
|
ui.StopLoop()
|
|
|
|
}
|
2017-02-16 00:00:31 +00:00
|
|
|
})
|
2017-03-01 01:10:33 +00:00
|
|
|
ui.Handle("/sys/kbd/D", func(ui.Event) {
|
2017-03-06 03:03:05 +00:00
|
|
|
dumpContainer(cursor.Selected())
|
2017-03-01 01:10:33 +00:00
|
|
|
})
|
2017-01-21 18:15:29 +00:00
|
|
|
ui.Handle("/sys/kbd/f", func(ui.Event) {
|
|
|
|
menu = FilterMenu
|
|
|
|
ui.StopLoop()
|
|
|
|
})
|
2017-02-13 03:37:17 +00:00
|
|
|
ui.Handle("/sys/kbd/H", func(ui.Event) {
|
|
|
|
config.Toggle("enableHeader")
|
2017-03-08 00:10:38 +00:00
|
|
|
RedrawRows(true)
|
2017-02-13 03:37:17 +00:00
|
|
|
})
|
2017-01-21 18:15:29 +00:00
|
|
|
ui.Handle("/sys/kbd/r", func(e ui.Event) {
|
2017-02-07 03:33:09 +00:00
|
|
|
config.Toggle("sortReversed")
|
2017-01-12 19:48:29 +00:00
|
|
|
})
|
|
|
|
ui.Handle("/sys/kbd/s", func(ui.Event) {
|
|
|
|
menu = SortMenu
|
2016-12-22 16:15:22 +00:00
|
|
|
ui.StopLoop()
|
|
|
|
})
|
2020-01-02 23:02:53 +00:00
|
|
|
ui.Handle("/sys/kbd/c", func(ui.Event) {
|
|
|
|
menu = ColumnsMenu
|
|
|
|
ui.StopLoop()
|
|
|
|
})
|
2018-01-11 16:15:18 +00:00
|
|
|
ui.Handle("/sys/kbd/S", func(ui.Event) {
|
2018-01-11 18:19:01 +00:00
|
|
|
path, err := config.Write()
|
|
|
|
if err == nil {
|
|
|
|
log.Statusf("wrote config to %s", path)
|
|
|
|
} else {
|
|
|
|
log.StatusErr(err)
|
|
|
|
}
|
|
|
|
ui.StopLoop()
|
2018-01-11 16:15:18 +00:00
|
|
|
})
|
2017-02-23 22:00:05 +00:00
|
|
|
|
2016-12-22 16:15:22 +00:00
|
|
|
ui.Handle("/timer/1s", func(e ui.Event) {
|
2018-01-11 18:27:30 +00:00
|
|
|
if log.StatusQueued() {
|
|
|
|
ui.StopLoop()
|
|
|
|
}
|
2019-05-22 16:58:55 +00:00
|
|
|
connErr = RefreshDisplay()
|
|
|
|
if connErr != nil {
|
|
|
|
ui.StopLoop()
|
|
|
|
}
|
2016-12-22 16:15:22 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
ui.Handle("/sys/wnd/resize", func(e ui.Event) {
|
2017-03-06 02:00:30 +00:00
|
|
|
header.Align()
|
2018-01-11 18:19:01 +00:00
|
|
|
status.Align()
|
2017-03-08 07:45:31 +00:00
|
|
|
cursor.ScrollPage()
|
2017-02-26 06:22:50 +00:00
|
|
|
cGrid.SetWidth(ui.TermWidth())
|
2017-03-08 00:52:31 +00:00
|
|
|
log.Infof("resize: width=%v max-rows=%v", cGrid.Width, cGrid.MaxRows())
|
2017-03-08 00:10:38 +00:00
|
|
|
RedrawRows(true)
|
2016-12-22 16:15:22 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
ui.Loop()
|
2018-01-11 14:23:28 +00:00
|
|
|
|
2019-05-22 16:58:55 +00:00
|
|
|
if connErr != nil {
|
|
|
|
return ShowConnError(connErr)
|
|
|
|
}
|
|
|
|
|
2018-01-11 18:19:01 +00:00
|
|
|
if log.StatusQueued() {
|
|
|
|
for sm := range log.FlushStatus() {
|
|
|
|
if sm.IsError {
|
|
|
|
status.ShowErr(sm.Text)
|
|
|
|
} else {
|
|
|
|
status.Show(sm.Text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-01-08 23:07:58 +00:00
|
|
|
if menu != nil {
|
2018-01-11 14:23:28 +00:00
|
|
|
for menu != nil {
|
|
|
|
menu = menu()
|
2017-03-09 23:09:58 +00:00
|
|
|
}
|
2017-03-06 08:51:50 +00:00
|
|
|
return false
|
|
|
|
}
|
2018-01-11 14:23:28 +00:00
|
|
|
|
2017-01-01 22:42:13 +00:00
|
|
|
return true
|
2016-12-22 16:15:22 +00:00
|
|
|
}
|