/
description.go
111 lines (93 loc) · 2.46 KB
/
description.go
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* Copyright 2022 The Furiko Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package printer
import (
"fmt"
"io"
"strings"
"github.com/fatih/color"
"github.com/jedib0t/go-pretty/v6/text"
)
// Section contains a section name and a list of description key-value pairs.
type Section struct {
Name string
Descs [][]string
}
type SectionGenerator func() (Section, error)
// ToSectionGenerator converts a Section into a SectionGenerator.
func ToSectionGenerator(section Section) SectionGenerator {
return func() (Section, error) {
return section, nil
}
}
// DescriptionPrinter prints key-value pairs, separated by sections.
type DescriptionPrinter struct {
out io.Writer
minSpacing int
}
func NewDescriptionPrinter(out io.Writer) *DescriptionPrinter {
return &DescriptionPrinter{
out: out,
minSpacing: 2,
}
}
func (p *DescriptionPrinter) Header(header string) {
p.Print(p.getHeader(header))
}
func (p *DescriptionPrinter) Descriptions(kvs [][]string) {
p.Print(kvs)
}
func (p *DescriptionPrinter) NewLine() {
p.Print([][]string{{}})
}
func (p *DescriptionPrinter) Divider() {
p.Print([][]string{{"---"}})
}
func (p *DescriptionPrinter) getHeader(header string) [][]string {
return [][]string{
{color.CyanString(strings.ToUpper(header))},
}
}
// Print prints each of the given key-value pairs.
func (p *DescriptionPrinter) Print(kvs [][]string) {
var maxLen int
for _, kv := range kvs {
if len(kv) == 0 {
continue
}
if len(kv[0]) > maxLen {
maxLen = len(kv[0])
}
}
// Add a minimum space between keys and values, plus one for the colon.
maxLen += p.minSpacing + 1
for _, kv := range kvs {
// Add empty line.
if len(kv) == 0 {
_, _ = fmt.Fprintln(p.out, "")
continue
}
// Add single key without colon.
key := kv[0]
if len(kv) == 1 {
_, _ = fmt.Fprintln(p.out, key)
continue
}
// Print key-value pair.
value := kv[1]
_, _ = fmt.Fprintln(p.out, text.Pad(key+":", maxLen, ' ')+value)
}
}