1 Star 0 Fork 0

is小强 / coroutine-tool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
progress_bar.go 1.77 KB
一键复制 编辑 原始数据 按行查看 历史
is小强 提交于 2023-10-17 17:34 . 修改包名同时优化进度条
/**
* Created by goland.
* User: adam_wang
* Date: 2023-07-23 00:45:14
*/
package progress_bar
import (
"fmt"
"strconv"
)
// Bar
// @Description: 进度条结构体
type Bar struct {
percent int64 //百分比
cur int64 //当前进度位置
total int64 //总进度
rate string //进度条
graph string //显示符号
}
// NewBar 实例化一个进度条
// @receiver bar *Bar
// @param start int64
// @param total int64
func (bar *Bar) NewBar(start, total int64) {
bar.cur = start
bar.total = total
if bar.graph == "" {
bar.graph = "█"
}
bar.percent = bar.getPercent()
for i := 0; i < int(bar.percent); i += 2 {
//初始化进度条位置
bar.rate += bar.graph
}
}
// NewBarWithGraph 实例化一个进度条(可自定义进度条图形)
// @receiver bar *Bar
// @param start int64
// @param total int64
// @param graph string
func (bar *Bar) NewBarWithGraph(start, total int64, graph string) {
bar.graph = graph
bar.NewBar(start, total)
}
// Run 执行进度条展示
// @receiver bar *Bar
// @param cur int64
func (bar *Bar) Run(cur int64) {
bar.cur = cur
last := bar.percent
bar.percent = bar.getPercent()
if bar.percent != last && bar.percent%2 == 0 {
bar.rate += bar.graph
}
//根据当前设置的进度总数以及进度条占位长度计算总的进度条长度(加上一个固定占位长度)
// TODO:这里后面需要根据真正的百分比和固定长度进行展示
barLen, _ := strconv.Atoi(strconv.FormatInt(bar.total/int64(len(bar.graph)), 10))
barLen += 3
fmt.Printf("\r %-"+strconv.Itoa(barLen)+"s%3d%% %d/%d", bar.rate, bar.percent, bar.cur, bar.total)
}
// 获取进度百分比
// @receiver bar *Bar
// @return int64
func (bar *Bar) getPercent() int64 {
return int64(float32(bar.cur) / float32(bar.total) * 100)
}
Go
1
https://gitee.com/adam-qiang/coroutine-tool.git
git@gitee.com:adam-qiang/coroutine-tool.git
adam-qiang
coroutine-tool
coroutine-tool
v1.0.1

搜索帮助

53164aa7 5694891 3bd8fe86 5694891