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 | ce52535 | 2019-06-08 18:58:00 -0700 | [diff] [blame] | 20 | |
| 21 | "android/soong/ui/status" |
| 22 | ) |
| 23 | |
| 24 | type dumbStatusOutput struct { |
Colin Cross | 097ed2a | 2019-06-08 21:48:58 -0700 | [diff] [blame^] | 25 | writer io.Writer |
Colin Cross | ce52535 | 2019-06-08 18:58:00 -0700 | [diff] [blame] | 26 | formatter formatter |
| 27 | } |
| 28 | |
| 29 | // NewDumbStatusOutput returns a StatusOutput that represents the |
| 30 | // current build status similarly to Ninja's built-in terminal |
| 31 | // output. |
Colin Cross | 097ed2a | 2019-06-08 21:48:58 -0700 | [diff] [blame^] | 32 | func NewDumbStatusOutput(w io.Writer, formatter formatter) status.StatusOutput { |
Colin Cross | ce52535 | 2019-06-08 18:58:00 -0700 | [diff] [blame] | 33 | return &dumbStatusOutput{ |
| 34 | writer: w, |
| 35 | formatter: formatter, |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func (s *dumbStatusOutput) Message(level status.MsgLevel, message string) { |
| 40 | if level >= status.StatusLvl { |
| 41 | fmt.Fprintln(s.writer, s.formatter.message(level, message)) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func (s *dumbStatusOutput) StartAction(action *status.Action, counts status.Counts) { |
| 46 | } |
| 47 | |
| 48 | func (s *dumbStatusOutput) FinishAction(result status.ActionResult, counts status.Counts) { |
| 49 | str := result.Description |
| 50 | if str == "" { |
| 51 | str = result.Command |
| 52 | } |
| 53 | |
| 54 | progress := s.formatter.progress(counts) + str |
| 55 | |
| 56 | output := s.formatter.result(result) |
| 57 | output = string(stripAnsiEscapes([]byte(output))) |
| 58 | |
| 59 | if output != "" { |
| 60 | fmt.Fprint(s.writer, progress, "\n", output) |
| 61 | } else { |
| 62 | fmt.Fprintln(s.writer, progress) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | func (s *dumbStatusOutput) Flush() {} |