Skip to content

Commit

Permalink
godoc
Browse files Browse the repository at this point in the history
  • Loading branch information
Shai Nagar committed Jun 1, 2021
1 parent 1569180 commit 63d8224
Showing 4 changed files with 60 additions and 27 deletions.
28 changes: 14 additions & 14 deletions internal/main.go
Original file line number Diff line number Diff line change
@@ -115,12 +115,12 @@ func demoSpinner(ctx *demoContext) {
m := termite.NewMatrix(termite.StdoutWriter, progressRefreshInterval)
cancel := m.Start()

customFormatter1 := &CustomSpinnerFormatter{
customFormatter1 := &customSpinnerFormatter{
charSeq: []string{"\u2588", "\u2587", "\u2586", "\u2585", "\u2584", "\u2583", "\u2582", "\u2581"},
formatTitleFn: color.CyanString,
formatIndicatorFn: color.RedString,
}
customFormatter2 := &CustomSpinnerFormatter{
customFormatter2 := &customSpinnerFormatter{
charSeq: []string{"/", "-", "\\", "|"},
formatTitleFn: color.MagentaString,
formatIndicatorFn: color.GreenString,
@@ -199,10 +199,10 @@ func demoConcurrentProgressBars(ctx *demoContext) {

termWidth := ctx.termWidth
termite.AllocateNewLines(4) // allocate 4 lines
tick1, cancel1 = progressTickerWith(termWidth*1/8, &CustomProgressBarFormatter{Fill: '\u258C', formatBorderFn: color.WhiteString, formatFillFn: color.HiCyanString})
tick2, cancel2 = progressTickerWith(termWidth*1/4, &CustomProgressBarFormatter{Fill: '\u2592', formatBorderFn: color.YellowString, formatFillFn: color.BlueString})
tick3, cancel3 = progressTickerWith(termWidth*3/8, &CustomProgressBarFormatter{Fill: '\u2591', formatBorderFn: color.GreenString, formatFillFn: color.RedString})
tick4, cancel4 = progressTickerWith(termWidth*1/2, &CustomProgressBarFormatter{Fill: '\u2587', formatBorderFn: color.RedString, formatFillFn: color.GreenString})
tick1, cancel1 = progressTickerWith(termWidth*1/8, &customProgressBarFormatter{Fill: '\u258C', formatBorderFn: color.WhiteString, formatFillFn: color.HiCyanString})
tick2, cancel2 = progressTickerWith(termWidth*1/4, &customProgressBarFormatter{Fill: '\u2592', formatBorderFn: color.YellowString, formatFillFn: color.BlueString})
tick3, cancel3 = progressTickerWith(termWidth*3/8, &customProgressBarFormatter{Fill: '\u2591', formatBorderFn: color.GreenString, formatFillFn: color.RedString})
tick4, cancel4 = progressTickerWith(termWidth*1/2, &customProgressBarFormatter{Fill: '\u2587', formatBorderFn: color.RedString, formatFillFn: color.GreenString})

defer func() {
cancel1()
@@ -237,38 +237,38 @@ func printTitle(s string, ctx *demoContext) {
termite.Println("")
}

type CustomSpinnerFormatter struct {
type customSpinnerFormatter struct {
charSeq []string
formatTitleFn func(format string, a ...interface{}) string
formatIndicatorFn func(format string, a ...interface{}) string
}

func (f *CustomSpinnerFormatter) FormatTitle(s string) string {
func (f *customSpinnerFormatter) FormatTitle(s string) string {
return f.formatTitleFn(s)
}

func (f *CustomSpinnerFormatter) FormatIndicator(char string) string {
func (f *customSpinnerFormatter) FormatIndicator(char string) string {
return f.formatIndicatorFn(char)
}

func (f *CustomSpinnerFormatter) CharSeq() []string {
func (f *customSpinnerFormatter) CharSeq() []string {
return f.charSeq
}

type CustomProgressBarFormatter struct {
type customProgressBarFormatter struct {
Fill rune
formatBorderFn func(format string, a ...interface{}) string
formatFillFn func(format string, a ...interface{}) string
}

func (f *CustomProgressBarFormatter) FormatLeftBorder() string {
func (f *customProgressBarFormatter) FormatLeftBorder() string {
return f.formatBorderFn(fmt.Sprintf("%c", termite.DefaultProgressBarLeftBorder))
}

func (f *CustomProgressBarFormatter) FormatRightBorder() string {
func (f *customProgressBarFormatter) FormatRightBorder() string {
return f.formatBorderFn(fmt.Sprintf("%c", termite.DefaultProgressBarLeftBorder))
}

func (f *CustomProgressBarFormatter) FormatFill() string {
func (f *customProgressBarFormatter) FormatFill() string {
return f.formatFillFn(fmt.Sprintf("%c", f.Fill))
}
43 changes: 31 additions & 12 deletions progress_bar.go
Original file line number Diff line number Diff line change
@@ -10,41 +10,60 @@ import (
)

const (
DefaultProgressBarLeftBorder = '\u258F'
// DefaultProgressBarLeftBorder default progress bar left border character
DefaultProgressBarLeftBorder = '\u258F'

// DefaultProgressBarRightBorder default progress bar right border character
DefaultProgressBarRightBorder = '\u2595'
DefaultProgressBarFill = '\u2587'

// DefaultProgressBarFill default progress bar fill character
DefaultProgressBarFill = '\u2587'
)

func DefaultProgressBarFormatter() ProgressBarFormatter {
// DefaultProgressBarFormatter returns a new instance of the default ProgressBarFormatter
func DefaultProgressBarFormatter() *SimpleProgressBarFormatter {
return &SimpleProgressBarFormatter{
LeftBorder: DefaultProgressBarLeftBorder,
RightBorder: DefaultProgressBarRightBorder,
Fill: DefaultProgressBarFill,
LeftBorderChar: DefaultProgressBarLeftBorder,
RightBorderChar: DefaultProgressBarRightBorder,
FillChar: DefaultProgressBarFill,
}
}

// ProgressBarFormatter a formatter to control the style of a ProgressBar.
type ProgressBarFormatter interface {
// FormatLeftBorder returns a string that contains one visible character and optionally
// additional styling charatcers such as color codes, background and other effects.
FormatLeftBorder() string

// FormatRightBorder returns a string that contains one visible character and optionally
// additional styling charatcers such as color codes, background and other effects.
FormatRightBorder() string

// FormatFill returns a string that contains one visible character and optionally
// additional styling charatcers such as color codes, background and other effects.
FormatFill() string
}

// SimpleProgressBarFormatter a simple ProgressBarFormatter implementation which is based on constructor values.
type SimpleProgressBarFormatter struct {
LeftBorder rune
RightBorder rune
Fill rune
LeftBorderChar rune
RightBorderChar rune
FillChar rune
}

// FormatLeftBorder returns the left border char
func (f *SimpleProgressBarFormatter) FormatLeftBorder() string {
return fmt.Sprintf("%c", f.LeftBorder)
return fmt.Sprintf("%c", f.LeftBorderChar)
}

// FormatRightBorder returns the right border char
func (f *SimpleProgressBarFormatter) FormatRightBorder() string {
return fmt.Sprintf("%c", f.RightBorder)
return fmt.Sprintf("%c", f.RightBorderChar)
}

// FormatFill returns the fill char
func (f *SimpleProgressBarFormatter) FormatFill() string {
return fmt.Sprintf("%c", f.Fill)
return fmt.Sprintf("%c", f.FillChar)
}

// TickFn a tick handle
2 changes: 1 addition & 1 deletion progress_bar_test.go
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ import (
)

const (
fakeTerminalWidth = 100
fakeTerminalWidth = 100
)

func TestFullWidthProgressBar(t *testing.T) {
14 changes: 14 additions & 0 deletions spinner.go
Original file line number Diff line number Diff line change
@@ -10,30 +10,44 @@ import (
"time"
)

// DefaultSpinnerCharSeq returns the default character sequence of a spinner.
func DefaultSpinnerCharSeq() []string {
return []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"}
}

// DefaultSpinnerFormatter returns a default
func DefaultSpinnerFormatter() SpinnerFormatter {
return &SimpleSpinnerFormatter{}
}

// SpinnerFormatter a formatter to be used with a Spinner to customize its style.
type SpinnerFormatter interface {
// FormatTitle returns the input string with optional styling codes or anything else.
FormatTitle(s string) string

// FormatIndicator returns a string that contains one visible character - the one passed as input -
// and optionally additional styling charatcers such as color codes, background and other effects.
FormatIndicator(char string) string

// CharSeq the character sequence to use as indicators.
CharSeq() []string
}

// SimpleSpinnerFormatter a simple spinner formatter implementation that uses the default
// spinner character sequence and passes the title and the indicator setrings unchanged.
type SimpleSpinnerFormatter struct{}

// FormatTitle returns the input title as is
func (f *SimpleSpinnerFormatter) FormatTitle(s string) string {
return s
}

// FormatIndicator returns the input char as is
func (f *SimpleSpinnerFormatter) FormatIndicator(char string) string {
return char
}

// CharSeq returns the default character sequence.
func (f *SimpleSpinnerFormatter) CharSeq() []string {
return DefaultSpinnerCharSeq()
}

0 comments on commit 63d8224

Please sign in to comment.