-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtui.go
223 lines (177 loc) · 4.33 KB
/
tui.go
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package main
import (
"easy-copy/color"
"easy-copy/flags"
"easy-copy/input"
"easy-copy/progress"
"easy-copy/tasks"
"easy-copy/ui"
"fmt"
"math"
"os"
"path/filepath"
"strconv"
"time"
)
const (
barWidth = 50 // in px
maxWidth = 80
redrawSpeed = 100 // in ms
)
var lines int = 0
func drawLoop() {
for !progress.CopyDone {
for i := 0; i < lines; i++ {
fmt.Print("\033[1A\033[2K")
}
lines = 0
if flags.Current.Verbosity() > flags.VerbQuiet {
printBar()
unit := ui.SizeAutoUnit(float64(progress.FullSize))
fmt.Print(ui.FormatSize(float64(progress.DoneSize), unit))
fmt.Print(" / ")
fmt.Print(ui.FormatSize(float64(progress.FullSize), unit))
fmt.Println()
lines++
printOperation()
fmt.Println()
lines++
fmt.Println()
lines++
}
printConflict()
time.Sleep(redrawSpeed * time.Millisecond)
}
}
func printBar() {
var barFilled int
if progress.FullSize == 0 {
// unneeded as this is only called after the iterator is done
barFilled = barWidth / 2
} else {
barFilled = int(math.Round(float64(barWidth) * float64(progress.DoneSize) / float64(progress.FullSize)))
}
fmt.Print(" [")
for i := 0; i < barFilled-1; i++ {
fmt.Print("=")
}
if barFilled == barWidth {
fmt.Print("=")
} else {
fmt.Print(">")
}
for i := barFilled; i < barWidth; i++ {
fmt.Print(" ")
}
fmt.Print("] ")
}
func printOperation() {
fmt.Print(" ")
switch progress.CurrentTask {
case progress.TaskCopy:
fmt.Print("Copying " + ui.ShrinkPath(progress.CurrentFile, maxWidth/2))
case progress.TaskLink:
fmt.Print("Linking " + ui.ShrinkPath(progress.CurrentFile, maxWidth/2))
case progress.TaskMkdir:
fmt.Print("Creating " + ui.ShrinkPath(progress.CurrentFile, maxWidth/2))
}
fmt.Print(" @ ")
fmt.Print(ui.FormatSize(float64(progress.SizePerSecond),
ui.SizeAutoUnit(float64(progress.SizePerSecond))))
fmt.Print("/s")
// remaining time:
secondsLeft := float32(progress.FullSize-progress.DoneSize) / progress.SizePerSecond
fmt.Print(", ")
fmt.Print(ui.FormatSeconds(float64(secondsLeft)))
fmt.Print(" remaining")
}
func printConflict() {
c := tasks.ReadPendingConflict() // only pop when user answered
if c == nil {
return
}
conflict := c.Source
conflict = filepath.Base(conflict)
fmt.Print(color.FGColors.Yellow, color.Text.Bold)
fmt.Print(conflict)
fmt.Print(color.Text.Reset, color.FGColors.Magenta)
fmt.Print(" already exists in ")
fmt.Print(color.FGColors.Yellow, color.Text.Bold)
fmt.Print(filepath.Dir(c.Dest))
fmt.Println(color.Text.Reset + color.FGColors.Magenta)
lines++
fmt.Println("[S]kip | Skip [A]ll | [O]verwrite | O[v]erwrite All")
lines++
fmt.Print("[I]nfo | [R]ename target | [Q]uit")
fmt.Println(color.Text.Reset)
lines++
in := input.GetChoice("saovirq")
switch in {
case 's':
skipFile(c.Source)
case 'o':
tasks.PushSolvedConflict(*c)
case 'a':
flags.Current.SetOnConflict(flags.ConflictSkip)
tasks.ClearPendingConflicts()
case 'v':
flags.Current.SetOnConflict(flags.ConflictOverwrite)
tasks.SolveAllConflicts()
case 'i':
panic("not supported")
case 'r':
panic("not supported")
case 'q':
os.Exit(0)
}
tasks.PopPendingConflict()
}
/**
* Add the file size to done_size and 1 to done_amount.
*/
func skipFile(path string) {
stat, err := os.Lstat(path)
if err != nil {
progress.DoneSize += 0
} else if stat.Mode().IsRegular() {
progress.DoneSize += uint64(stat.Size())
} else if stat.Mode()&os.ModeSymlink != 0 {
progress.DoneSize += uint64(progress.SymlinkSize)
}
progress.DoneAmount++
}
func printSummary() {
if flags.Current.Verbosity() <= flags.VerbCrit {
return
}
elapsed := time.Since(progress.Start)
for i := 0; i < lines; i++ {
fmt.Print("\033[1A\033[2K")
}
fmt.Print(" [")
for i := 0; i < barWidth; i++ {
fmt.Print("=")
}
fmt.Println("]")
fmt.Print(" ")
switch flags.Current.Mode() {
case flags.ModeCopy:
fmt.Print("Copied ")
case flags.ModeMove:
fmt.Print("Moved ")
case flags.ModeRemove:
fmt.Print("Deleted ")
}
if progress.FullAmount == 1 {
fmt.Print("1 file in ")
} else {
fmt.Print(strconv.FormatUint(progress.DoneAmount, 9))
fmt.Print(" files in ")
}
fullSpeed := float64(progress.FullSize) / elapsed.Seconds()
fmt.Print(ui.FormatSeconds(elapsed.Seconds()))
fmt.Print(" (")
fmt.Print(ui.FormatSize(fullSpeed, ui.SizeAutoUnit(fullSpeed)))
fmt.Print("/s).")
fmt.Println()
}