2018-06-30 23:05:31 +08:00
|
|
|
// Show the dynamic progress bar
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/accounting"
|
|
|
|
"github.com/rclone/rclone/fs/log"
|
2018-06-30 23:05:31 +08:00
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// interval between progress prints
|
|
|
|
defaultProgressInterval = 500 * time.Millisecond
|
|
|
|
// time format for logging
|
|
|
|
logTimeFormat = "2006-01-02 15:04:05"
|
|
|
|
)
|
|
|
|
|
|
|
|
// startProgress starts the progress bar printing
|
|
|
|
//
|
2018-10-04 04:46:18 +08:00
|
|
|
// It returns a func which should be called to stop the stats.
|
|
|
|
func startProgress() func() {
|
2018-12-19 20:36:29 +08:00
|
|
|
err := initTerminal()
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(nil, "Failed to start progress: %v", err)
|
|
|
|
return func() {}
|
|
|
|
}
|
2018-06-30 23:05:31 +08:00
|
|
|
stopStats := make(chan struct{})
|
|
|
|
oldLogPrint := fs.LogPrint
|
|
|
|
if !log.Redirected() {
|
|
|
|
// Intercept the log calls if not logging to file or syslog
|
|
|
|
fs.LogPrint = func(level fs.LogLevel, text string) {
|
|
|
|
printProgress(fmt.Sprintf("%s %-6s: %s", time.Now().Format(logTimeFormat), level, text))
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2018-10-04 04:46:18 +08:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
2018-06-30 23:05:31 +08:00
|
|
|
go func() {
|
2018-10-04 04:46:18 +08:00
|
|
|
defer wg.Done()
|
2018-06-30 23:05:31 +08:00
|
|
|
progressInterval := defaultProgressInterval
|
2018-09-04 21:39:48 +08:00
|
|
|
if ShowStats() && *statsInterval > 0 {
|
2018-06-30 23:05:31 +08:00
|
|
|
progressInterval = *statsInterval
|
|
|
|
}
|
|
|
|
ticker := time.NewTicker(progressInterval)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
printProgress("")
|
|
|
|
case <-stopStats:
|
|
|
|
ticker.Stop()
|
2018-11-11 17:57:37 +08:00
|
|
|
printProgress("")
|
2018-06-30 23:05:31 +08:00
|
|
|
fs.LogPrint = oldLogPrint
|
|
|
|
fmt.Println("")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2018-10-04 04:46:18 +08:00
|
|
|
return func() {
|
|
|
|
close(stopStats)
|
|
|
|
wg.Wait()
|
|
|
|
}
|
2018-06-30 23:05:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// VT100 codes
|
|
|
|
const (
|
|
|
|
eraseLine = "\x1b[2K"
|
|
|
|
moveToStartOfLine = "\x1b[0G"
|
|
|
|
moveUp = "\x1b[A"
|
|
|
|
)
|
|
|
|
|
|
|
|
// state for the progress printing
|
|
|
|
var (
|
|
|
|
nlines = 0 // number of lines in the previous stats block
|
|
|
|
progressMu sync.Mutex
|
|
|
|
)
|
|
|
|
|
2019-04-30 20:06:24 +08:00
|
|
|
// printProgress prints the progress with an optional log
|
2018-06-30 23:05:31 +08:00
|
|
|
func printProgress(logMessage string) {
|
2019-08-08 22:19:41 +08:00
|
|
|
progressMu.Lock()
|
|
|
|
defer progressMu.Unlock()
|
|
|
|
|
2018-06-30 23:05:31 +08:00
|
|
|
var buf bytes.Buffer
|
|
|
|
w, h, err := terminal.GetSize(int(os.Stdout.Fd()))
|
|
|
|
if err != nil {
|
|
|
|
w, h = 80, 25
|
|
|
|
}
|
|
|
|
_ = h
|
2019-07-18 18:13:54 +08:00
|
|
|
stats := strings.TrimSpace(accounting.GlobalStats().String())
|
2018-06-30 23:05:31 +08:00
|
|
|
logMessage = strings.TrimSpace(logMessage)
|
|
|
|
|
|
|
|
out := func(s string) {
|
|
|
|
buf.WriteString(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
if logMessage != "" {
|
|
|
|
out("\n")
|
|
|
|
out(moveUp)
|
|
|
|
}
|
|
|
|
// Move to the start of the block we wrote erasing all the previous lines
|
|
|
|
for i := 0; i < nlines-1; i++ {
|
|
|
|
out(eraseLine)
|
|
|
|
out(moveUp)
|
|
|
|
}
|
|
|
|
out(eraseLine)
|
|
|
|
out(moveToStartOfLine)
|
|
|
|
if logMessage != "" {
|
|
|
|
out(eraseLine)
|
|
|
|
out(logMessage + "\n")
|
|
|
|
}
|
|
|
|
fixedLines := strings.Split(stats, "\n")
|
|
|
|
nlines = len(fixedLines)
|
|
|
|
for i, line := range fixedLines {
|
|
|
|
if len(line) > w {
|
|
|
|
line = line[:w]
|
|
|
|
}
|
|
|
|
out(line)
|
|
|
|
if i != nlines-1 {
|
|
|
|
out("\n")
|
|
|
|
}
|
|
|
|
}
|
2018-08-17 23:59:59 +08:00
|
|
|
writeToTerminal(buf.Bytes())
|
2018-06-30 23:05:31 +08:00
|
|
|
}
|