Skip to content

Commit

Permalink
refactor: use adhocore/chin for spinner
Browse files Browse the repository at this point in the history
  • Loading branch information
adhocore committed Oct 24, 2022
1 parent 32cacb1 commit 1fd2628
Showing 1 changed file with 13 additions and 30 deletions.
43 changes: 13 additions & 30 deletions cmd/fast/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,43 @@ package main

import (
"flag"
"fmt"
"log"
"sync"
"time"

"github.com/adhocore/chin"
"github.com/adhocore/fast/internal/fast"
)

func main() {
var noUp bool
var wg sync.WaitGroup
var noUp bool

func init() {
flag.BoolVar(&noUp, "noup", false, "Do not show upload speed (shows only download speed)")
flag.Parse()
wg.Add(1)
}

ch := make(chan bool)
func main() {
var wg sync.WaitGroup

go doSpin(ch)
go doFast(ch, &wg, noUp)
s := chin.New().WithWait(&wg)
go s.Start()

wg.Wait()
}
wg.Add(1)
go doFast(s, &wg, noUp)

func doSpin(ch chan bool) {
chars := []string{"+", "\\", "|", "/", "-", "+", "\\", "|", "/", "-"}

for {
outer:
select {
case _, ok := <-ch:
if ok {
fmt.Print("\010")
break outer
}
default:
for _, c := range chars {
fmt.Print(c, "\010")
time.Sleep(50 * time.Millisecond)
}
}
}
wg.Wait()
}

func doFast(ch chan bool, wg *sync.WaitGroup, noUp bool) {
func doFast(s *chin.Chin, wg *sync.WaitGroup, noUp bool) {
defer wg.Done()

start := time.Now()
res, err := fast.Measure(noUp)
ch <- true

if err != nil {
log.Fatalf("error measuring speed: %v", err)
}

s.Stop()
fast.Out(res, start)
}

0 comments on commit 1fd2628

Please sign in to comment.