-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtool.go
51 lines (45 loc) · 1 KB
/
tool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"fmt"
"os"
"regexp"
"strconv"
"strings"
)
func parseSize(sizeStr string) int64 {
re := regexp.MustCompile(`(?i)^(\d+(\.\d+)?)([BKMG]?)$`)
matches := re.FindStringSubmatch(sizeStr)
if matches == nil {
fmt.Println("错误:无效的大小格式:", sizeStr)
os.Exit(1)
}
value, _ := strconv.ParseFloat(matches[1], 64)
unit := strings.ToUpper(matches[3])
switch unit {
case "B":
return int64(value)
case "K":
return int64(value * 1024)
case "M":
return int64(value * 1024 * 1024)
case "G", "":
return int64(value * 1024 * 1024 * 1024) // 默认 GB
default:
fmt.Println("错误:未知单位:", unit)
os.Exit(1)
}
return 0
}
// 顯示進度條
func showProgress() {
progress := float64(currentSize) / float64(isoSize) * 100
barLength := int(progress / 2)
remaining := 50 - barLength
fmt.Printf("\r进度: [%s%s] %.2f%% (%d / %d MB)",
strings.Repeat("#", barLength),
strings.Repeat("-", remaining),
progress,
currentSize/1024/1024,
isoSize/1024/1024,
)
}