-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathconcurrent.go
82 lines (72 loc) · 2.13 KB
/
concurrent.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
package wlog
import "sync"
// ConcurrentUI is a wrapper for UI that makes the UI thread safe.
type ConcurrentUI struct {
UI UI
l sync.Mutex
}
// Log calls UI.Log to write.
// This is a thread safe function.
func (ui *ConcurrentUI) Log(message string) {
ui.l.Lock()
defer ui.l.Unlock()
ui.UI.Log(message)
}
// Output calls UI.Output to write.
// This is a thread safe function.
func (ui *ConcurrentUI) Output(message string) {
ui.l.Lock()
defer ui.l.Unlock()
ui.UI.Output(message)
}
// Success calls UI.Success to write.
// Useful when you want separate colors or prefixes.
// This is a thread safe function.
func (ui *ConcurrentUI) Success(message string) {
ui.l.Lock()
defer ui.l.Unlock()
ui.UI.Success(message)
}
// Info calls UI.Info to write.
// Useful when you want separate colors or prefixes.
// This is a thread safe function.
func (ui *ConcurrentUI) Info(message string) {
ui.l.Lock()
defer ui.l.Unlock()
ui.UI.Info(message)
}
// Error calls UI.Error to write.
// This is a thread safe function.
func (ui *ConcurrentUI) Error(message string) {
ui.l.Lock()
defer ui.l.Unlock()
ui.UI.Error(message)
}
// Warn calls UI.Warn to write.
// Useful when you want separate colors or prefixes.
// This is a thread safe function.
func (ui *ConcurrentUI) Warn(message string) {
ui.l.Lock()
defer ui.l.Unlock()
ui.UI.Warn(message)
}
// Running calls UI.Running to write.
// Useful when you want separate colors or prefixes.
// This is a thread safe function.
func (ui *ConcurrentUI) Running(message string) {
ui.l.Lock()
defer ui.l.Unlock()
ui.UI.Running(message)
}
// Ask will call UI.Ask with message then wait for UI.Ask to return a response and/or error.
// It will clean the response by removing any carriage returns and new lines that if finds.
//Then it will trim the message using the trim variable.
//Use and empty string to specify you do not want to trim.
// If a message is not used ("") then it will not prompt user before waiting on a response.
// This is a thread safe function.
func (ui *ConcurrentUI) Ask(message, trim string) (string, error) {
ui.l.Lock()
defer ui.l.Unlock()
res, err := ui.UI.Ask(message, trim)
return res, err
}