Skip to content

Commit

Permalink
feat(tui): optimize display
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Apr 20, 2024
1 parent c79338a commit 8e8aea2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 2 additions & 0 deletions README_sc.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
24 changes: 18 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"flag"
"fmt"
"os"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
10 changes: 7 additions & 3 deletions ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down

0 comments on commit 8e8aea2

Please sign in to comment.