Skip to content

Commit

Permalink
better api
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Dec 12, 2016
1 parent a7d3849 commit f1f63cc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# spin-ext

A very simple spinner for cli golang apps.

Example:

```go
Expand All @@ -9,15 +11,13 @@ import (
"fmt"
"time"

spin "github.com/caarlos0/spin-ext"
"github.com/caarlos0/spin"
)

func main() {
spin := spin.New(`⦾⦿`, "%s Working...")
spin.Work(func() error {
time.Sleep(1000 * time.Millisecond)
return nil
})
fmt.Println("Done!")
s := spin.New("%s Working...")
s.Start()
time.Sleep(100 * 20 * time.Millisecond)
s.Stop()
}
```
8 changes: 4 additions & 4 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"time"

spin "github.com/caarlos0/spin-ext"
"github.com/caarlos0/spin"
)

func main() {
Expand Down Expand Up @@ -32,7 +32,7 @@ func main() {
func show(name, frames string) {
s := spin.New(" \033[36m[" + name + "] computing\033[m %s")
s.Set(frames)
s.Work(func() {
time.Sleep(100 * 10 * time.Millisecond)
})
s.Start()
time.Sleep(100 * 20 * time.Millisecond)
s.Stop()
}
9 changes: 6 additions & 3 deletions spin.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,19 @@ func (s *Spinner) Set(frames string) {
s.length = len(s.frames)
}

// Work shows the spinner, execute the given task and then hide the spinner
func (s *Spinner) Work(task func()) {
// Start shows the spinner
func (s *Spinner) Start() {
s.active = true
go func() {
for s.active {
fmt.Printf(s.text, s.next())
time.Sleep(100 * time.Millisecond)
}
}()
task()
}

// Stop hides the spinner
func (s *Spinner) Stop() {
s.active = false
fmt.Printf(ClearLine)
}
Expand Down

0 comments on commit f1f63cc

Please sign in to comment.