Skip to content

Commit

Permalink
Merge pull request #14 from theckman/suffix_auto_colon
Browse files Browse the repository at this point in the history
Add support for automatic suffix delimiter using colon
  • Loading branch information
theckman committed Mar 18, 2020
2 parents dd5318c + 56cdd70 commit f82a078
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 9 deletions.
32 changes: 23 additions & 9 deletions spinner.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ type Config struct {
// recommended that this string starts with an space ` ` character.
Suffix string

// SuffixAutoColon configures whether the spinner adds a colon after the
// suffix automatically. If there is a message, a colon followed by a space
// is added to the suffix. Otherwise, if there is no message the colon is
// omitted.
SuffixAutoColon bool

// Message is the string printed after the suffix. If a suffix is present,
// `: ` is appended to the suffix before printing the message. It results in
// a message like:
Expand Down Expand Up @@ -151,10 +157,11 @@ type Config struct {
//
// Note: You need to use New() to construct a *Spinner.
type Spinner struct {
writer io.Writer
colorAll bool
cursorHidden bool
isWindows bool
writer io.Writer
colorAll bool
cursorHidden bool
suffixAutoColon bool
isWindows bool

active *uint32
delayDuration *int64 // to allow atomic updates
Expand Down Expand Up @@ -193,6 +200,7 @@ func New(cfg Config) (*Spinner, error) {

colorAll: cfg.ColorAll,
cursorHidden: cfg.HideCursor,
suffixAutoColon: cfg.SuffixAutoColon,
isWindows: runtime.GOOS == "windows",
colorFn: fmt.Sprintf,
stopColorFn: fmt.Sprintf,
Expand Down Expand Up @@ -413,15 +421,15 @@ func (s *Spinner) paintUpdate(timer *time.Timer) {
}
}

if _, err := paint(s.writer, mw, c, p, m, suf, s.colorAll, cFn); err != nil {
if _, err := paint(s.writer, mw, c, p, m, suf, s.suffixAutoColon, s.colorAll, cFn); err != nil {
panic(fmt.Sprintf("failed to paint line: %v", err))
}
} else {
if err := s.eraseWindows(); err != nil {
panic(fmt.Sprintf("failed to erase line: %v", err))
}

n, err := paint(s.writer, mw, c, p, m, suf, false, fmt.Sprintf)
n, err := paint(s.writer, mw, c, p, m, suf, s.suffixAutoColon, false, fmt.Sprintf)

if err != nil {
panic(fmt.Sprintf("failed to paint line: %v", err))
Expand Down Expand Up @@ -472,7 +480,7 @@ func (s *Spinner) paintStop(chanOk bool) {
}

// paint the line with a newline as it's the final line
if _, err := paint(s.writer, mw, c, p, m+"\n", suf, s.colorAll, cFn); err != nil {
if _, err := paint(s.writer, mw, c, p, m+"\n", suf, s.suffixAutoColon, s.colorAll, cFn); err != nil {
panic(fmt.Sprintf("failed to paint line: %v", err))
}

Expand All @@ -485,7 +493,7 @@ func (s *Spinner) paintStop(chanOk bool) {
return
}

if _, err := paint(s.writer, mw, c, p, m+"\n", suf, false, fmt.Sprintf); err != nil {
if _, err := paint(s.writer, mw, c, p, m+"\n", suf, s.suffixAutoColon, false, fmt.Sprintf); err != nil {
panic(fmt.Sprintf("failed to paint line: %v", err))
}

Expand Down Expand Up @@ -526,7 +534,7 @@ func padChar(char character, maxWidth int) string {

// paint writes a single line to the s.writer, using the provided character,
// message, and color function
func paint(w io.Writer, maxWidth int, char character, prefix, message, suffix string, colorAll bool, colorFn func(format string, a ...interface{}) string) (int, error) {
func paint(w io.Writer, maxWidth int, char character, prefix, message, suffix string, suffixAutoColon, colorAll bool, colorFn func(format string, a ...interface{}) string) (int, error) {
if char.Size == 0 {
if colorAll {
return fmt.Fprint(w, colorFn(message))
Expand All @@ -537,6 +545,12 @@ func paint(w io.Writer, maxWidth int, char character, prefix, message, suffix st

c := padChar(char, maxWidth)

if suffixAutoColon {
if len(suffix) > 0 && len(message) > 0 && message != "\n" {
suffix += ": "
}
}

if colorAll {
return fmt.Fprint(w, colorFn("%s%s%s%s", prefix, c, suffix, message))
}
Expand Down
45 changes: 45 additions & 0 deletions spinner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,21 @@ func TestSpinner_paintUpdate(t *testing.T) {
},
want: "\r\033[K\ray msg\r\033[K\raz msg\r\033[K\ray msg",
},
{
name: "spinner_no_hide_cursor_auto_cursor",
spinner: &Spinner{
mu: &sync.Mutex{},
prefix: "a",
message: "msg",
suffix: " ",
maxWidth: 1,
colorFn: fmt.Sprintf,
chars: []character{{Value: "y", Size: 1}, {Value: "z", Size: 1}},
delayDuration: int64Ptr(10),
suffixAutoColon: true,
},
want: "\r\033[K\ray : msg\r\033[K\raz : msg\r\033[K\ray : msg",
},
{
name: "spinner_hide_cursor",
spinner: &Spinner{
Expand Down Expand Up @@ -975,6 +990,36 @@ func TestSpinner_paintStop(t *testing.T) {
},
want: "\r\033[K\rax stop\n",
},
{
name: "ok_auto_colon",
ok: true,
spinner: &Spinner{
mu: &sync.Mutex{},
prefix: "a",
suffix: " ",
maxWidth: 1,
stopColorFn: fmt.Sprintf,
stopChar: character{Value: "x", Size: 1},
stopMsg: "stop",
suffixAutoColon: true,
},
want: "\r\033[K\rax : stop\n",
},
{
name: "ok_auto_colon_no_msg",
ok: true,
spinner: &Spinner{
mu: &sync.Mutex{},
prefix: "a",
suffix: " ",
maxWidth: 1,
stopColorFn: fmt.Sprintf,
stopChar: character{Value: "x", Size: 1},
stopMsg: "",
suffixAutoColon: true,
},
want: "\r\033[K\rax \n",
},
{
name: "ok_unhide",
ok: true,
Expand Down

0 comments on commit f82a078

Please sign in to comment.