blob: 4e8c56804032e4a10b30ee4689e9c008b20e6185 [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
27}
28
Patrice Arrudaf445ba12020-07-28 17:49:01 +000029// NewSimpleStatusOutput returns a StatusOutput that represents the
Colin Crossce525352019-06-08 18:58:00 -070030// current build status similarly to Ninja's built-in terminal
31// output.
Patrice Arrudaf445ba12020-07-28 17:49:01 +000032func NewSimpleStatusOutput(w io.Writer, formatter formatter) status.StatusOutput {
33 return &simpleStatusOutput{
Colin Crossce525352019-06-08 18:58:00 -070034 writer: w,
35 formatter: formatter,
36 }
37}
38
Patrice Arrudaf445ba12020-07-28 17:49:01 +000039func (s *simpleStatusOutput) Message(level status.MsgLevel, message string) {
Colin Crossce525352019-06-08 18:58:00 -070040 if level >= status.StatusLvl {
41 fmt.Fprintln(s.writer, s.formatter.message(level, message))
42 }
43}
44
Patrice Arrudaf445ba12020-07-28 17:49:01 +000045func (s *simpleStatusOutput) StartAction(action *status.Action, counts status.Counts) {
Colin Crossce525352019-06-08 18:58:00 -070046}
47
Patrice Arrudaf445ba12020-07-28 17:49:01 +000048func (s *simpleStatusOutput) FinishAction(result status.ActionResult, counts status.Counts) {
Colin Crossce525352019-06-08 18:58:00 -070049 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
Patrice Arrudaf445ba12020-07-28 17:49:01 +000066func (s *simpleStatusOutput) Flush() {}
Colin Crosse0df1a32019-06-09 19:40:08 -070067
Patrice Arrudaf445ba12020-07-28 17:49:01 +000068func (s *simpleStatusOutput) Write(p []byte) (int, error) {
Colin Crosse0df1a32019-06-09 19:40:08 -070069 fmt.Fprint(s.writer, string(p))
70 return len(p), nil
71}