-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransient.go
More file actions
69 lines (61 loc) · 2.07 KB
/
Copy pathtransient.go
File metadata and controls
69 lines (61 loc) · 2.07 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
package console
import (
"errors"
"strings"
)
const clearTransientLine = "\r\x1b[2K"
// ErrTransientActive is returned when another live loader or progress display owns the transient line.
//
// Example: inspect the transient ownership error
//
// fmt.Println(console.ErrTransientActive)
// // console: another transient display is already active
var ErrTransientActive = errors.New("console: another transient display is already active")
// transientOwner renders one replaceable line while the console coordinates durable output.
type transientOwner interface {
renderTransient() string
}
// normalizeTransientMessage reduces live display labels to one balanced, safe physical line.
func normalizeTransientMessage(message string) string {
message = sanitizeLayoutText(message, false)
message = strings.Join(strings.Fields(message), " ")
return balanceANSILines([]string{message})[0]
}
// acquireTransient grants one owner exclusive access to the replaceable output line.
func (c *Console) acquireTransient(owner transientOwner) error {
c.transientMu.Lock()
defer c.transientMu.Unlock()
if c.active != nil && c.active != owner {
return ErrTransientActive
}
c.active = owner
return nil
}
// renderTransient redraws owner only while it still controls an otherwise complete output line.
func (c *Console) renderTransient(owner transientOwner) {
c.transientMu.Lock()
defer c.transientMu.Unlock()
if c.active != owner || c.partialLine {
return
}
c.outputMu.Lock()
_, _ = writeConsoleString(c.stdout, owner.renderTransient())
c.outputMu.Unlock()
}
// releaseTransient clears the replaceable line and relinquishes ownership after live work has stopped.
func (c *Console) releaseTransient(owner transientOwner, durableOutcome bool) {
c.transientMu.Lock()
defer c.transientMu.Unlock()
if c.active != owner {
return
}
c.outputMu.Lock()
if c.partialLine && durableOutcome && !c.promptActive {
_, _ = writeConsoleString(c.stdout, "\n")
c.partialLine = false
} else if !c.partialLine {
_, _ = writeConsoleString(c.stdout, clearTransientLine)
}
c.active = nil
c.outputMu.Unlock()
}