Skip to content

Commit

Permalink
优化更新进度显示
Browse files Browse the repository at this point in the history
  • Loading branch information
Jrohy committed Jan 17, 2022
1 parent 03b1a43 commit 141fd43
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
6 changes: 5 additions & 1 deletion data.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ func checkCfw() *cfwInfo {
if len(child) > 1 {
info, _ := item.Cmdline()
ci.rootPath = strings.Trim(path.Dir(strings.Replace(info, "\\", "/", -1)), "\"")
ci.version = getExeVersion(strings.Replace(info, "\"", "", -1))
info = strings.Replace(info, "\"", "", -1)
if !IsExists(info) {
exit("请尝试以管理员身份运行此程序")
}
ci.version = getExeVersion(info)
ci.process = item
if IsExists(fmt.Sprintf("%s/Uninstall Clash for Windows.exe", ci.rootPath)) {
ci.installVersion = true
Expand Down
18 changes: 15 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,14 @@ func downloadPack() []*downloadInfo {
}

func updateCfw(cfw *cfwInfo, diList []*downloadInfo) {
var stopCh chan struct{}
closeChan := func() {
close(stopCh)
fmt.Printf("\n\n")
}
if updateCore || updateTrans {
fmt.Println("更新cfw中...")
stopCh = make(chan struct{})
go showProgress("更新cfw中", stopCh)
cfw.process.Kill()
}
if updateTrans {
Expand All @@ -98,13 +104,15 @@ func updateCfw(cfw *cfwInfo, diList []*downloadInfo) {
err = copy.Copy(fullPath(path.Join(diList[0].fileName, "app.asar")), path.Join(cfw.rootPath, "resources/app.asar"))
}
if err != nil {
closeChan()
fmt.Println("请尝试以管理员身份运行此程序:")
exit(err.Error())
}
}
if updateCore {
if cfw.portableData {
if err := copy.Copy(cfw.rootPath+"/data", fullPath(diList[0].fileName+"/data")); err != nil {
closeChan()
exit(err.Error())
}
}
Expand All @@ -114,6 +122,7 @@ func updateCfw(cfw *cfwInfo, diList []*downloadInfo) {
}
}
if err := copy.Copy(fullPath(diList[0].fileName), cfw.rootPath); err != nil {
closeChan()
exit(err.Error())
}
}
Expand All @@ -125,6 +134,7 @@ func updateCfw(cfw *cfwInfo, diList []*downloadInfo) {
break
}
}
closeChan()
fmt.Printf("更新成功!\n\n")
}
}
Expand All @@ -139,8 +149,10 @@ func checkEnv() *cfwInfo {
proxyUrl := fmt.Sprintf("127.0.0.1:%s", cfwInfo.mixPort)
os.Setenv("HTTP_PROXY", proxyUrl)
os.Setenv("HTTPS_PROXY", proxyUrl)
fmt.Println("正在获取cfw最新的版本号..")
cfwVersionList = recentlyTag("https://github.com/Fndroid/clash_for_windows_pkg/tags")
if !cfwInfo.installVersion {
fmt.Println("正在获取cfw最新的版本号..")
cfwVersionList = recentlyTag("https://github.com/Fndroid/clash_for_windows_pkg/tags")
}
updateUpdater()
if !cfwInfo.installVersion {
cfwSelect()
Expand Down
17 changes: 17 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,20 @@ func startBackground() {
exit(err.Error())
}
}

func showProgress(tip string, stopCh chan struct{}) {
count := 1
for {
select {
case <-stopCh:
return
default:
if count > 3 {
count = 1
}
fmt.Printf("\r%s%-3s", tip, strings.Repeat(".", count))
time.Sleep(time.Millisecond * 500)
count++
}
}
}

0 comments on commit 141fd43

Please sign in to comment.