allow multi-line metadata in expanded view

This commit is contained in:
Bradley Cicenas
2017-05-15 10:37:27 +00:00
parent 6a8848d1e2
commit c76036a6f2

View File

@@ -1,6 +1,8 @@
package expanded package expanded
import ( import (
"strings"
ui "github.com/gizak/termui" ui "github.com/gizak/termui"
) )
@@ -24,12 +26,33 @@ func NewInfo(id string) *Info {
func (w *Info) Set(k, v string) { func (w *Info) Set(k, v string) {
w.data[k] = v w.data[k] = v
// rebuild rows // rebuild rows
w.Rows = [][]string{} w.Rows = [][]string{}
for _, k := range displayInfo { for _, k := range displayInfo {
if v, ok := w.data[k]; ok { if v, ok := w.data[k]; ok {
w.Rows = append(w.Rows, []string{k, v}) w.Rows = append(w.Rows, mkInfoRows(k, v)...)
} }
} }
w.Height = len(w.Rows) + 2 w.Height = len(w.Rows) + 2
} }
// 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
}