@@ -69,24 +69,44 @@ var buildAbortCmd = &cobra.Command{
69
69
var logsFollow = false
70
70
var logsRaw = false
71
71
72
- func formatLogEntry (logEntry * api.LogEntry ) string {
72
+ func formatLogEntry (logEntry * api.LogEntry , terminalWidth int ) string {
73
73
line := & strings.Builder {}
74
74
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 {}
77
78
switch logEntry .Source {
78
79
case api .LogEntrySourceUnspecified :
79
80
case api .LogEntrySourceWrapper :
80
81
switch logEntry .Stage {
81
82
case api .LogEntryStageUnspecified :
82
- line .WriteString (color .WhiteString ("→ %s" , logEntry .Content ))
83
+ colorWrapper = color .WhiteString
84
+ contextPrefix = []rune ("→ " )
83
85
case api .LogEntryStageSetup :
84
- line .WriteString (color .GreenString ("setup ↗ %s" , logEntry .Content ))
86
+ colorWrapper = color .GreenString
87
+ contextPrefix = []rune ("setup ↗ " )
85
88
case api .LogEntryStageTurndown :
86
- line .WriteString (color .YellowString ("turndown ↘ %s" , logEntry .Content ))
89
+ colorWrapper = color .YellowString
90
+ contextPrefix = []rune ("turndown ↘ " )
87
91
}
88
92
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 ))])))
90
110
}
91
111
return line .String ()
92
112
}
@@ -117,13 +137,14 @@ var buildLogsCmd = &cobra.Command{
117
137
return fmt .Errorf ("no builds available" )
118
138
}
119
139
// Sort builds by date
140
+ width , _ , _ := terminal .GetSize (0 )
120
141
sort .Slice (builds , func (i , j int ) bool { return builds [i ].CreatedAt .After (builds [j ].CreatedAt ) })
121
142
latestBuildID := builds [0 ].ID
122
143
consumer := func (le api.LogEntry ) {
123
144
if logsRaw {
124
145
fmt .Println (le .Content )
125
146
} else {
126
- fmt .Println (formatLogEntry (& le ))
147
+ fmt .Println (formatLogEntry (& le , width ))
127
148
}
128
149
}
129
150
if logsFollow {
@@ -135,7 +156,7 @@ var buildLogsCmd = &cobra.Command{
135
156
136
157
var buildWatchCmd = & cobra.Command {
137
158
Use : "watch [prefix]" ,
138
- Short : "Watch a live build until its completion." ,
159
+ Short : "Watch a build until its completion." ,
139
160
Args : cobra .MaximumNArgs (1 ),
140
161
Run : runAndHandle (func (cmd * cobra.Command , args []string ) error {
141
162
cfg , err := config .NewServiceConfigWithFallback (functionConfiguration , & buildService , globalConfiguration )
@@ -162,8 +183,8 @@ var buildWatchCmd = &cobra.Command{
162
183
sort .Slice (builds , func (i , j int ) bool { return builds [i ].CreatedAt .After (builds [j ].CreatedAt ) })
163
184
latestBuildID := builds [0 ].ID
164
185
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 ( ))
167
188
build , err := client .InspectBuild (cfg .Project (), cfg .Service (), latestBuildID )
168
189
if err != nil {
169
190
return err
@@ -183,7 +204,9 @@ var buildWatchCmd = &cobra.Command{
183
204
case api .LogEntryStageTurndown :
184
205
bar .Describe ("Turning down build environment ..." )
185
206
}
186
- bar .AddDetail (formatLogEntry (& le ))
207
+ bar .AddDetail (formatLogEntry (& le , terminalWidth ))
208
+ fmt .Printf ("\n \033 [1A\033 [K" )
209
+ bar .RenderBlank ()
187
210
})
188
211
189
212
build , err = client .InspectBuild (cfg .Project (), cfg .Service (), latestBuildID )
@@ -192,9 +215,9 @@ var buildWatchCmd = &cobra.Command{
192
215
}
193
216
switch build .Status {
194
217
case "done" :
195
- bar .Describe ("Build has succeeded." )
218
+ bar .Describe (color . GreenString ( "Build has succeeded." ) )
196
219
case "failed" :
197
- bar .Describe ("Build has failed." )
220
+ bar .Describe (color . RedString ( "Build has failed." ) )
198
221
}
199
222
bar .Finish ()
200
223
fmt .Println ()
0 commit comments