blob: 936b275a1217c1718219d29d8d0c22d9053adcce [file] [log] [blame]
Colin Crossce525352019-06-08 18:58:00 -07001// 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
15package terminal
16
17import (
18 "fmt"
Colin Cross097ed2a2019-06-08 21:48:58 -070019 "io"
Colin Crossce525352019-06-08 18:58:00 -070020
21 "android/soong/ui/status"
22)
23
Patrice Arrudaf445ba12020-07-28 17:49:01 +000024type simpleStatusOutput struct {
Colin Cross097ed2a2019-06-08 21:48:58 -070025 writer io.Writer
Colin Crossce525352019-06-08 18:58:00 -070026 formatter formatter
Colin Cross3c0fe0e2021-02-10 13:11:18 -080027 keepANSI bool
Colin Crossce525352019-06-08 18:58:00 -070028}
29
Patrice Arrudaf445ba12020-07-28 17:49:01 +000030// NewSimpleStatusOutput returns a StatusOutput that represents the
Colin Crossce525352019-06-08 18:58:00 -070031// current build status similarly to Ninja's built-in terminal
32// output.
Colin Cross3c0fe0e2021-02-10 13:11:18 -080033func NewSimpleStatusOutput(w io.Writer, formatter formatter, keepANSI bool) status.StatusOutput {
Patrice Arrudaf445ba12020-07-28 17:49:01 +000034 return &simpleStatusOutput{
Colin Crossce525352019-06-08 18:58:00 -070035 writer: w,
36 formatter: formatter,
Colin Cross3c0fe0e2021-02-10 13:11:18 -080037 keepANSI: keepANSI,
Colin Crossce525352019-06-08 18:58:00 -070038 }
39}
40
Patrice Arrudaf445ba12020-07-28 17:49:01 +000041func (s *simpleStatusOutput) Message(level status.MsgLevel, message string) {
Colin Crossce525352019-06-08 18:58:00 -070042 if level >= status.StatusLvl {
43 fmt.Fprintln(s.writer, s.formatter.message(level, message))
44 }
45}
46
Patrice Arrudaf445ba12020-07-28 17:49:01 +000047func (s *simpleStatusOutput) StartAction(action *status.Action, counts status.Counts) {
Colin Crossce525352019-06-08 18:58:00 -070048}
49
Patrice Arrudaf445ba12020-07-28 17:49:01 +000050func (s *simpleStatusOutput) FinishAction(result status.ActionResult, counts status.Counts) {
Colin Crossce525352019-06-08 18:58:00 -070051 str := result.Description
52 if str == "" {
53 str = result.Command
54 }
55
56 progress := s.formatter.progress(counts) + str
57
58 output := s.formatter.result(result)
Colin Cross3c0fe0e2021-02-10 13:11:18 -080059 if !s.keepANSI {
60 output = string(stripAnsiEscapes([]byte(output)))
61 }
Colin Crossce525352019-06-08 18:58:00 -070062
63 if output != "" {
64 fmt.Fprint(s.writer, progress, "\n", output)
65 } else {
66 fmt.Fprintln(s.writer, progress)
67 }
68}
69
Patrice Arrudaf445ba12020-07-28 17:49:01 +000070func (s *simpleStatusOutput) Flush() {}
Colin Crosse0df1a32019-06-09 19:40:08 -070071
Patrice Arrudaf445ba12020-07-28 17:49:01 +000072func (s *simpleStatusOutput) Write(p []byte) (int, error) {
Colin Crosse0df1a32019-06-09 19:40:08 -070073 fmt.Fprint(s.writer, string(p))
74 return len(p), nil
75}