2017-08-05 11:28:20 +00:00
|
|
|
package single
|
2017-03-06 08:51:50 +00:00
|
|
|
|
|
|
|
import (
|
2017-05-15 10:37:27 +00:00
|
|
|
"strings"
|
|
|
|
|
2017-03-06 08:51:50 +00:00
|
|
|
ui "github.com/gizak/termui"
|
|
|
|
)
|
|
|
|
|
2018-09-06 19:01:16 +00:00
|
|
|
var displayInfo = []string{"id", "name", "image", "ports", "IPs", "state", "created", "health"}
|
2017-03-06 22:05:04 +00:00
|
|
|
|
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
|
2017-03-06 22:05:04 +00:00
|
|
|
p.Width = colWidth[0]
|
2017-03-07 23:40:03 +00:00
|
|
|
p.FgColor = ui.ThemeAttr("par.text.fg")
|
2017-03-10 00:16:40 +00:00
|
|
|
p.Separator = false
|
2017-03-06 08:51:50 +00:00
|
|
|
i := &Info{p, make(map[string]string)}
|
2017-03-06 22:05:04 +00:00
|
|
|
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{}
|
2017-03-06 22:05:04 +00:00
|
|
|
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 22:05:04 +00:00
|
|
|
}
|
2017-03-06 08:51:50 +00:00
|
|
|
}
|
2017-05-15 10:37:27 +00:00
|
|
|
|
2017-03-06 22:05:04 +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]})
|
|
|
|
|
2018-09-06 19:01:16 +00:00
|
|
|
// append any additional lines in separate row
|
2017-05-15 10:37:27 +00:00
|
|
|
if len(lines) > 1 {
|
|
|
|
for _, line := range lines[1:] {
|
|
|
|
if line != "" {
|
|
|
|
rows = append(rows, []string{"", line})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rows
|
|
|
|
}
|