Colin Cross | ce52535 | 2019-06-08 18:58:00 -0700 | [diff] [blame] | 1 | // Copyright 2019 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package terminal |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
Colin Cross | 097ed2a | 2019-06-08 21:48:58 -0700 | [diff] [blame] | 19 | "io" |
Colin Cross | 4355ee6 | 2019-06-11 23:01:36 -0700 | [diff] [blame] | 20 | "os" |
| 21 | "os/signal" |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 22 | "strconv" |
Colin Cross | ce52535 | 2019-06-08 18:58:00 -0700 | [diff] [blame] | 23 | "strings" |
| 24 | "sync" |
Colin Cross | 4355ee6 | 2019-06-11 23:01:36 -0700 | [diff] [blame] | 25 | "syscall" |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 26 | "time" |
Colin Cross | ce52535 | 2019-06-08 18:58:00 -0700 | [diff] [blame] | 27 | |
| 28 | "android/soong/ui/status" |
| 29 | ) |
| 30 | |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 31 | const tableHeightEnVar = "SOONG_UI_TABLE_HEIGHT" |
| 32 | |
| 33 | type actionTableEntry struct { |
Jeongik Cha | adbdbc3 | 2023-11-30 08:58:26 +0900 | [diff] [blame] | 34 | action *status.Action |
| 35 | startTime time.Time |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Colin Cross | ce52535 | 2019-06-08 18:58:00 -0700 | [diff] [blame] | 38 | type smartStatusOutput struct { |
Colin Cross | 097ed2a | 2019-06-08 21:48:58 -0700 | [diff] [blame] | 39 | writer io.Writer |
Colin Cross | ce52535 | 2019-06-08 18:58:00 -0700 | [diff] [blame] | 40 | formatter formatter |
| 41 | |
| 42 | lock sync.Mutex |
| 43 | |
| 44 | haveBlankLine bool |
Colin Cross | 4355ee6 | 2019-06-11 23:01:36 -0700 | [diff] [blame] | 45 | |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 46 | tableMode bool |
| 47 | tableHeight int |
| 48 | requestedTableHeight int |
| 49 | termWidth, termHeight int |
| 50 | |
| 51 | runningActions []actionTableEntry |
| 52 | ticker *time.Ticker |
| 53 | done chan bool |
Colin Cross | 4355ee6 | 2019-06-11 23:01:36 -0700 | [diff] [blame] | 54 | sigwinch chan os.Signal |
| 55 | sigwinchHandled chan bool |
Joe Onorato | eadb0fb | 2023-06-24 15:03:28 -0700 | [diff] [blame] | 56 | |
| 57 | // Once there is a failure, we stop printing command output so the error |
| 58 | // is easier to find |
| 59 | haveFailures bool |
| 60 | // If we are dropping errors, then at the end, we report a message to go |
| 61 | // look in the verbose log if you want that command output. |
| 62 | postFailureActionCount int |
Colin Cross | ce52535 | 2019-06-08 18:58:00 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // NewSmartStatusOutput returns a StatusOutput that represents the |
| 66 | // current build status similarly to Ninja's built-in terminal |
| 67 | // output. |
Colin Cross | 097ed2a | 2019-06-08 21:48:58 -0700 | [diff] [blame] | 68 | func NewSmartStatusOutput(w io.Writer, formatter formatter) status.StatusOutput { |
Colin Cross | 4355ee6 | 2019-06-11 23:01:36 -0700 | [diff] [blame] | 69 | s := &smartStatusOutput{ |
Colin Cross | ce52535 | 2019-06-08 18:58:00 -0700 | [diff] [blame] | 70 | writer: w, |
| 71 | formatter: formatter, |
| 72 | |
| 73 | haveBlankLine: true, |
Colin Cross | 4355ee6 | 2019-06-11 23:01:36 -0700 | [diff] [blame] | 74 | |
Dan Willemsen | 1eee154 | 2019-07-30 13:44:03 -0700 | [diff] [blame] | 75 | tableMode: true, |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 76 | |
| 77 | done: make(chan bool), |
Colin Cross | 4355ee6 | 2019-06-11 23:01:36 -0700 | [diff] [blame] | 78 | sigwinch: make(chan os.Signal), |
Colin Cross | ce52535 | 2019-06-08 18:58:00 -0700 | [diff] [blame] | 79 | } |
Colin Cross | 4355ee6 | 2019-06-11 23:01:36 -0700 | [diff] [blame] | 80 | |
Dan Willemsen | 1eee154 | 2019-07-30 13:44:03 -0700 | [diff] [blame] | 81 | if env, ok := os.LookupEnv(tableHeightEnVar); ok { |
| 82 | h, _ := strconv.Atoi(env) |
| 83 | s.tableMode = h > 0 |
| 84 | s.requestedTableHeight = h |
| 85 | } |
| 86 | |
Colin Cross | 72a28a6 | 2021-02-10 13:12:41 -0800 | [diff] [blame] | 87 | if w, h, ok := termSize(s.writer); ok { |
| 88 | s.termWidth, s.termHeight = w, h |
| 89 | s.computeTableHeight() |
| 90 | } else { |
| 91 | s.tableMode = false |
| 92 | } |
Colin Cross | 4355ee6 | 2019-06-11 23:01:36 -0700 | [diff] [blame] | 93 | |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 94 | if s.tableMode { |
| 95 | // Add empty lines at the bottom of the screen to scroll back the existing history |
| 96 | // and make room for the action table. |
| 97 | // TODO: read the cursor position to see if the empty lines are necessary? |
| 98 | for i := 0; i < s.tableHeight; i++ { |
| 99 | fmt.Fprintln(w) |
| 100 | } |
| 101 | |
| 102 | // Hide the cursor to prevent seeing it bouncing around |
| 103 | fmt.Fprintf(s.writer, ansi.hideCursor()) |
| 104 | |
| 105 | // Configure the empty action table |
| 106 | s.actionTable() |
| 107 | |
| 108 | // Start a tick to update the action table periodically |
| 109 | s.startActionTableTick() |
| 110 | } |
| 111 | |
Colin Cross | 4355ee6 | 2019-06-11 23:01:36 -0700 | [diff] [blame] | 112 | s.startSigwinch() |
| 113 | |
| 114 | return s |
Colin Cross | ce52535 | 2019-06-08 18:58:00 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | func (s *smartStatusOutput) Message(level status.MsgLevel, message string) { |
| 118 | if level < status.StatusLvl { |
| 119 | return |
| 120 | } |
| 121 | |
| 122 | str := s.formatter.message(level, message) |
| 123 | |
| 124 | s.lock.Lock() |
| 125 | defer s.lock.Unlock() |
| 126 | |
| 127 | if level > status.StatusLvl { |
| 128 | s.print(str) |
| 129 | } else { |
| 130 | s.statusLine(str) |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | func (s *smartStatusOutput) StartAction(action *status.Action, counts status.Counts) { |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 135 | startTime := time.Now() |
| 136 | |
Colin Cross | ce52535 | 2019-06-08 18:58:00 -0700 | [diff] [blame] | 137 | str := action.Description |
| 138 | if str == "" { |
| 139 | str = action.Command |
| 140 | } |
| 141 | |
| 142 | progress := s.formatter.progress(counts) |
| 143 | |
| 144 | s.lock.Lock() |
| 145 | defer s.lock.Unlock() |
| 146 | |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 147 | s.runningActions = append(s.runningActions, actionTableEntry{ |
| 148 | action: action, |
| 149 | startTime: startTime, |
| 150 | }) |
| 151 | |
Colin Cross | ce52535 | 2019-06-08 18:58:00 -0700 | [diff] [blame] | 152 | s.statusLine(progress + str) |
| 153 | } |
| 154 | |
| 155 | func (s *smartStatusOutput) FinishAction(result status.ActionResult, counts status.Counts) { |
| 156 | str := result.Description |
| 157 | if str == "" { |
| 158 | str = result.Command |
| 159 | } |
| 160 | |
| 161 | progress := s.formatter.progress(counts) + str |
| 162 | |
| 163 | output := s.formatter.result(result) |
| 164 | |
| 165 | s.lock.Lock() |
| 166 | defer s.lock.Unlock() |
| 167 | |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 168 | for i, runningAction := range s.runningActions { |
| 169 | if runningAction.action == result.Action { |
| 170 | s.runningActions = append(s.runningActions[:i], s.runningActions[i+1:]...) |
| 171 | break |
| 172 | } |
| 173 | } |
| 174 | |
Joe Onorato | eadb0fb | 2023-06-24 15:03:28 -0700 | [diff] [blame] | 175 | s.statusLine(progress) |
| 176 | |
| 177 | // Stop printing when there are failures, but don't skip actions that also have their own errors. |
Colin Cross | ce52535 | 2019-06-08 18:58:00 -0700 | [diff] [blame] | 178 | if output != "" { |
Joe Onorato | eadb0fb | 2023-06-24 15:03:28 -0700 | [diff] [blame] | 179 | if !s.haveFailures || result.Error != nil { |
| 180 | s.requestLine() |
| 181 | s.print(output) |
| 182 | } else { |
| 183 | s.postFailureActionCount++ |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | if result.Error != nil { |
| 188 | s.haveFailures = true |
Colin Cross | ce52535 | 2019-06-08 18:58:00 -0700 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
| 192 | func (s *smartStatusOutput) Flush() { |
Colin Cross | 8cc1991 | 2019-10-29 15:22:04 -0700 | [diff] [blame] | 193 | if s.tableMode { |
| 194 | // Stop the action table tick outside of the lock to avoid lock ordering issues between s.done and |
| 195 | // s.lock, the goroutine in startActionTableTick can get blocked on the lock and be unable to read |
| 196 | // from the channel. |
| 197 | s.stopActionTableTick() |
| 198 | } |
| 199 | |
Colin Cross | ce52535 | 2019-06-08 18:58:00 -0700 | [diff] [blame] | 200 | s.lock.Lock() |
| 201 | defer s.lock.Unlock() |
| 202 | |
Colin Cross | 4355ee6 | 2019-06-11 23:01:36 -0700 | [diff] [blame] | 203 | s.stopSigwinch() |
| 204 | |
Joe Onorato | eadb0fb | 2023-06-24 15:03:28 -0700 | [diff] [blame] | 205 | if s.postFailureActionCount > 0 { |
| 206 | s.requestLine() |
| 207 | if s.postFailureActionCount == 1 { |
| 208 | s.print(fmt.Sprintf("There was 1 action that completed after the action that failed. See verbose.log.gz for its output.")) |
| 209 | } else { |
| 210 | s.print(fmt.Sprintf("There were %d actions that completed after the action that failed. See verbose.log.gz for their output.", s.postFailureActionCount)) |
| 211 | } |
| 212 | } |
| 213 | |
Colin Cross | ce52535 | 2019-06-08 18:58:00 -0700 | [diff] [blame] | 214 | s.requestLine() |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 215 | |
| 216 | s.runningActions = nil |
| 217 | |
| 218 | if s.tableMode { |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 219 | // Update the table after clearing runningActions to clear it |
| 220 | s.actionTable() |
| 221 | |
| 222 | // Reset the scrolling region to the whole terminal |
| 223 | fmt.Fprintf(s.writer, ansi.resetScrollingMargins()) |
| 224 | _, height, _ := termSize(s.writer) |
| 225 | // Move the cursor to the top of the now-blank, previously non-scrolling region |
Colin Cross | bf8f57e | 2019-09-20 15:00:22 -0700 | [diff] [blame] | 226 | fmt.Fprintf(s.writer, ansi.setCursor(height-s.tableHeight, 1)) |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 227 | // Turn the cursor back on |
| 228 | fmt.Fprintf(s.writer, ansi.showCursor()) |
| 229 | } |
Colin Cross | ce52535 | 2019-06-08 18:58:00 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Colin Cross | e0df1a3 | 2019-06-09 19:40:08 -0700 | [diff] [blame] | 232 | func (s *smartStatusOutput) Write(p []byte) (int, error) { |
| 233 | s.lock.Lock() |
| 234 | defer s.lock.Unlock() |
| 235 | s.print(string(p)) |
| 236 | return len(p), nil |
| 237 | } |
| 238 | |
Colin Cross | ce52535 | 2019-06-08 18:58:00 -0700 | [diff] [blame] | 239 | func (s *smartStatusOutput) requestLine() { |
| 240 | if !s.haveBlankLine { |
| 241 | fmt.Fprintln(s.writer) |
| 242 | s.haveBlankLine = true |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | func (s *smartStatusOutput) print(str string) { |
| 247 | if !s.haveBlankLine { |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 248 | fmt.Fprint(s.writer, "\r", ansi.clearToEndOfLine()) |
Colin Cross | ce52535 | 2019-06-08 18:58:00 -0700 | [diff] [blame] | 249 | s.haveBlankLine = true |
| 250 | } |
| 251 | fmt.Fprint(s.writer, str) |
| 252 | if len(str) == 0 || str[len(str)-1] != '\n' { |
| 253 | fmt.Fprint(s.writer, "\n") |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | func (s *smartStatusOutput) statusLine(str string) { |
| 258 | idx := strings.IndexRune(str, '\n') |
| 259 | if idx != -1 { |
| 260 | str = str[0:idx] |
| 261 | } |
| 262 | |
| 263 | // Limit line width to the terminal width, otherwise we'll wrap onto |
| 264 | // another line and we won't delete the previous line. |
Colin Cross | 5137de0 | 2019-06-20 15:22:50 -0700 | [diff] [blame] | 265 | str = elide(str, s.termWidth) |
Colin Cross | ce52535 | 2019-06-08 18:58:00 -0700 | [diff] [blame] | 266 | |
Colin Cross | 00bdfd8 | 2019-06-11 11:16:23 -0700 | [diff] [blame] | 267 | // Move to the beginning on the line, turn on bold, print the output, |
| 268 | // turn off bold, then clear the rest of the line. |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 269 | start := "\r" + ansi.bold() |
| 270 | end := ansi.regular() + ansi.clearToEndOfLine() |
Colin Cross | 00bdfd8 | 2019-06-11 11:16:23 -0700 | [diff] [blame] | 271 | fmt.Fprint(s.writer, start, str, end) |
Colin Cross | ce52535 | 2019-06-08 18:58:00 -0700 | [diff] [blame] | 272 | s.haveBlankLine = false |
| 273 | } |
Colin Cross | 4355ee6 | 2019-06-11 23:01:36 -0700 | [diff] [blame] | 274 | |
Colin Cross | 5137de0 | 2019-06-20 15:22:50 -0700 | [diff] [blame] | 275 | func elide(str string, width int) string { |
| 276 | if width > 0 && len(str) > width { |
Colin Cross | 4355ee6 | 2019-06-11 23:01:36 -0700 | [diff] [blame] | 277 | // TODO: Just do a max. Ninja elides the middle, but that's |
| 278 | // more complicated and these lines aren't that important. |
Colin Cross | 5137de0 | 2019-06-20 15:22:50 -0700 | [diff] [blame] | 279 | str = str[:width] |
Colin Cross | 4355ee6 | 2019-06-11 23:01:36 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | return str |
| 283 | } |
| 284 | |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 285 | func (s *smartStatusOutput) startActionTableTick() { |
| 286 | s.ticker = time.NewTicker(time.Second) |
| 287 | go func() { |
| 288 | for { |
| 289 | select { |
| 290 | case <-s.ticker.C: |
| 291 | s.lock.Lock() |
| 292 | s.actionTable() |
| 293 | s.lock.Unlock() |
| 294 | case <-s.done: |
| 295 | return |
| 296 | } |
| 297 | } |
| 298 | }() |
| 299 | } |
| 300 | |
| 301 | func (s *smartStatusOutput) stopActionTableTick() { |
| 302 | s.ticker.Stop() |
| 303 | s.done <- true |
| 304 | } |
| 305 | |
Colin Cross | 4355ee6 | 2019-06-11 23:01:36 -0700 | [diff] [blame] | 306 | func (s *smartStatusOutput) startSigwinch() { |
| 307 | signal.Notify(s.sigwinch, syscall.SIGWINCH) |
| 308 | go func() { |
| 309 | for _ = range s.sigwinch { |
| 310 | s.lock.Lock() |
| 311 | s.updateTermSize() |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 312 | if s.tableMode { |
| 313 | s.actionTable() |
| 314 | } |
Colin Cross | 4355ee6 | 2019-06-11 23:01:36 -0700 | [diff] [blame] | 315 | s.lock.Unlock() |
| 316 | if s.sigwinchHandled != nil { |
| 317 | s.sigwinchHandled <- true |
| 318 | } |
| 319 | } |
| 320 | }() |
| 321 | } |
| 322 | |
| 323 | func (s *smartStatusOutput) stopSigwinch() { |
| 324 | signal.Stop(s.sigwinch) |
| 325 | close(s.sigwinch) |
| 326 | } |
| 327 | |
Colin Cross | 72a28a6 | 2021-02-10 13:12:41 -0800 | [diff] [blame] | 328 | // computeTableHeight recomputes s.tableHeight based on s.termHeight and s.requestedTableHeight. |
| 329 | func (s *smartStatusOutput) computeTableHeight() { |
| 330 | tableHeight := s.requestedTableHeight |
| 331 | if tableHeight == 0 { |
| 332 | tableHeight = s.termHeight / 4 |
| 333 | if tableHeight < 1 { |
| 334 | tableHeight = 1 |
| 335 | } else if tableHeight > 10 { |
| 336 | tableHeight = 10 |
| 337 | } |
| 338 | } |
| 339 | if tableHeight > s.termHeight-1 { |
| 340 | tableHeight = s.termHeight - 1 |
| 341 | } |
| 342 | s.tableHeight = tableHeight |
| 343 | } |
| 344 | |
| 345 | // updateTermSize recomputes the table height after a SIGWINCH and pans any existing text if |
| 346 | // necessary. |
Colin Cross | 4355ee6 | 2019-06-11 23:01:36 -0700 | [diff] [blame] | 347 | func (s *smartStatusOutput) updateTermSize() { |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 348 | if w, h, ok := termSize(s.writer); ok { |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 349 | oldScrollingHeight := s.termHeight - s.tableHeight |
| 350 | |
| 351 | s.termWidth, s.termHeight = w, h |
| 352 | |
| 353 | if s.tableMode { |
Colin Cross | 72a28a6 | 2021-02-10 13:12:41 -0800 | [diff] [blame] | 354 | s.computeTableHeight() |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 355 | |
| 356 | scrollingHeight := s.termHeight - s.tableHeight |
| 357 | |
Colin Cross | 72a28a6 | 2021-02-10 13:12:41 -0800 | [diff] [blame] | 358 | // If the scrolling region has changed, attempt to pan the existing text so that it is |
| 359 | // not overwritten by the table. |
| 360 | if scrollingHeight < oldScrollingHeight { |
| 361 | pan := oldScrollingHeight - scrollingHeight |
| 362 | if pan > s.tableHeight { |
| 363 | pan = s.tableHeight |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 364 | } |
Colin Cross | 72a28a6 | 2021-02-10 13:12:41 -0800 | [diff] [blame] | 365 | fmt.Fprint(s.writer, ansi.panDown(pan)) |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 366 | } |
| 367 | } |
Colin Cross | 4355ee6 | 2019-06-11 23:01:36 -0700 | [diff] [blame] | 368 | } |
| 369 | } |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 370 | |
| 371 | func (s *smartStatusOutput) actionTable() { |
| 372 | scrollingHeight := s.termHeight - s.tableHeight |
| 373 | |
| 374 | // Update the scrolling region in case the height of the terminal changed |
Colin Cross | f0b987e | 2019-09-20 15:01:51 -0700 | [diff] [blame] | 375 | |
Colin Cross | bf8f57e | 2019-09-20 15:00:22 -0700 | [diff] [blame] | 376 | fmt.Fprint(s.writer, ansi.setScrollingMargins(1, scrollingHeight)) |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 377 | |
| 378 | // Write as many status lines as fit in the table |
Colin Cross | f0b987e | 2019-09-20 15:01:51 -0700 | [diff] [blame] | 379 | for tableLine := 0; tableLine < s.tableHeight; tableLine++ { |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 380 | if tableLine >= s.tableHeight { |
| 381 | break |
| 382 | } |
Colin Cross | f0b987e | 2019-09-20 15:01:51 -0700 | [diff] [blame] | 383 | // Move the cursor to the correct line of the non-scrolling region |
| 384 | fmt.Fprint(s.writer, ansi.setCursor(scrollingHeight+1+tableLine, 1)) |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 385 | |
Colin Cross | f0b987e | 2019-09-20 15:01:51 -0700 | [diff] [blame] | 386 | if tableLine < len(s.runningActions) { |
| 387 | runningAction := s.runningActions[tableLine] |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 388 | |
Colin Cross | f0b987e | 2019-09-20 15:01:51 -0700 | [diff] [blame] | 389 | seconds := int(time.Since(runningAction.startTime).Round(time.Second).Seconds()) |
| 390 | |
| 391 | desc := runningAction.action.Description |
| 392 | if desc == "" { |
| 393 | desc = runningAction.action.Command |
| 394 | } |
| 395 | |
| 396 | color := "" |
| 397 | if seconds >= 60 { |
| 398 | color = ansi.red() + ansi.bold() |
| 399 | } else if seconds >= 30 { |
| 400 | color = ansi.yellow() + ansi.bold() |
| 401 | } |
| 402 | |
| 403 | durationStr := fmt.Sprintf(" %2d:%02d ", seconds/60, seconds%60) |
| 404 | desc = elide(desc, s.termWidth-len(durationStr)) |
| 405 | durationStr = color + durationStr + ansi.regular() |
| 406 | fmt.Fprint(s.writer, durationStr, desc) |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 407 | } |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 408 | fmt.Fprint(s.writer, ansi.clearToEndOfLine()) |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | // Move the cursor back to the last line of the scrolling region |
Colin Cross | bf8f57e | 2019-09-20 15:00:22 -0700 | [diff] [blame] | 412 | fmt.Fprint(s.writer, ansi.setCursor(scrollingHeight, 1)) |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | var ansi = ansiImpl{} |
| 416 | |
| 417 | type ansiImpl struct{} |
| 418 | |
| 419 | func (ansiImpl) clearToEndOfLine() string { |
| 420 | return "\x1b[K" |
| 421 | } |
| 422 | |
| 423 | func (ansiImpl) setCursor(row, column int) string { |
| 424 | // Direct cursor address |
| 425 | return fmt.Sprintf("\x1b[%d;%dH", row, column) |
| 426 | } |
| 427 | |
| 428 | func (ansiImpl) setScrollingMargins(top, bottom int) string { |
| 429 | // Set Top and Bottom Margins DECSTBM |
| 430 | return fmt.Sprintf("\x1b[%d;%dr", top, bottom) |
| 431 | } |
| 432 | |
| 433 | func (ansiImpl) resetScrollingMargins() string { |
| 434 | // Set Top and Bottom Margins DECSTBM |
| 435 | return fmt.Sprintf("\x1b[r") |
| 436 | } |
| 437 | |
Colin Cross | 5137de0 | 2019-06-20 15:22:50 -0700 | [diff] [blame] | 438 | func (ansiImpl) red() string { |
| 439 | return "\x1b[31m" |
| 440 | } |
| 441 | |
| 442 | func (ansiImpl) yellow() string { |
| 443 | return "\x1b[33m" |
| 444 | } |
| 445 | |
Colin Cross | 3dac80e | 2019-06-11 11:19:06 -0700 | [diff] [blame] | 446 | func (ansiImpl) bold() string { |
| 447 | return "\x1b[1m" |
| 448 | } |
| 449 | |
| 450 | func (ansiImpl) regular() string { |
| 451 | return "\x1b[0m" |
| 452 | } |
| 453 | |
| 454 | func (ansiImpl) showCursor() string { |
| 455 | return "\x1b[?25h" |
| 456 | } |
| 457 | |
| 458 | func (ansiImpl) hideCursor() string { |
| 459 | return "\x1b[?25l" |
| 460 | } |
| 461 | |
| 462 | func (ansiImpl) panDown(lines int) string { |
| 463 | return fmt.Sprintf("\x1b[%dS", lines) |
| 464 | } |
| 465 | |
| 466 | func (ansiImpl) panUp(lines int) string { |
| 467 | return fmt.Sprintf("\x1b[%dT", lines) |
| 468 | } |