-
Notifications
You must be signed in to change notification settings - Fork 631
Expand file tree
/
Copy pathprogress.go
More file actions
40 lines (33 loc) · 1.06 KB
/
Copy pathprogress.go
File metadata and controls
40 lines (33 loc) · 1.06 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
package cameradar
import (
"strconv"
"strings"
)
const progressMessagePrefix = "\x00progress:"
// ProgressTotalMessage returns a progress control message that sets the total units for a step.
func ProgressTotalMessage(total int) string {
return progressMessagePrefix + "total=" + strconv.Itoa(total)
}
// ProgressTickMessage returns a progress control message that increments a step's progress by one unit.
func ProgressTickMessage() string {
return progressMessagePrefix + "tick"
}
// ParseProgressMessage parses a progress control message.
// It returns a kind of "total" or "tick" and an optional value.
func ParseProgressMessage(message string) (string, int, bool) {
if !strings.HasPrefix(message, progressMessagePrefix) {
return "", 0, false
}
payload := strings.TrimPrefix(message, progressMessagePrefix)
if payload == "tick" {
return "tick", 1, true
}
if valuePart, ok := strings.CutPrefix(payload, "total="); ok {
value, err := strconv.Atoi(valuePart)
if err != nil {
return "", 0, false
}
return "total", value, true
}
return "", 0, false
}