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 | |
Patrice Arruda | f445ba1 | 2020-07-28 17:49:01 +0000 | [diff] [blame] | 24 | type simpleStatusOutput struct { |
Sasha Smundak | 2cec8df | 2022-05-06 16:04:44 -0700 | [diff] [blame] | 25 | writer io.Writer |
| 26 | formatter formatter |
| 27 | keepANSI bool |
Colin Cross | ce52535 | 2019-06-08 18:58:00 -0700 | [diff] [blame] | 28 | } |
| 29 | |
Patrice Arruda | f445ba1 | 2020-07-28 17:49:01 +0000 | [diff] [blame] | 30 | // NewSimpleStatusOutput returns a StatusOutput that represents the |
Colin Cross | ce52535 | 2019-06-08 18:58:00 -0700 | [diff] [blame] | 31 | // current build status similarly to Ninja's built-in terminal |
| 32 | // output. |
Sasha Smundak | 2cec8df | 2022-05-06 16:04:44 -0700 | [diff] [blame] | 33 | func NewSimpleStatusOutput(w io.Writer, formatter formatter, keepANSI bool) status.StatusOutput { |
Patrice Arruda | f445ba1 | 2020-07-28 17:49:01 +0000 | [diff] [blame] | 34 | return &simpleStatusOutput{ |
Sasha Smundak | 2cec8df | 2022-05-06 16:04:44 -0700 | [diff] [blame] | 35 | writer: w, |
| 36 | formatter: formatter, |
| 37 | keepANSI: keepANSI, |
Colin Cross | ce52535 | 2019-06-08 18:58:00 -0700 | [diff] [blame] | 38 | } |
| 39 | } |
| 40 | |
Patrice Arruda | f445ba1 | 2020-07-28 17:49:01 +0000 | [diff] [blame] | 41 | func (s *simpleStatusOutput) Message(level status.MsgLevel, message string) { |
Sasha Smundak | 2cec8df | 2022-05-06 16:04:44 -0700 | [diff] [blame] | 42 | if level >= status.StatusLvl { |
Dan Willemsen | e2cdecf | 2022-06-08 20:32:07 -0700 | [diff] [blame] | 43 | output := s.formatter.message(level, message) |
| 44 | if !s.keepANSI { |
| 45 | output = string(stripAnsiEscapes([]byte(output))) |
| 46 | } |
| 47 | fmt.Fprintln(s.writer, output) |
Colin Cross | ce52535 | 2019-06-08 18:58:00 -0700 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
Sasha Smundak | 2cec8df | 2022-05-06 16:04:44 -0700 | [diff] [blame] | 51 | func (s *simpleStatusOutput) StartAction(action *status.Action, counts status.Counts) { |
Colin Cross | ce52535 | 2019-06-08 18:58:00 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Patrice Arruda | f445ba1 | 2020-07-28 17:49:01 +0000 | [diff] [blame] | 54 | func (s *simpleStatusOutput) FinishAction(result status.ActionResult, counts status.Counts) { |
Colin Cross | ce52535 | 2019-06-08 18:58:00 -0700 | [diff] [blame] | 55 | str := result.Description |
| 56 | if str == "" { |
| 57 | str = result.Command |
| 58 | } |
| 59 | |
| 60 | progress := s.formatter.progress(counts) + str |
| 61 | |
| 62 | output := s.formatter.result(result) |
Colin Cross | 3c0fe0e | 2021-02-10 13:11:18 -0800 | [diff] [blame] | 63 | if !s.keepANSI { |
| 64 | output = string(stripAnsiEscapes([]byte(output))) |
| 65 | } |
Colin Cross | ce52535 | 2019-06-08 18:58:00 -0700 | [diff] [blame] | 66 | |
| 67 | if output != "" { |
| 68 | fmt.Fprint(s.writer, progress, "\n", output) |
| 69 | } else { |
| 70 | fmt.Fprintln(s.writer, progress) |
| 71 | } |
| 72 | } |
| 73 | |
Patrice Arruda | f445ba1 | 2020-07-28 17:49:01 +0000 | [diff] [blame] | 74 | func (s *simpleStatusOutput) Flush() {} |
Colin Cross | e0df1a3 | 2019-06-09 19:40:08 -0700 | [diff] [blame] | 75 | |
Patrice Arruda | f445ba1 | 2020-07-28 17:49:01 +0000 | [diff] [blame] | 76 | func (s *simpleStatusOutput) Write(p []byte) (int, error) { |
Colin Cross | e0df1a3 | 2019-06-09 19:40:08 -0700 | [diff] [blame] | 77 | fmt.Fprint(s.writer, string(p)) |
| 78 | return len(p), nil |
| 79 | } |