mirror of
https://gitee.com/zhangsken/aliyunpan.git
synced 2026-10-16 02:23:49 +08:00
beautify download ui dashboard
This commit is contained in:
@@ -647,6 +647,8 @@ func (dtu *DownloadTaskUnit) Run() (result *taskframework.TaskUnitRunResult) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dtu.logf("[%s] 将会下载到路径: %s\n", dtu.taskInfo.Id(), dtu.SavePath)
|
dtu.logf("[%s] 将会下载到路径: %s\n", dtu.taskInfo.Id(), dtu.SavePath)
|
||||||
|
// 更新UI面板任务状态
|
||||||
|
dtu.updateUITaskState(ui.TaskRunning, "")
|
||||||
|
|
||||||
var ok bool
|
var ok bool
|
||||||
er := dtu.download()
|
er := dtu.download()
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ package ui
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/tickstep/aliyunpan/internal/utils"
|
||||||
"io"
|
"io"
|
||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
@@ -430,9 +431,8 @@ func (db *DownloadDashboard) headerLines(snapshot dashboardSnapshot, innerWidth
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
speedStr := converter.ConvertFileSize(speed, 2) + "/s"
|
|
||||||
line1 := fmt.Sprintf("总速度: %s | 文件: %d/%d | 失败: %d | 已用时间: %s | 剩余时间: %s | 状态: %s",
|
line1 := fmt.Sprintf("总速度: %s | 文件: %d/%d | 失败: %d | 已用时间: %s | 剩余时间: %s | 状态: %s",
|
||||||
speedStr, doneFiles, totalFiles, failedFiles, formatDurationShort(elapsed), left, status)
|
utils.FormatSpeedFixedWidth(speed, 11), doneFiles, totalFiles, failedFiles, formatDurationShort(elapsed), left, status)
|
||||||
line1 = fitLine(line1, innerWidth)
|
line1 = fitLine(line1, innerWidth)
|
||||||
|
|
||||||
progressPct := 0.0
|
progressPct := 0.0
|
||||||
@@ -611,7 +611,10 @@ func renderTaskLine(task *downloadTask, index int, width int) string {
|
|||||||
percent := "--.-%"
|
percent := "--.-%"
|
||||||
if task.total > 0 {
|
if task.total > 0 {
|
||||||
progressPct = float64(task.downloaded) / float64(task.total)
|
progressPct = float64(task.downloaded) / float64(task.total)
|
||||||
percent = fmt.Sprintf("%5.1f%%", math.Min(100, progressPct*100))
|
percent = fmt.Sprintf("%5.1f%%", math.Min(100, progressPct*100)) // 将浮点数格式化为:总宽度 5 个字符,包含 1 位小数,后面加百分号
|
||||||
|
} else {
|
||||||
|
// 空闲队列左边多一个空格,对齐活跃队列
|
||||||
|
percent = " --.-%"
|
||||||
}
|
}
|
||||||
|
|
||||||
// 速度
|
// 速度
|
||||||
@@ -644,7 +647,7 @@ func renderTaskLine(task *downloadTask, index int, width int) string {
|
|||||||
rateWidth := 12
|
rateWidth := 12
|
||||||
etaWidth := 8
|
etaWidth := 8
|
||||||
percentWidth := displayWidth(percent)
|
percentWidth := displayWidth(percent)
|
||||||
fixed := displayWidth(prefix) + 1 + 2 + barWidth + 1 + percentWidth + 1 + rateWidth + 1 + etaWidth
|
fixed := displayWidth(prefix) + 1 + 2 /*进度条左右[]括号*/ + barWidth + 1 + percentWidth + 1 + rateWidth + 1 + etaWidth
|
||||||
nameWidth := width - fixed
|
nameWidth := width - fixed
|
||||||
if nameWidth < 8 {
|
if nameWidth < 8 {
|
||||||
reduce := 8 - nameWidth
|
reduce := 8 - nameWidth
|
||||||
@@ -663,7 +666,7 @@ func renderTaskLine(task *downloadTask, index int, width int) string {
|
|||||||
etaWidth -= cut
|
etaWidth -= cut
|
||||||
reduce -= cut
|
reduce -= cut
|
||||||
}
|
}
|
||||||
fixed = displayWidth(prefix) + 1 + 2 + barWidth + 1 + percentWidth + 1 + rateWidth + 1 + etaWidth
|
fixed = displayWidth(prefix) + 1 /*1个空格分隔*/ + 2 /*进度条左右[]括号*/ + barWidth /*进度条长度*/ + 1 /*1个空格分隔*/ + percentWidth + 1 /*1个空格分隔*/ + rateWidth + 1 /*1个空格分隔*/ + etaWidth
|
||||||
nameWidth = width - fixed
|
nameWidth = width - fixed
|
||||||
if nameWidth < 4 {
|
if nameWidth < 4 {
|
||||||
nameWidth = 4
|
nameWidth = 4
|
||||||
|
|||||||
@@ -301,3 +301,49 @@ func UnixTime2LocalFormatStr(unixTime int64) string {
|
|||||||
cz := time.FixedZone("CST", 8*3600) // 东8区
|
cz := time.FixedZone("CST", 8*3600) // 东8区
|
||||||
return t.In(cz).Format("2006-01-02T15:04:05.000Z")
|
return t.In(cz).Format("2006-01-02T15:04:05.000Z")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FormatSpeedFixedWidth 格式化速度显示
|
||||||
|
// speed: 速度值,单位 Byte/s
|
||||||
|
// 返回格式: " xxx.xx MB/s" (固定长度,左侧用空格补齐)
|
||||||
|
func FormatSpeedFixedWidth(speed int64, totalWidth int) string {
|
||||||
|
const (
|
||||||
|
kb = 1024.0
|
||||||
|
mb = kb * 1024
|
||||||
|
gb = mb * 1024
|
||||||
|
tb = gb * 1024
|
||||||
|
)
|
||||||
|
|
||||||
|
var value float64
|
||||||
|
var unit string
|
||||||
|
|
||||||
|
speedFloat := float64(speed)
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case speedFloat >= tb:
|
||||||
|
value = speedFloat / tb
|
||||||
|
unit = "TB/s"
|
||||||
|
case speedFloat >= gb:
|
||||||
|
value = speedFloat / gb
|
||||||
|
unit = "GB/s"
|
||||||
|
case speedFloat >= mb:
|
||||||
|
value = speedFloat / mb
|
||||||
|
unit = "MB/s"
|
||||||
|
case speedFloat >= kb:
|
||||||
|
value = speedFloat / kb
|
||||||
|
unit = "KB/s"
|
||||||
|
default:
|
||||||
|
value = speedFloat
|
||||||
|
unit = "B/s"
|
||||||
|
}
|
||||||
|
|
||||||
|
// 格式化为两位小数
|
||||||
|
formatted := fmt.Sprintf("%.2f %s", value, unit)
|
||||||
|
|
||||||
|
// 如果不足指定宽度,左侧用空格补齐
|
||||||
|
if len(formatted) < totalWidth {
|
||||||
|
spaces := strings.Repeat(" ", totalWidth-len(formatted))
|
||||||
|
return spaces + formatted
|
||||||
|
}
|
||||||
|
|
||||||
|
return formatted
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user