Skip to content

Commit

Permalink
Merge pull request #9 from Kiura/mac-os-newline-bugfix
Browse files Browse the repository at this point in the history
Mac os newline bugfix
  • Loading branch information
schollz authored Nov 11, 2017
2 parents e627e52 + 9b62a28 commit 702bf7c
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 24 deletions.
7 changes: 7 additions & 0 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ import (
)

func main() {

bar := progressbar.New(1000)

// options for themes
// theme, _ := themes.NewDefault(1)
// theme := themes.New("~")
// bar.SetTheme(theme)

bar.Reset()
for i := 0; i < 1000; i++ {
bar.Add(1)
Expand Down
63 changes: 39 additions & 24 deletions progressbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"os"
"runtime"
"strings"
"sync"
"time"
Expand All @@ -24,27 +25,27 @@ type ProgressBar struct {
startTime time.Time
w io.Writer

// symbols
symbolFinished string
symbolLeft string
leftBookend string
rightBookend string
theme []string

sync.RWMutex
}

func (p *ProgressBar) SetTheme(theme []string) {
p.Lock()
p.theme = theme
p.Unlock()
}

// New returns a new ProgressBar
// with the specified maximum
func New(max int) *ProgressBar {
return &ProgressBar{
max: max,
size: 40,
symbolFinished: "█",
symbolLeft: " ",
leftBookend: "|",
rightBookend: "|",
w: os.Stdout,
lastShown: time.Now(),
startTime: time.Now(),
max: max,
size: 40,
theme: []string{"█", " ", "|", "|"},
w: os.Stdout,
lastShown: time.Now(),
startTime: time.Now(),
}
}

Expand Down Expand Up @@ -107,23 +108,37 @@ func (p *ProgressBar) Show() error {
if p.currentNum > p.max {
return errors.New("current number exceeds max")
}
secondsLeft := time.Since(p.startTime).Seconds() / float64(p.currentNum) * (float64(p.max) - float64(p.currentNum))
s := fmt.Sprintf("\r%4d%% %s%s%s%s [%s:%s] ",
p.currentPercent,
p.leftBookend,
strings.Repeat(p.symbolFinished, p.currentSaucerSize),
strings.Repeat(p.symbolLeft, p.size-p.currentSaucerSize),
p.rightBookend,
(time.Duration(time.Since(p.startTime).Seconds()) * time.Second).String(),
(time.Duration(secondsLeft) * time.Second).String(),
)

s := p.String()

_, err := io.WriteString(p.w, s)
if err != nil {
return err
}

// handle mac os newline
// this breaks your test for some reason
if runtime.GOOS == "darwin" {
fmt.Fprintf(p.w, "\033[%dA", 0)
}

if f, ok := p.w.(*os.File); ok {
f.Sync()
}
return nil
}

func (p *ProgressBar) String() string {
p.RLock()
defer p.RUnlock()
leftTime := time.Since(p.startTime).Seconds() / float64(p.currentNum) * (float64(p.max) - float64(p.currentNum))
return fmt.Sprintf("\r%4d%% %s%s%s%s [%s:%s] ",
p.currentPercent,
p.theme[2],
strings.Repeat(p.theme[0], p.currentSaucerSize),
strings.Repeat(p.theme[1], p.size-p.currentSaucerSize),
p.theme[3],
(time.Duration(time.Since(p.startTime).Seconds()) * time.Second).String(),
(time.Duration(leftTime) * time.Second).String(),
)
}
44 changes: 44 additions & 0 deletions themes/themes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package themes

import (
"errors"
)

var defaultSymbolsFinished = []rune("█◎▭☢≈⋮─━═=╸")
var defaultTheme = []rune("█ ||")

// New returns a new theme
func New(symbols ...string) []string {
symbols = append(symbols, make([]string, 4-len(symbols))...)
for i, _ := range symbols {
if len(symbols[i]) == 0 {
symbols[i] = string(defaultTheme[i])
}
}
return []string{
symbols[0],
symbols[1],
symbols[2],
symbols[3],
}
}

// NewDefault returns nth theme from default themes
func NewDefault(n uint8) ([]string, error) {
if n > uint8(len(defaultSymbolsFinished)) {
return nil, errors.New("n must be less than defined themes")
}
return New(string(defaultSymbolsFinished[n])), nil
}

func NewFromRunes(symbols []rune) ([]string, error) {
if len(symbols) != 4 {
return []string{}, errors.New("symbols lenght must be exactly 4")
}
return []string{
string(symbols[0]),
string(symbols[1]),
string(symbols[2]),
string(symbols[3]),
}, nil
}
9 changes: 9 additions & 0 deletions themes/themes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package themes

import "testing"

func TestNewDefault(t *testing.T) {
if _, err := NewDefault(uint8(len(defaultSymbolsFinished) + 1)); err == nil {
t.Error("should have an error if n > default themes")
}
}

0 comments on commit 702bf7c

Please sign in to comment.