diff --git a/README.md b/README.md index 8536a3a..e60ec78 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,8 @@ Usage: rvcmd [-notrs] [-dns dns.yaml] 'target/to/download' -f force download even file exists -notrs use standard TLS client + -notui + use plain text instead of TUI -w uint connection waiting seconds (default 4) 'target/to/download' diff --git a/README_sc.md b/README_sc.md index 1ab18fc..efdf83c 100644 --- a/README_sc.md +++ b/README_sc.md @@ -73,6 +73,8 @@ Usage: rvcmd [-notrs] [-dns dns.yaml] 'target/to/download' -f force download even file exists -notrs use standard TLS client + -notui + use plain text instead of TUI -w uint connection waiting seconds (default 4) 'target/to/download' diff --git a/main.go b/main.go index 9a27592..34fe9be 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "context" "flag" "fmt" "os" @@ -54,6 +55,8 @@ func main() { defer ui.Close() sc = newscreen() } + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() go func() { if *dnsf != "" { f, err := os.Open(*dnsf) @@ -80,12 +83,21 @@ func main() { errorln(err) return } - err = usercfg.download(args[0], "", time.Second*time.Duration(*wait), *cust, !*ntrs, *force) - if err != nil { - errorln(err) - return + ch := make(chan struct{}) + go func() { + err := usercfg.download(args[0], "", time.Second*time.Duration(*wait), *cust, !*ntrs, *force) + ch <- struct{}{} + if err != nil { + errorln(err) + return + } + }() + select { + case <-ch: + infoln("all download tasks finished.") + case <-ctx.Done(): + logrus.Warnln("download canceled") } - infoln("all download tasks finished.") }() - sc.flushloop(time.Second) + sc.show(time.Second) } diff --git a/ui.go b/ui.go index 803d42a..95a9cb3 100644 --- a/ui.go +++ b/ui.go @@ -47,7 +47,7 @@ func newscreen() (s screen) { s.logroll.SetRect(w/2, 0, w, h/2) s.speedln = widgets.NewPlot() - s.speedln.Title = "Speed" + s.speedln.Title = "Speed (MB/s)" s.speedln.Data = make([][]float64, 1) s.speedln.Data[0] = []float64{0, 0} s.speedln.AxesColor = ui.ColorWhite @@ -60,18 +60,22 @@ func newscreen() (s screen) { func (s *screen) logwrite(sz int) { s.Lock() defer s.Unlock() + w := s.speedln.Size().X s.totaldl += sz tdiff := time.Since(s.lastclr) if tdiff > time.Second { s.speedln.Data[0] = append(s.speedln.Data[0], - float64(s.totaldl/1024)/(float64(tdiff)/float64(time.Second)), + float64(s.totaldl/1024)/1024/(float64(tdiff)/float64(time.Second)), ) + if len(s.speedln.Data[0]) > w-5 { + s.speedln.Data[0] = s.speedln.Data[0][1:] + } s.totaldl = 0 s.lastclr = time.Now() } } -func (s *screen) flushloop(interval time.Duration) { +func (s *screen) show(interval time.Duration) { t := time.NewTicker(interval) defer t.Stop() s.flush()