blob: cef3b5d5c16d4d01ce0d762a97bb0883e4525836 [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 {
Sasha Smundak827aead2021-11-19 11:22:54 -080025 writer io.Writer
26 formatter formatter
27 keepANSI bool
28 outputLevel status.MsgLevel
Colin Crossce525352019-06-08 18:58:00 -070029}
30
Patrice Arrudaf445ba12020-07-28 17:49:01 +000031// NewSimpleStatusOutput returns a StatusOutput that represents the
Colin Crossce525352019-06-08 18:58:00 -070032// current build status similarly to Ninja's built-in terminal
33// output.
Sasha Smundak827aead2021-11-19 11:22:54 -080034func 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 Arrudaf445ba12020-07-28 17:49:01 +000039 return &simpleStatusOutput{
Sasha Smundak827aead2021-11-19 11:22:54 -080040 writer: w,
41 formatter: formatter,
42 keepANSI: keepANSI,
43 outputLevel: level,
Colin Crossce525352019-06-08 18:58:00 -070044 }
45}
46
Patrice Arrudaf445ba12020-07-28 17:49:01 +000047func (s *simpleStatusOutput) Message(level status.MsgLevel, message string) {
Sasha Smundak827aead2021-11-19 11:22:54 -080048 if level >= s.outputLevel {
Dan Willemsene2cdecf2022-06-08 20:32:07 -070049 output := s.formatter.message(level, message)
50 if !s.keepANSI {
51 output = string(stripAnsiEscapes([]byte(output)))
52 }
53 fmt.Fprintln(s.writer, output)
Colin Crossce525352019-06-08 18:58:00 -070054 }
55}
56
Sasha Smundak827aead2021-11-19 11:22:54 -080057func (s *simpleStatusOutput) StartAction(_ *status.Action, _ status.Counts) {
Colin Crossce525352019-06-08 18:58:00 -070058}
59
Patrice Arrudaf445ba12020-07-28 17:49:01 +000060func (s *simpleStatusOutput) FinishAction(result status.ActionResult, counts status.Counts) {
Sasha Smundak827aead2021-11-19 11:22:54 -080061 if s.outputLevel > status.StatusLvl {
62 return
63 }
Colin Crossce525352019-06-08 18:58:00 -070064 str := result.Description
65 if str == "" {
66 str = result.Command
67 }
68
69 progress := s.formatter.progress(counts) + str
70
71 output := s.formatter.result(result)
Colin Cross3c0fe0e2021-02-10 13:11:18 -080072 if !s.keepANSI {
73 output = string(stripAnsiEscapes([]byte(output)))
74 }
Colin Crossce525352019-06-08 18:58:00 -070075
76 if output != "" {
77 fmt.Fprint(s.writer, progress, "\n", output)
78 } else {
79 fmt.Fprintln(s.writer, progress)
80 }
81}
82
Patrice Arrudaf445ba12020-07-28 17:49:01 +000083func (s *simpleStatusOutput) Flush() {}
Colin Crosse0df1a32019-06-09 19:40:08 -070084
Patrice Arrudaf445ba12020-07-28 17:49:01 +000085func (s *simpleStatusOutput) Write(p []byte) (int, error) {
Colin Crosse0df1a32019-06-09 19:40:08 -070086 fmt.Fprint(s.writer, string(p))
87 return len(p), nil
88}