Skip to content

Commit

Permalink
feat(cmd.fast): add flag for upload speed, show spinner
Browse files Browse the repository at this point in the history
  • Loading branch information
adhocore committed May 6, 2021
1 parent 83290fc commit c897efa
Showing 1 changed file with 53 additions and 1 deletion.
54 changes: 53 additions & 1 deletion cmd/fast/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,61 @@
package main

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

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

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

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

ch := make(chan bool)

go doSpin(ch)
go doFast(ch, &wg, noUp)

wg.Wait()
}

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)
}
}
}
}

func doFast(ch chan bool, 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)
}

fast.Out(res, start)
}

0 comments on commit c897efa

Please sign in to comment.