]>
Commit | Line | Data |
---|---|---|
15c0b25d AP |
1 | package cli |
2 | ||
3 | import ( | |
107c1cdb ND |
4 | "github.com/fatih/color" |
5 | ) | |
6 | ||
7 | const ( | |
8 | noColor = -1 | |
15c0b25d AP |
9 | ) |
10 | ||
11 | // UiColor is a posix shell color code to use. | |
12 | type UiColor struct { | |
13 | Code int | |
14 | Bold bool | |
15 | } | |
16 | ||
17 | // A list of colors that are useful. These are all non-bolded by default. | |
18 | var ( | |
107c1cdb ND |
19 | UiColorNone UiColor = UiColor{noColor, false} |
20 | UiColorRed = UiColor{int(color.FgHiRed), false} | |
21 | UiColorGreen = UiColor{int(color.FgHiGreen), false} | |
22 | UiColorYellow = UiColor{int(color.FgHiYellow), false} | |
23 | UiColorBlue = UiColor{int(color.FgHiBlue), false} | |
24 | UiColorMagenta = UiColor{int(color.FgHiMagenta), false} | |
25 | UiColorCyan = UiColor{int(color.FgHiCyan), false} | |
15c0b25d AP |
26 | ) |
27 | ||
28 | // ColoredUi is a Ui implementation that colors its output according | |
29 | // to the given color schemes for the given type of output. | |
30 | type ColoredUi struct { | |
31 | OutputColor UiColor | |
32 | InfoColor UiColor | |
33 | ErrorColor UiColor | |
34 | WarnColor UiColor | |
35 | Ui Ui | |
36 | } | |
37 | ||
38 | func (u *ColoredUi) Ask(query string) (string, error) { | |
39 | return u.Ui.Ask(u.colorize(query, u.OutputColor)) | |
40 | } | |
41 | ||
42 | func (u *ColoredUi) AskSecret(query string) (string, error) { | |
43 | return u.Ui.AskSecret(u.colorize(query, u.OutputColor)) | |
44 | } | |
45 | ||
46 | func (u *ColoredUi) Output(message string) { | |
47 | u.Ui.Output(u.colorize(message, u.OutputColor)) | |
48 | } | |
49 | ||
50 | func (u *ColoredUi) Info(message string) { | |
51 | u.Ui.Info(u.colorize(message, u.InfoColor)) | |
52 | } | |
53 | ||
54 | func (u *ColoredUi) Error(message string) { | |
55 | u.Ui.Error(u.colorize(message, u.ErrorColor)) | |
56 | } | |
57 | ||
58 | func (u *ColoredUi) Warn(message string) { | |
59 | u.Ui.Warn(u.colorize(message, u.WarnColor)) | |
60 | } | |
61 | ||
107c1cdb ND |
62 | func (u *ColoredUi) colorize(message string, uc UiColor) string { |
63 | if uc.Code == noColor { | |
15c0b25d AP |
64 | return message |
65 | } | |
66 | ||
107c1cdb ND |
67 | attr := []color.Attribute{color.Attribute(uc.Code)} |
68 | if uc.Bold { | |
69 | attr = append(attr, color.Bold) | |
15c0b25d AP |
70 | } |
71 | ||
107c1cdb | 72 | return color.New(attr...).SprintFunc()(message) |
15c0b25d | 73 | } |