-
Notifications
You must be signed in to change notification settings - Fork 64
/
view.go
181 lines (151 loc) · 4.26 KB
/
view.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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2022, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package processtree
import (
"strconv"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/reflow/indent"
"kraftkit.sh/tui"
"kraftkit.sh/utils"
)
func (pt ProcessTree) View() string {
if pt.norender {
return ""
}
s := ""
finished := 0
// Update timers on all active items and their parents
_ = pt.traverseTreeAndCall(pt.tree, func(pti *ProcessTreeItem) error {
if pti.status == StatusSuccess ||
pti.status == StatusFailed ||
pti.status == StatusFailedChild {
finished++
}
return nil
})
for _, pti := range pt.tree {
s += pt.printItem(pti, 0)
}
if len(pt.verb) > 0 {
title := tui.TextTitle(
pt.verb + " " + utils.HumanizeDuration(pt.timer.Elapsed()) +
" (" + strconv.Itoa(finished) + "/" + strconv.Itoa(pt.total) + ")",
)
s = title + "\n" + s
}
if pt.norender {
s += " no render! \n"
}
if !pt.quitting {
s += tui.TextLightGray("ctrl+c to cancel\n")
}
return s
}
func (stm ProcessTree) printItem(pti *ProcessTreeItem, offset uint) string {
if pti.status == StatusSuccess && stm.hide || ((pti.status == StatusFailed || pti.status == StatusFailedChild) && stm.hideError) {
return ""
}
failed := 0
completed := 0
running := 0
// Determine the status of immediate children whilst we're printing
for _, child := range pti.children {
if child.status == StatusFailed ||
child.status == StatusFailedChild {
failed++
} else if child.status == StatusSuccess {
completed++
} else if child.status == StatusRunningChild ||
child.status == StatusRunning ||
child.status == StatusRunningButAChildHasFailed {
running++
}
}
if len(pti.children) > 0 {
if failed > 0 && running > 0 {
pti.status = StatusRunningButAChildHasFailed
} else if running > 0 {
pti.status = StatusRunningChild
} else if failed > 0 {
pti.status = StatusFailedChild
}
}
width := lipgloss.Width
textLeft := ""
switch pti.status {
case StatusSuccess:
textLeft += tui.TextWhiteBgGreen("[+]")
case StatusFailed, StatusFailedChild:
textLeft += tui.TextWhiteBgRed("<!>")
case StatusRunning, StatusRunningChild, StatusRunningButAChildHasFailed:
textLeft += tui.TextWhiteBgBlue("[" + pti.spinner.View() + "]")
default:
textLeft += "[ ]"
}
textLeft += " " + pti.textLeft
if pti.status == StatusRunning || pti.status == StatusRunningChild {
textLeft += pti.ellipsis
} else if pti.status == StatusSuccess {
textLeft += "... done!"
}
elapsed := utils.HumanizeDuration(pti.timer.Elapsed())
rightTimerWidth := width(elapsed)
if rightTimerWidth > stm.rightPad {
stm.rightPad = rightTimerWidth
}
textRight := ""
if len(pti.textRight) > 0 {
switch pti.status {
case StatusSuccess:
textRight += tui.TextWhiteBgGreen(pti.textRight)
case StatusFailed, StatusFailedChild:
textRight += tui.TextWhiteBgRed(pti.textRight)
case StatusRunning, StatusRunningChild, StatusRunningButAChildHasFailed:
textRight += tui.TextWhiteBgBlue(pti.textRight)
default:
textRight += pti.textRight
}
}
elapsed = "[" + elapsed + "]"
textRight += " " + tui.TextLightGray(indent.String(elapsed, uint(stm.rightPad-rightTimerWidth)))
left := lipgloss.NewStyle().
Width(stm.width - width(textRight) - int(offset*INDENTS)).
Height(1).
Render(textLeft)
right := lipgloss.NewStyle().
Width(width(textRight)).
Height(1).
Render(textRight)
s := lipgloss.JoinHorizontal(lipgloss.Top,
left,
right,
) + "\n"
// Print the logs for this item
truncate := 0
loglen := len(pti.logs) - LOGLEN
if pti.status == StatusFailed && !pti.hideError {
truncate = 0
} else if loglen > 0 {
truncate = loglen
}
if pti.status == StatusRunning || ((pti.status == StatusFailed || pti.status == StatusFailedChild) && !pti.hideError) {
for i, line := range pti.logs[truncate:] {
s += line
if i < len(pti.logs[truncate:]) {
s += "\n"
}
}
}
// Print the child processes
for _, child := range pti.children {
s += stm.printItem(child, offset+1)
}
// Do not indent the root node
if offset == 0 {
return s
}
// Since this method is recursive, indent by 1 factor
return indent.String(s, INDENTS)
}