Explorar el Código

打印显示进度修改

kinve hace 1 año
padre
commit
66f5c50318
Se han modificado 2 ficheros con 24 adiciones y 12 borrados
  1. BIN
      main.exe
  2. 24 12
      main.go

BIN
main.exe


+ 24 - 12
main.go

@@ -10,6 +10,7 @@ import (
 	"os"
 	"strings"
 	"sync"
+	"time"
 )
 
 // 配置文件
@@ -80,26 +81,27 @@ type Downloader struct {
 	Current int64
 }
 
-func printProgressBar(progress int, total int) {
-	bar := ""
-	numSlashes := progress * 100 / total / 5
-	for i := 0; i < numSlashes; i++ {
-		bar += "\u2588" // 这是一个完整的块字符
-	}
-	fmt.Printf("下载进度: [%-21s] %.2f%%\r", bar, float64(progress*100.0/total))
+func printProgressBar(progress int64, total int64) {
+	// bar := ""
+	// numSlashes := progress * 100 / total / 5
+	// for i := 0; i < numSlashes; i++ {
+	// 	bar += "\u2588" // 这是一个完整的块字符
+	// }
+	// fmt.Printf("下载进度: [%-21s] %.2f%%\r", bar, float64(progress*100.0/total))
+	fmt.Printf("  正在下载:%d / %d bytes (%.2f%%)\r", progress, total, float32(progress*10000/total)/100)
 }
 
 func (d *Downloader) Read(p []byte) (n int, err error) {
 	n, err = d.Reader.Read(p)
 	d.Current += int64(n)
 
-	printProgressBar(int(d.Current*10000/d.Total), 10000)
+	printProgressBar(d.Current, d.Total)
 	return
 }
 
-func downloadFile(url, filePath string) {
+func downloadFile(item int, url string, filePath string) {
 	defer wg.Done()
-	fmt.Println("下载", filePath)
+	fmt.Printf("%d.下载%s\n", item, filePath)
 	resp, err := http.Get(url)
 	if err != nil {
 		log.Fatalln(err)
@@ -108,6 +110,11 @@ func downloadFile(url, filePath string) {
 		_ = resp.Body.Close()
 	}()
 	file, err := os.Create(filePath)
+	if err != nil {
+		fmt.Println("文件创建失败:", err)
+		// 其他处理
+	}
+
 	defer func() {
 		_ = file.Close()
 	}()
@@ -133,7 +140,7 @@ func http_down(project string, down_list string, proxy string) {
 	folderPath := latest_version
 	err := os.MkdirAll(folderPath, os.ModePerm)
 	if err != nil {
-		fmt.Println("创建文件夹错误: %v", err)
+		fmt.Printf("创建文件夹错误: %v\n", err)
 		return
 	}
 
@@ -150,11 +157,16 @@ func http_down(project string, down_list string, proxy string) {
 		task[git_down_url+filename] = folderPath + "/" + filename
 	}
 
+	item := 1
 	for k, v := range task {
 		wg.Add(1)
-		downloadFile(k, v)
+		downloadFile(item, k, v)
+		item += 1
 	}
 	wg.Wait()
+
+	fmt.Println("下载完成")
+	time.Sleep(1 * time.Second)
 }
 
 func main() {