diff options
Diffstat (limited to 'vendor/github.com/mitchellh/cli/ui_colored.go')
-rw-r--r-- | vendor/github.com/mitchellh/cli/ui_colored.go | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/vendor/github.com/mitchellh/cli/ui_colored.go b/vendor/github.com/mitchellh/cli/ui_colored.go new file mode 100644 index 0000000..e3d5131 --- /dev/null +++ b/vendor/github.com/mitchellh/cli/ui_colored.go | |||
@@ -0,0 +1,69 @@ | |||
1 | package cli | ||
2 | |||
3 | import ( | ||
4 | "fmt" | ||
5 | ) | ||
6 | |||
7 | // UiColor is a posix shell color code to use. | ||
8 | type UiColor struct { | ||
9 | Code int | ||
10 | Bold bool | ||
11 | } | ||
12 | |||
13 | // A list of colors that are useful. These are all non-bolded by default. | ||
14 | var ( | ||
15 | UiColorNone UiColor = UiColor{-1, false} | ||
16 | UiColorRed = UiColor{31, false} | ||
17 | UiColorGreen = UiColor{32, false} | ||
18 | UiColorYellow = UiColor{33, false} | ||
19 | UiColorBlue = UiColor{34, false} | ||
20 | UiColorMagenta = UiColor{35, false} | ||
21 | UiColorCyan = UiColor{36, false} | ||
22 | ) | ||
23 | |||
24 | // ColoredUi is a Ui implementation that colors its output according | ||
25 | // to the given color schemes for the given type of output. | ||
26 | type ColoredUi struct { | ||
27 | OutputColor UiColor | ||
28 | InfoColor UiColor | ||
29 | ErrorColor UiColor | ||
30 | WarnColor UiColor | ||
31 | Ui Ui | ||
32 | } | ||
33 | |||
34 | func (u *ColoredUi) Ask(query string) (string, error) { | ||
35 | return u.Ui.Ask(u.colorize(query, u.OutputColor)) | ||
36 | } | ||
37 | |||
38 | func (u *ColoredUi) AskSecret(query string) (string, error) { | ||
39 | return u.Ui.AskSecret(u.colorize(query, u.OutputColor)) | ||
40 | } | ||
41 | |||
42 | func (u *ColoredUi) Output(message string) { | ||
43 | u.Ui.Output(u.colorize(message, u.OutputColor)) | ||
44 | } | ||
45 | |||
46 | func (u *ColoredUi) Info(message string) { | ||
47 | u.Ui.Info(u.colorize(message, u.InfoColor)) | ||
48 | } | ||
49 | |||
50 | func (u *ColoredUi) Error(message string) { | ||
51 | u.Ui.Error(u.colorize(message, u.ErrorColor)) | ||
52 | } | ||
53 | |||
54 | func (u *ColoredUi) Warn(message string) { | ||
55 | u.Ui.Warn(u.colorize(message, u.WarnColor)) | ||
56 | } | ||
57 | |||
58 | func (u *ColoredUi) colorize(message string, color UiColor) string { | ||
59 | if color.Code == -1 { | ||
60 | return message | ||
61 | } | ||
62 | |||
63 | attr := 0 | ||
64 | if color.Bold { | ||
65 | attr = 1 | ||
66 | } | ||
67 | |||
68 | return fmt.Sprintf("\033[%d;%dm%s\033[0m", attr, color.Code, message) | ||
69 | } | ||