-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecutor.go
117 lines (102 loc) · 2.58 KB
/
executor.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
package main
import (
"fmt"
"os"
"regexp"
"sync"
"time"
)
func execute(argSegments *[]string, argTimeout *time.Duration) {
var cmds []string
segmentOutput := struct {
sync.RWMutex
segments map[string]Segment
}{segments: make(map[string]Segment)}
wg := new(sync.WaitGroup)
re := regexp.MustCompile("\\s*=\\s*")
for _, argSegment := range *argSegments {
split := re.Split(argSegment, 2)
cmd := split[0]
hasParam := len(split) == 2
param := ""
if hasParam {
param = split[1]
if _, ok := segmentFuncsWithParam[cmd]; !ok {
println("unknown segment (parameter needed):", cmd)
os.Exit(1)
}
} else {
if _, ok := segmentFuncsWithoutParams[cmd]; !ok {
println("unknown segment:", cmd)
os.Exit(1)
}
}
cmds = append(cmds, cmd)
wg.Add(1)
go func(argCmd string, hasParam bool, argParam string) {
defer wg.Done()
chanSegment := make(chan Segment, 1)
go func() {
seg := makeSegment()
if hasParam {
segmentFuncsWithParam[argCmd](&seg, argParam)
} else {
segmentFuncsWithoutParams[argCmd](&seg)
}
chanSegment <- seg
}()
select {
case seg := <-chanSegment:
segmentOutput.Lock()
segmentOutput.segments[argCmd] = seg
segmentOutput.Unlock()
case <-time.After(*argTimeout):
seg := makeSegment()
seg.AddHeadline("Segment ", argCmd, " timed out")
segmentOutput.Lock()
segmentOutput.segments[argCmd] = seg
segmentOutput.Unlock()
}
}(cmd, hasParam, param)
}
wg.Wait()
// print headlines
for _, cmd := range cmds {
segment := segmentOutput.segments[cmd]
for _, headline := range segment.headLines {
fmt.Println(headline)
}
}
isFirstPrinted := false
lastNonEmptyColor := SegmentColor{-1, -1}
// print segments
for i, cmd := range cmds {
segment := segmentOutput.segments[cmd]
if segment.IsPrintable() {
if !isFirstPrinted {
isFirstPrinted = true
fmt.Print(segment.beginColor.fmt())
fmt.Print(" ")
} else {
sepColor := SegmentColor{fg: lastNonEmptyColor.bg, bg: segment.beginColor.bg}
fmt.Print(" ")
fmt.Print(sepColor.fmt())
fmt.Print(IconSegmentSep)
fmt.Print(" ")
}
}
if i == len(cmds)-1 { // last
currentNonEmptyColor := SegmentColor{-1, -1}
if segment.currentColor.IsEmpty() {
currentNonEmptyColor = lastNonEmptyColor
} else {
currentNonEmptyColor = segment.currentColor
}
segment.AddText(" ", resetColors(), fmtColorFg(currentNonEmptyColor.bg), IconSegmentSep, " ", resetColors())
}
fmt.Print(segment.text)
if !segment.currentColor.IsEmpty() {
lastNonEmptyColor = segment.currentColor
}
}
}