Commit 0f98852d authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer: Ui error output is red

parent 2217606e
...@@ -101,8 +101,8 @@ func (c Command) Run(env packer.Environment, args []string) int { ...@@ -101,8 +101,8 @@ func (c Command) Run(env packer.Environment, args []string) int {
var ui packer.Ui var ui packer.Ui
ui = &packer.ColoredUi{ ui = &packer.ColoredUi{
colors[i%len(colors)], Color: colors[i%len(colors)],
env.Ui(), Ui: env.Ui(),
} }
ui = &packer.PrefixedUi{ ui = &packer.PrefixedUi{
......
...@@ -28,8 +28,9 @@ type Ui interface { ...@@ -28,8 +28,9 @@ type Ui interface {
// ColoredUi is a UI that is colored using terminal colors. // ColoredUi is a UI that is colored using terminal colors.
type ColoredUi struct { type ColoredUi struct {
Color UiColor Color UiColor
Ui Ui ErrorColor UiColor
Ui Ui
} }
// PrefixedUi is a UI that wraps another UI implementation and adds a // PrefixedUi is a UI that wraps another UI implementation and adds a
...@@ -48,24 +49,29 @@ type ReaderWriterUi struct { ...@@ -48,24 +49,29 @@ type ReaderWriterUi struct {
} }
func (u *ColoredUi) Say(message string) { func (u *ColoredUi) Say(message string) {
u.Ui.Say(u.colorize(message, true)) u.Ui.Say(u.colorize(message, u.Color, true))
} }
func (u *ColoredUi) Message(message string) { func (u *ColoredUi) Message(message string) {
u.Ui.Message(u.colorize(message, false)) u.Ui.Message(u.colorize(message, u.Color, false))
} }
func (u *ColoredUi) Error(message string) { func (u *ColoredUi) Error(message string) {
u.Ui.Error(u.colorize(message, false)) color := u.ErrorColor
if color == 0 {
color = UiColorRed
}
u.Ui.Error(u.colorize(message, color, true))
} }
func (u *ColoredUi) colorize(message string, bold bool) string { func (u *ColoredUi) colorize(message string, color UiColor, bold bool) string {
attr := 0 attr := 0
if bold { if bold {
attr = 1 attr = 1
} }
return fmt.Sprintf("\033[%d;%d;40m%s\033[0m", attr, u.Color, message) return fmt.Sprintf("\033[%d;%d;40m%s\033[0m", attr, color, message)
} }
func (u *PrefixedUi) Say(message string) { func (u *PrefixedUi) Say(message string) {
......
...@@ -14,13 +14,26 @@ func testUi() *ReaderWriterUi { ...@@ -14,13 +14,26 @@ func testUi() *ReaderWriterUi {
} }
func TestColoredUi(t *testing.T) { func TestColoredUi(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
bufferUi := testUi() bufferUi := testUi()
ui := &ColoredUi{UiColorRed, bufferUi} ui := &ColoredUi{UiColorYellow, UiColorRed, bufferUi}
ui.Say("foo") ui.Say("foo")
assert.Equal(readWriter(bufferUi), "\033[1;31;40mfoo\033[0m\n", "should have color") result := readWriter(bufferUi)
if result != "\033[1;33;40mfoo\033[0m\n" {
t.Fatalf("invalid output: %s", result)
}
ui.Message("foo")
result = readWriter(bufferUi)
if result != "\033[0;33;40mfoo\033[0m\n" {
t.Fatalf("invalid output: %s", result)
}
ui.Error("foo")
result = readWriter(bufferUi)
if result != "\033[1;31;40mfoo\033[0m\n" {
t.Fatalf("invalid output: %s", result)
}
} }
func TestPrefixedUi(t *testing.T) { func TestPrefixedUi(t *testing.T) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment