1
0
mirror of https://github.com/fumiama/RVC-Models-Downloader.git synced 2024-11-17 13:47:11 +09:00

feat(tui): optimize display

This commit is contained in:
源文雨 2024-04-20 18:12:41 +09:00
parent c79338aa40
commit 8e8aea2cef
4 changed files with 29 additions and 9 deletions

View File

@ -73,6 +73,8 @@ Usage: rvcmd [-notrs] [-dns dns.yaml] 'target/to/download'
-f force download even file exists -f force download even file exists
-notrs -notrs
use standard TLS client use standard TLS client
-notui
use plain text instead of TUI
-w uint -w uint
connection waiting seconds (default 4) connection waiting seconds (default 4)
'target/to/download' 'target/to/download'

View File

@ -73,6 +73,8 @@ Usage: rvcmd [-notrs] [-dns dns.yaml] 'target/to/download'
-f force download even file exists -f force download even file exists
-notrs -notrs
use standard TLS client use standard TLS client
-notui
use plain text instead of TUI
-w uint -w uint
connection waiting seconds (default 4) connection waiting seconds (default 4)
'target/to/download' 'target/to/download'

18
main.go
View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"context"
"flag" "flag"
"fmt" "fmt"
"os" "os"
@ -54,6 +55,8 @@ func main() {
defer ui.Close() defer ui.Close()
sc = newscreen() sc = newscreen()
} }
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() { go func() {
if *dnsf != "" { if *dnsf != "" {
f, err := os.Open(*dnsf) f, err := os.Open(*dnsf)
@ -80,12 +83,21 @@ func main() {
errorln(err) errorln(err)
return return
} }
err = usercfg.download(args[0], "", time.Second*time.Duration(*wait), *cust, !*ntrs, *force) ch := make(chan struct{})
go func() {
err := usercfg.download(args[0], "", time.Second*time.Duration(*wait), *cust, !*ntrs, *force)
ch <- struct{}{}
if err != nil { if err != nil {
errorln(err) errorln(err)
return return
} }
infoln("all download tasks finished.")
}() }()
sc.flushloop(time.Second) select {
case <-ch:
infoln("all download tasks finished.")
case <-ctx.Done():
logrus.Warnln("download canceled")
}
}()
sc.show(time.Second)
} }

10
ui.go
View File

@ -47,7 +47,7 @@ func newscreen() (s screen) {
s.logroll.SetRect(w/2, 0, w, h/2) s.logroll.SetRect(w/2, 0, w, h/2)
s.speedln = widgets.NewPlot() s.speedln = widgets.NewPlot()
s.speedln.Title = "Speed" s.speedln.Title = "Speed (MB/s)"
s.speedln.Data = make([][]float64, 1) s.speedln.Data = make([][]float64, 1)
s.speedln.Data[0] = []float64{0, 0} s.speedln.Data[0] = []float64{0, 0}
s.speedln.AxesColor = ui.ColorWhite s.speedln.AxesColor = ui.ColorWhite
@ -60,18 +60,22 @@ func newscreen() (s screen) {
func (s *screen) logwrite(sz int) { func (s *screen) logwrite(sz int) {
s.Lock() s.Lock()
defer s.Unlock() defer s.Unlock()
w := s.speedln.Size().X
s.totaldl += sz s.totaldl += sz
tdiff := time.Since(s.lastclr) tdiff := time.Since(s.lastclr)
if tdiff > time.Second { if tdiff > time.Second {
s.speedln.Data[0] = append(s.speedln.Data[0], 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.totaldl = 0
s.lastclr = time.Now() s.lastclr = time.Now()
} }
} }
func (s *screen) flushloop(interval time.Duration) { func (s *screen) show(interval time.Duration) {
t := time.NewTicker(interval) t := time.NewTicker(interval)
defer t.Stop() defer t.Stop()
s.flush() s.flush()