Files
ctop/cwidgets/expanded/info.go
T

59 lines
1.0 KiB
Go
Raw Normal View History

2017-03-06 08:51:50 +00:00
package expanded
import (
2017-05-15 10:37:27 +00:00
"strings"
2017-03-06 08:51:50 +00:00
ui "github.com/gizak/termui"
)
2017-06-10 13:46:54 +00:00
var displayInfo = []string{"id", "name", "image", "ports", "state", "created"}
2017-03-06 08:51:50 +00:00
type Info struct {
*ui.Table
data map[string]string
}
func NewInfo(id string) *Info {
p := ui.NewTable()
p.Height = 4
p.Width = colWidth[0]
2017-03-08 10:40:03 +11:00
p.FgColor = ui.ThemeAttr("par.text.fg")
2017-03-09 16:16:40 -08:00
p.Separator = false
2017-03-06 08:51:50 +00:00
i := &Info{p, make(map[string]string)}
i.Set("id", id)
2017-03-06 08:51:50 +00:00
return i
}
func (w *Info) Set(k, v string) {
w.data[k] = v
2017-05-15 10:37:27 +00:00
2017-03-06 08:51:50 +00:00
// rebuild rows
w.Rows = [][]string{}
for _, k := range displayInfo {
if v, ok := w.data[k]; ok {
2017-05-15 10:37:27 +00:00
w.Rows = append(w.Rows, mkInfoRows(k, v)...)
}
2017-03-06 08:51:50 +00:00
}
2017-05-15 10:37:27 +00:00
w.Height = len(w.Rows) + 2
2017-03-06 08:51:50 +00:00
}
2017-05-15 10:37:27 +00:00
// Build row(s) from a key and value string
func mkInfoRows(k, v string) (rows [][]string) {
lines := strings.Split(v, "\n")
// initial row with field name
rows = append(rows, []string{k, lines[0]})
// append any additional lines in seperate row
if len(lines) > 1 {
for _, line := range lines[1:] {
if line != "" {
rows = append(rows, []string{"", line})
}
}
}
return rows
}