Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better charmap selection #25

Merged
merged 1 commit into from
Jul 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/advanced/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func main() {
// Create a new spinner manager
sm := ysmrr.NewSpinnerManager(
ysmrr.WithCharMap(charmap.Arrows),
ysmrr.WithCharMap(charmap.Pipe),
ysmrr.WithSpinnerColor(colors.FgHiBlue),
)

Expand Down
2 changes: 1 addition & 1 deletion examples/basic_with_config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func main() {
// Create a new spinner manager
sm := ysmrr.NewSpinnerManager(
ysmrr.WithCharMap(charmap.Arrows),
ysmrr.WithCharMap(charmap.Arrow),
ysmrr.WithSpinnerColor(colors.FgHiBlue),
ysmrr.WithMessageColor(colors.FgHiYellow),
)
Expand Down
6 changes: 3 additions & 3 deletions manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (sm *spinnerManager) setNextFrame() {
// )
func NewSpinnerManager(options ...managerOption) SpinnerManager {
sm := &spinnerManager{
chars: charmap.Dots,
chars: charmap.GetCharMap(charmap.Dots),
frameDuration: 100 * time.Millisecond,
spinnerColor: colors.FgHiGreen,
errorColor: colors.FgHiRed,
Expand Down Expand Up @@ -231,9 +231,9 @@ type managerOption func(*spinnerManager)
// WithCharMap sets the characters used for the spinners.
// Available charmaps can be found in the package github.com/chelnak/ysmrr/pkg/charmap.
// The default charmap is the Dots.
func WithCharMap(chars []string) managerOption {
func WithCharMap(c charmap.CharMap) managerOption {
return func(sm *spinnerManager) {
sm.chars = chars
sm.chars = charmap.GetCharMap(c)
}
}

Expand Down
10 changes: 6 additions & 4 deletions manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/stretchr/testify/assert"
)

var arrows = charmap.GetCharMap(charmap.Arrow)

func TestNewSpinnerManager(t *testing.T) {
spinnerManager := ysmrr.NewSpinnerManager()
assert.NotNil(t, spinnerManager)
Expand All @@ -27,10 +29,10 @@ func TestNewSpinnerManager_WithWriter(t *testing.T) {

func TestNewSpinnerManager_WithCharMap(t *testing.T) {
spinnerManager := ysmrr.NewSpinnerManager(
ysmrr.WithCharMap(charmap.Arrows),
ysmrr.WithCharMap(charmap.Arrow),
)

assert.Equal(t, charmap.Arrows, spinnerManager.GetCharMap())
assert.Equal(t, arrows, spinnerManager.GetCharMap())
}

func TestNewSpinnerManager_WithFrameDuration(t *testing.T) {
Expand Down Expand Up @@ -58,10 +60,10 @@ func TestGetWriter(t *testing.T) {

func TestGetCharMap(t *testing.T) {
spinnerManager := ysmrr.NewSpinnerManager(
ysmrr.WithCharMap(charmap.Arrows),
ysmrr.WithCharMap(charmap.Arrow),
)

assert.Equal(t, charmap.Arrows, spinnerManager.GetCharMap())
assert.Equal(t, arrows, spinnerManager.GetCharMap())
}

func TestGetFrameDuration(t *testing.T) {
Expand Down
60 changes: 40 additions & 20 deletions pkg/charmap/charmap.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,47 @@
// Package charmap provides a collection of character maps to be used
// with a spinner.
// Spinners have been borrowed from the following sources:
// * https://wiki.tcl-lang.org/page/Text+Spinner
// * https://stackoverflow.com/questions/2685435/cooler-ascii-spinners
package charmap

var Dots = []string{
"⠋",
"⠙",
"⠹",
"⠸",
"⠼",
"⠴",
"⠦",
"⠧",
"⠇",
"⠏",
type CharMap int

const (
Arc CharMap = iota + 100
Arrow
Baloon
Baloon2
Circle
CircleHalves
CircleQuarters
Dots
Hamburger
Layer
Pipe
Point
Star
SquareCorners
)

var lookup = map[CharMap][]string{
Arc: {"◜", "◠", "◝", "◞", "◡", "◟"},
Arrow: {"←", "↖", "↑", "↗", "→", "↘", "↓", "↙"},
Baloon: {".", "o", "O", "@", "*"},
Baloon2: {".", "o", "O", "°", "O", "o", "."},
Circle: {"◡", "⊙", "◠"},
CircleHalves: {"◐", "◓", "◑", "◒"},
CircleQuarters: {"◴", "◷", "◶", "◵"},
Dots: {"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"},
Hamburger: {"☱", "☲", "☴"},
Layer: {"-", "=", "≡"},
Pipe: {"┤", "┘", "┴", "└", "├", "┌", "┬", "┐"},
Point: {"∙∙∙", "●∙∙", "∙●∙", "∙∙●", "∙∙∙"},
Star: {"✶", "✸", "✹", "✺", "✹", "✷"},
SquareCorners: {"◰", "◳", "◲", "◱"},
}

var Arrows = []string{
"←",
"↖",
"↑",
"↗",
"→",
"↘",
"↓",
"↙",
// GetCharMap retirms a slice of strings for the given CharMap.
func GetCharMap(c CharMap) []string {
return lookup[c]
}
55 changes: 55 additions & 0 deletions pkg/charmap/charmap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package charmap_test

import (
"testing"

"github.com/chelnak/ysmrr/pkg/charmap"
"github.com/stretchr/testify/assert"
)

var (
Arc = []string{"◜", "◠", "◝", "◞", "◡", "◟"}
Arrow = []string{"←", "↖", "↑", "↗", "→", "↘", "↓", "↙"}
Baloon = []string{".", "o", "O", "@", "*"}
Baloon2 = []string{".", "o", "O", "°", "O", "o", "."}
Circle = []string{"◡", "⊙", "◠"}
CircleHalves = []string{"◐", "◓", "◑", "◒"}
CircleQuarters = []string{"◴", "◷", "◶", "◵"}
Dots = []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"}
Hamburger = []string{"☱", "☲", "☴"}
Layer = []string{"-", "=", "≡"}
Pipe = []string{"┤", "┘", "┴", "└", "├", "┌", "┬", "┐"}
Point = []string{"∙∙∙", "●∙∙", "∙●∙", "∙∙●", "∙∙∙"}
Star = []string{"✶", "✸", "✹", "✺", "✹", "✷"}
SquareCorners = []string{"◰", "◳", "◲", "◱"}
)

func TestCharMaps(t *testing.T) {
tests := []struct {
name string
c charmap.CharMap
want []string
}{
{name: "Arc", c: charmap.Arc, want: Arc},
{name: "Arrow", c: charmap.Arrow, want: Arrow},
{name: "Baloon", c: charmap.Baloon, want: Baloon},
{name: "Baloon2", c: charmap.Baloon2, want: Baloon2},
{name: "Circle", c: charmap.Circle, want: Circle},
{name: "CircleHalves", c: charmap.CircleHalves, want: CircleHalves},
{name: "CircleQuarters", c: charmap.CircleQuarters, want: CircleQuarters},
{name: "Dots", c: charmap.Dots, want: Dots},
{name: "Hamburger", c: charmap.Hamburger, want: Hamburger},
{name: "Layer", c: charmap.Layer, want: Layer},
{name: "Pipe", c: charmap.Pipe, want: Pipe},
{name: "Point", c: charmap.Point, want: Point},
{name: "Star", c: charmap.Star, want: Star},
{name: "SquareCorners", c: charmap.SquareCorners, want: SquareCorners},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := charmap.GetCharMap(tt.c)
assert.Equal(t, tt.want, got)
})
}
}
5 changes: 3 additions & 2 deletions spinner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ func TestPrint(t *testing.T) {
spinner := ysmrr.NewSpinner(opts)

var buf bytes.Buffer
spinner.Print(&buf, charmap.Dots[0])
dots := charmap.GetCharMap(charmap.Dots)
spinner.Print(&buf, dots[0])

want := fmt.Sprintf("%s %s\r\n", charmap.Dots[0], initialMessage)
want := fmt.Sprintf("%s %s\r\n", dots[0], initialMessage)
assert.Equal(t, want, buf.String())
}

Expand Down