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