-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathplain_format.go
More file actions
112 lines (105 loc) · 2.61 KB
/
plain_format.go
File metadata and controls
112 lines (105 loc) · 2.61 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
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
package output
import (
"fmt"
"strings"
)
// FormatEventLine converts an output event into a single display line.
func FormatEventLine(event any) (string, bool) {
switch e := event.(type) {
case MessageEvent:
return formatMessageEvent(e), true
case SpinnerEvent:
if e.Active {
return e.Text + "...", true
}
return "", false
case ErrorEvent:
return formatErrorEvent(e), true
case ContainerStatusEvent:
return formatStatusLine(e), true
case ProgressEvent:
return formatProgressLine(e)
case UserInputRequestEvent:
return formatUserInputRequest(e), true
case ContainerLogLineEvent:
return e.Line, true
default:
return "", false
}
}
func formatStatusLine(e ContainerStatusEvent) string {
switch e.Phase {
case "pulling":
return fmt.Sprintf("Pulling %s...", e.Container)
case "starting":
return fmt.Sprintf("Starting %s...", e.Container)
case "waiting":
return fmt.Sprintf("Waiting for %s to be ready...", e.Container)
case "ready":
if e.Detail != "" {
return fmt.Sprintf("%s ready (%s)", e.Container, e.Detail)
}
return fmt.Sprintf("%s ready", e.Container)
default:
if e.Detail != "" {
return fmt.Sprintf("%s: %s (%s)", e.Container, e.Phase, e.Detail)
}
return fmt.Sprintf("%s: %s", e.Container, e.Phase)
}
}
func formatProgressLine(e ProgressEvent) (string, bool) {
if e.Total > 0 {
pct := float64(e.Current) / float64(e.Total) * 100
return fmt.Sprintf(" %s: %s %.1f%%", e.LayerID, e.Status, pct), true
}
if e.Status != "" {
return fmt.Sprintf(" %s: %s", e.LayerID, e.Status), true
}
return "", false
}
func formatUserInputRequest(e UserInputRequestEvent) string {
switch len(e.Options) {
case 0:
return e.Prompt
case 1:
return fmt.Sprintf("%s (%s)", e.Prompt, e.Options[0].Label)
default:
labels := make([]string, len(e.Options))
for i, opt := range e.Options {
labels[i] = opt.Label
}
return fmt.Sprintf("%s [%s]", e.Prompt, strings.Join(labels, "/"))
}
}
func formatMessageEvent(e MessageEvent) string {
switch e.Severity {
case SeveritySuccess:
return "> Success: " + e.Text
case SeverityNote:
return "> Note: " + e.Text
case SeverityWarning:
return "> Warning: " + e.Text
default:
return e.Text
}
}
func formatErrorEvent(e ErrorEvent) string {
var sb strings.Builder
sb.WriteString("Error: ")
sb.WriteString(e.Title)
if e.Summary != "" {
sb.WriteString("\n ")
sb.WriteString(e.Summary)
}
if e.Detail != "" {
sb.WriteString("\n ")
sb.WriteString(e.Detail)
}
for _, action := range e.Actions {
sb.WriteString("\n → ")
sb.WriteString(action.Label)
sb.WriteString(" ")
sb.WriteString(action.Value)
}
return sb.String()
}