Ai
1 Star 0 Fork 0

appplugin/tools

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
format.go 685 Bytes
一键复制 编辑 原始数据 按行查看 历史
icey-yu 提交于 2024-09-02 11:05 +08:00 . fix: bar length print panic
package formatutil
import (
"fmt"
"strings"
)
// ProgressBar generates a formatted progress bar string.
func ProgressBar(name string, progress, total int) string {
var (
percentage float64
barLength = 50
progressLength int
)
if total == 0 {
percentage = 0
progressLength = 0
} else {
percentage = float64(progress) / float64(total) * 100
barLength = 50
progressLength = int(percentage / 100 * float64(barLength))
}
progressLength = min(progressLength, barLength)
bar := strings.Repeat("█", progressLength) + strings.Repeat(" ", barLength-progressLength)
return fmt.Sprintf("\r%s: [%s] %3.0f%% (%d/%d)", name, bar, percentage, progress, total)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/appplugin/tools.git
git@gitee.com:appplugin/tools.git
appplugin
tools
tools
v1.0.2

搜索帮助