Skip to content

Commit 26bc84a

Browse files
committed
Improve multi-line log formatting during build watch.
1 parent 57c9440 commit 26bc84a

File tree

1 file changed

+37
-14
lines changed

1 file changed

+37
-14
lines changed

cmd/builds.go

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,24 +69,44 @@ var buildAbortCmd = &cobra.Command{
6969
var logsFollow = false
7070
var logsRaw = false
7171

72-
func formatLogEntry(logEntry *api.LogEntry) string {
72+
func formatLogEntry(logEntry *api.LogEntry, terminalWidth int) string {
7373
line := &strings.Builder{}
7474

75-
fmt.Fprintf(line, "│ %s │ ", color.HiBlackString(logEntry.Timestamp.Format(time.RFC3339)))
76-
75+
timestampPrefix := []rune(fmt.Sprintf("│ %s │ ", logEntry.Timestamp.Format(time.RFC3339)))
76+
colorWrapper := color.WhiteString
77+
contextPrefix := []rune{}
7778
switch logEntry.Source {
7879
case api.LogEntrySourceUnspecified:
7980
case api.LogEntrySourceWrapper:
8081
switch logEntry.Stage {
8182
case api.LogEntryStageUnspecified:
82-
line.WriteString(color.WhiteString("→ %s", logEntry.Content))
83+
colorWrapper = color.WhiteString
84+
contextPrefix = []rune("→ ")
8385
case api.LogEntryStageSetup:
84-
line.WriteString(color.GreenString("setup ↗ %s", logEntry.Content))
86+
colorWrapper = color.GreenString
87+
contextPrefix = []rune("setup ↗ ")
8588
case api.LogEntryStageTurndown:
86-
line.WriteString(color.YellowString("turndown ↘ %s", logEntry.Content))
89+
colorWrapper = color.YellowString
90+
contextPrefix = []rune("turndown ↘ ")
8791
}
8892
case api.LogEntrySourceProcess:
89-
line.WriteString(color.WhiteString("→ %s", logEntry.Content))
93+
colorWrapper = color.WhiteString
94+
contextPrefix = []rune("")
95+
}
96+
97+
line.WriteString(color.HiBlackString(string(timestampPrefix)))
98+
line.WriteString(colorWrapper(string(contextPrefix)))
99+
100+
contentRunes := []rune(logEntry.Content)
101+
runeBlockLen := max(1, terminalWidth-len(timestampPrefix)-len(contextPrefix))
102+
for i := 0; i <= len(contentRunes)/runeBlockLen; i++ {
103+
if i != 0 {
104+
line.WriteString(color.HiBlackString(string(timestampPrefix)))
105+
for j := 0; j < len(contextPrefix); j++ {
106+
line.WriteRune(' ')
107+
}
108+
}
109+
line.WriteString(colorWrapper(string(contentRunes[i*runeBlockLen : min((i+1)*runeBlockLen, len(contentRunes))])))
90110
}
91111
return line.String()
92112
}
@@ -117,13 +137,14 @@ var buildLogsCmd = &cobra.Command{
117137
return fmt.Errorf("no builds available")
118138
}
119139
// Sort builds by date
140+
width, _, _ := terminal.GetSize(0)
120141
sort.Slice(builds, func(i, j int) bool { return builds[i].CreatedAt.After(builds[j].CreatedAt) })
121142
latestBuildID := builds[0].ID
122143
consumer := func(le api.LogEntry) {
123144
if logsRaw {
124145
fmt.Println(le.Content)
125146
} else {
126-
fmt.Println(formatLogEntry(&le))
147+
fmt.Println(formatLogEntry(&le, width))
127148
}
128149
}
129150
if logsFollow {
@@ -135,7 +156,7 @@ var buildLogsCmd = &cobra.Command{
135156

136157
var buildWatchCmd = &cobra.Command{
137158
Use: "watch [prefix]",
138-
Short: "Watch a live build until its completion.",
159+
Short: "Watch a build until its completion.",
139160
Args: cobra.MaximumNArgs(1),
140161
Run: runAndHandle(func(cmd *cobra.Command, args []string) error {
141162
cfg, err := config.NewServiceConfigWithFallback(functionConfiguration, &buildService, globalConfiguration)
@@ -162,8 +183,8 @@ var buildWatchCmd = &cobra.Command{
162183
sort.Slice(builds, func(i, j int) bool { return builds[i].CreatedAt.After(builds[j].CreatedAt) })
163184
latestBuildID := builds[0].ID
164185

165-
_, rows, _ := terminal.GetSize(0)
166-
bar := progressbar.NewOptions(-1, progressbar.OptionEnableColorCodes(true), progressbar.OptionSpinnerType(3), progressbar.OptionSetElapsedTime(false), progressbar.OptionSetMaxDetailRow(rows-2))
186+
terminalWidth, terminalHeight, _ := terminal.GetSize(0)
187+
bar := progressbar.NewOptions(-1, progressbar.OptionEnableColorCodes(true), progressbar.OptionSpinnerType(3), progressbar.OptionSetElapsedTime(false), progressbar.OptionSetMaxDetailRow(terminalHeight-2), progressbar.OptionFullWidth())
167188
build, err := client.InspectBuild(cfg.Project(), cfg.Service(), latestBuildID)
168189
if err != nil {
169190
return err
@@ -183,7 +204,9 @@ var buildWatchCmd = &cobra.Command{
183204
case api.LogEntryStageTurndown:
184205
bar.Describe("Turning down build environment ...")
185206
}
186-
bar.AddDetail(formatLogEntry(&le))
207+
bar.AddDetail(formatLogEntry(&le, terminalWidth))
208+
fmt.Printf("\n\033[1A\033[K")
209+
bar.RenderBlank()
187210
})
188211

189212
build, err = client.InspectBuild(cfg.Project(), cfg.Service(), latestBuildID)
@@ -192,9 +215,9 @@ var buildWatchCmd = &cobra.Command{
192215
}
193216
switch build.Status {
194217
case "done":
195-
bar.Describe("Build has succeeded.")
218+
bar.Describe(color.GreenString("Build has succeeded."))
196219
case "failed":
197-
bar.Describe("Build has failed.")
220+
bar.Describe(color.RedString("Build has failed."))
198221
}
199222
bar.Finish()
200223
fmt.Println()

0 commit comments

Comments
 (0)