4 // Please see the LICENSE file is this directory. |
4 // Please see the LICENSE file is this directory. |
5 |
5 |
6 package printer |
6 package printer |
7 |
7 |
8 import ( |
8 import ( |
9 "bytes" |
|
10 "encoding/json" |
9 "encoding/json" |
11 "fmt" |
10 "fmt" |
12 "go/doc" |
|
13 "io" |
11 "io" |
14 "os" |
12 "os" |
15 "reflect" |
13 "reflect" |
16 "strings" |
14 "strings" |
17 "text/template" |
15 "text/template" |
18 "time" |
16 "time" |
19 |
17 |
|
18 "github.com/kr/text" |
20 "github.com/m0t0k1ch1/gomif" |
19 "github.com/m0t0k1ch1/gomif" |
21 "github.com/mattn/go-isatty" |
20 "github.com/mattn/go-isatty" |
22 |
21 |
23 "github.com/McKael/madon" |
22 "github.com/McKael/madon" |
24 "github.com/McKael/madonctl/printer/colors" |
23 "github.com/McKael/madonctl/printer/colors" |
156 return "", nil |
155 return "", nil |
157 } |
156 } |
158 return colors.ANSICodeString(desc) |
157 return colors.ANSICodeString(desc) |
159 } |
158 } |
160 |
159 |
161 // Wrap text with indent prefix |
|
162 // Currently paragraph-based (cf. doc.ToText), which is not very good for |
|
163 // our use case since CRs are ignored. |
|
164 func wrap(indent string, lineLength int, text string) string { |
|
165 var buf bytes.Buffer |
|
166 |
|
167 width := lineLength - len(indent) |
|
168 if width < 10 { |
|
169 width = 10 |
|
170 } |
|
171 doc.ToText(&buf, text, indent, indent+" ", width) |
|
172 return buf.String() |
|
173 } |
|
174 |
|
175 // Parse datetime string from RFC3339 (default format in templates because |
160 // Parse datetime string from RFC3339 (default format in templates because |
176 // of implicit conversion to string) and return a local time. |
161 // of implicit conversion to string) and return a local time. |
177 func dateToLocal(s string) (time.Time, error) { |
162 func dateToLocal(s string) (time.Time, error) { |
178 t, err := time.Parse(time.RFC3339, s) |
163 t, err := time.Parse(time.RFC3339, s) |
179 if err != nil { |
164 if err != nil { |
180 return t, err |
165 return t, err |
181 } |
166 } |
182 return t.Local(), err |
167 return t.Local(), err |
183 } |
168 } |
|
169 |
|
170 // Wrap text with indent prefix |
|
171 func wrap(indent string, lineLength int, txt string) string { |
|
172 width := lineLength - len(indent) |
|
173 if width < 10 { |
|
174 width = 10 |
|
175 } |
|
176 |
|
177 lines := strings.SplitAfter(txt, "\n") |
|
178 var out []string |
|
179 for _, l := range lines { |
|
180 l = strings.TrimLeft(l, " ") |
|
181 out = append(out, text.Indent(text.Wrap(l, width), indent)) |
|
182 } |
|
183 return strings.Join(out, "\n") |
|
184 } |