2 Star 2 Fork 9

王布衣/gox

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
print.go 1.71 KB
一键复制 编辑 原始数据 按行查看 历史
王布衣 提交于 2024-01-24 15:22 . 改interface{}为any
package progressbar
import (
"fmt"
"gitee.com/quant1x/gox/api"
"strings"
"sync"
)
var (
mu sync.Mutex
gSrcLine = 0 //起点行
gCurrentLine = 0 //当前行
gMaxLine = 0 //最大行
)
func barMove(line int) {
//fmt.Println("\n\n\n\n", gCurrentLine, line)
fmt.Printf("\033[%dA\033[%dB", gCurrentLine, line)
gCurrentLine = line
}
func barPrint(line int, args ...any) {
mu.Lock()
defer mu.Unlock()
barMove(line)
var realArgs []any
realArgs = append(realArgs, "\r")
realArgs = append(realArgs, args...)
fmt.Print(realArgs...)
barMove(gMaxLine)
}
func barPrintf(line int, format string, args ...any) {
mu.Lock()
defer mu.Unlock()
barMove(line)
fmt.Printf("\r"+format, args...)
barMove(gMaxLine)
}
func barPrintln(line int, args ...any) {
mu.Lock()
defer mu.Unlock()
barMove(line)
var realArgs []any
realArgs = append(realArgs, "\r")
realArgs = append(realArgs, args...)
fmt.Print(realArgs...)
barMove(gMaxLine)
}
func Print(args ...any) {
mu.Lock()
lf := countLF("", args...)
if gMaxLine == 0 {
gMaxLine += lf + 1
} else {
gMaxLine += lf
}
mu.Unlock()
barPrint(gMaxLine, args...)
}
func Printf(format string, args ...any) {
mu.Lock()
lf := countLF(format, args...)
if gMaxLine == 0 {
gMaxLine += lf + 1
} else {
gMaxLine += lf
}
mu.Unlock()
barPrintf(gMaxLine, format, args...)
}
func Println(args ...any) {
mu.Lock()
lf := countLF("", args...)
lf++
if gMaxLine == 0 {
gMaxLine += lf + 1
} else {
gMaxLine += lf
}
mu.Unlock()
barPrintln(gMaxLine, args...)
}
func countLF(format string, args ...any) int {
var count int
count = strings.Count(format, "\n")
for _, arg := range args {
count += strings.Count(api.ToString(arg), "\n")
}
return count
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/quant1x/gox.git
git@gitee.com:quant1x/gox.git
quant1x
gox
gox
v1.20.3

搜索帮助