-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinfo.go
More file actions
47 lines (36 loc) · 775 Bytes
/
Copy pathinfo.go
File metadata and controls
47 lines (36 loc) · 775 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package stdcli
import (
"fmt"
"strings"
)
type Info struct {
Context *Context
Rows []InfoRow
}
type InfoRow struct {
Header string
Value string
}
func (i *Info) Add(header, value string) {
i.Rows = append(i.Rows, InfoRow{Header: header, Value: value})
}
func (i *Info) Print() error {
f := i.formatString()
for _, r := range i.Rows {
value := strings.Replace(r.Value, "\n", fmt.Sprintf(fmt.Sprintf("\n%%%ds ", i.headerWidth()), ""), -1)
i.Context.Writef(f, r.Header, value)
}
return nil
}
func (i *Info) formatString() string {
return fmt.Sprintf("<h1>%%-%ds</h1> <value>%%s</value>\n", i.headerWidth())
}
func (i *Info) headerWidth() int {
w := 0
for _, r := range i.Rows {
if len(r.Header) > w {
w = len(r.Header)
}
}
return w
}