forked from qianlnk/pgbar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint.go
More file actions
107 lines (87 loc) · 1.73 KB
/
print.go
File metadata and controls
107 lines (87 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package pgbar
import (
"fmt"
"strings"
"sync"
"github.com/qianlnk/to"
)
var (
mu sync.Mutex
gSrcLine = 0 //起点行
gCurrentLine = 0 //当前行
gMaxLine = 0 //最大行
)
func move(line int) {
//fmt.Println("\n\n\n\n", gCurrentLine, line)
fmt.Printf("\033[%dA\033[%dB", gCurrentLine, line)
gCurrentLine = line
}
func print(line int, args ...interface{}) {
mu.Lock()
defer mu.Unlock()
move(line)
var realArgs []interface{}
realArgs = append(realArgs, "\r")
realArgs = append(realArgs, args...)
fmt.Print(realArgs...)
move(gMaxLine)
}
func printf(line int, format string, args ...interface{}) {
mu.Lock()
defer mu.Unlock()
move(line)
fmt.Printf("\r"+format, args...)
move(gMaxLine)
}
func println(line int, args ...interface{}) {
mu.Lock()
defer mu.Unlock()
move(line)
var realArgs []interface{}
realArgs = append(realArgs, "\r")
realArgs = append(realArgs, args...)
fmt.Print(realArgs...)
move(gMaxLine)
}
func Print(args ...interface{}) {
mu.Lock()
lf := countLF("", args...)
if gMaxLine == 0 {
gMaxLine += lf + 1
} else {
gMaxLine += lf
}
mu.Unlock()
print(gMaxLine, args...)
}
func Printf(format string, args ...interface{}) {
mu.Lock()
lf := countLF(format, args...)
if gMaxLine == 0 {
gMaxLine += lf + 1
} else {
gMaxLine += lf
}
mu.Unlock()
printf(gMaxLine, format, args...)
}
func Println(args ...interface{}) {
mu.Lock()
lf := countLF("", args...)
lf++
if gMaxLine == 0 {
gMaxLine += lf + 1
} else {
gMaxLine += lf
}
mu.Unlock()
println(gMaxLine, args...)
}
func countLF(format string, args ...interface{}) int {
var count int
count = strings.Count(format, "\n")
for _, arg := range args {
count += strings.Count(to.String(arg), "\n")
}
return count
}