Skip to content

Commit

Permalink
Merge pull request mitchellh#75 from jefferai/fatih-color
Browse files Browse the repository at this point in the history
Switch to using fatih/color for ANSI color handling.
  • Loading branch information
mitchellh authored Apr 6, 2018
2 parents b068abc + 46455a8 commit c54c85e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 88 deletions.
32 changes: 18 additions & 14 deletions ui_colored.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package cli

import (
"fmt"
"github.com/fatih/color"
)

const (
noColor = -1
)

// UiColor is a posix shell color code to use.
Expand All @@ -12,13 +16,13 @@ type UiColor struct {

// A list of colors that are useful. These are all non-bolded by default.
var (
UiColorNone UiColor = UiColor{-1, false}
UiColorRed = UiColor{31, false}
UiColorGreen = UiColor{32, false}
UiColorYellow = UiColor{33, false}
UiColorBlue = UiColor{34, false}
UiColorMagenta = UiColor{35, false}
UiColorCyan = UiColor{36, false}
UiColorNone UiColor = UiColor{noColor, false}
UiColorRed = UiColor{int(color.FgHiRed), false}
UiColorGreen = UiColor{int(color.FgHiGreen), false}
UiColorYellow = UiColor{int(color.FgHiYellow), false}
UiColorBlue = UiColor{int(color.FgHiBlue), false}
UiColorMagenta = UiColor{int(color.FgHiMagenta), false}
UiColorCyan = UiColor{int(color.FgHiCyan), false}
)

// ColoredUi is a Ui implementation that colors its output according
Expand Down Expand Up @@ -55,15 +59,15 @@ func (u *ColoredUi) Warn(message string) {
u.Ui.Warn(u.colorize(message, u.WarnColor))
}

func (u *ColoredUi) colorize(message string, color UiColor) string {
if color.Code == -1 {
func (u *ColoredUi) colorize(message string, uc UiColor) string {
if uc.Code == noColor {
return message
}

attr := 0
if color.Bold {
attr = 1
attr := []color.Attribute{color.Attribute(uc.Code)}
if uc.Bold {
attr = append(attr, color.Bold)
}

return fmt.Sprintf("\033[%d;%dm%s\033[0m", attr, color.Code, message)
return color.New(attr...).SprintFunc()(message)
}
74 changes: 0 additions & 74 deletions ui_colored_test.go

This file was deleted.

0 comments on commit c54c85e

Please sign in to comment.