xref: /aosp_15_r20/build/soong/ui/terminal/simple_status.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1*333d2b36SAndroid Build Coastguard Worker// Copyright 2019 Google Inc. All rights reserved.
2*333d2b36SAndroid Build Coastguard Worker//
3*333d2b36SAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License");
4*333d2b36SAndroid Build Coastguard Worker// you may not use this file except in compliance with the License.
5*333d2b36SAndroid Build Coastguard Worker// You may obtain a copy of the License at
6*333d2b36SAndroid Build Coastguard Worker//
7*333d2b36SAndroid Build Coastguard Worker//     http://www.apache.org/licenses/LICENSE-2.0
8*333d2b36SAndroid Build Coastguard Worker//
9*333d2b36SAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software
10*333d2b36SAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS,
11*333d2b36SAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*333d2b36SAndroid Build Coastguard Worker// See the License for the specific language governing permissions and
13*333d2b36SAndroid Build Coastguard Worker// limitations under the License.
14*333d2b36SAndroid Build Coastguard Worker
15*333d2b36SAndroid Build Coastguard Workerpackage terminal
16*333d2b36SAndroid Build Coastguard Worker
17*333d2b36SAndroid Build Coastguard Workerimport (
18*333d2b36SAndroid Build Coastguard Worker	"fmt"
19*333d2b36SAndroid Build Coastguard Worker	"io"
20*333d2b36SAndroid Build Coastguard Worker
21*333d2b36SAndroid Build Coastguard Worker	"android/soong/ui/status"
22*333d2b36SAndroid Build Coastguard Worker)
23*333d2b36SAndroid Build Coastguard Worker
24*333d2b36SAndroid Build Coastguard Workertype simpleStatusOutput struct {
25*333d2b36SAndroid Build Coastguard Worker	writer    io.Writer
26*333d2b36SAndroid Build Coastguard Worker	formatter formatter
27*333d2b36SAndroid Build Coastguard Worker	keepANSI  bool
28*333d2b36SAndroid Build Coastguard Worker}
29*333d2b36SAndroid Build Coastguard Worker
30*333d2b36SAndroid Build Coastguard Worker// NewSimpleStatusOutput returns a StatusOutput that represents the
31*333d2b36SAndroid Build Coastguard Worker// current build status similarly to Ninja's built-in terminal
32*333d2b36SAndroid Build Coastguard Worker// output.
33*333d2b36SAndroid Build Coastguard Workerfunc NewSimpleStatusOutput(w io.Writer, formatter formatter, keepANSI bool) status.StatusOutput {
34*333d2b36SAndroid Build Coastguard Worker	return &simpleStatusOutput{
35*333d2b36SAndroid Build Coastguard Worker		writer:    w,
36*333d2b36SAndroid Build Coastguard Worker		formatter: formatter,
37*333d2b36SAndroid Build Coastguard Worker		keepANSI:  keepANSI,
38*333d2b36SAndroid Build Coastguard Worker	}
39*333d2b36SAndroid Build Coastguard Worker}
40*333d2b36SAndroid Build Coastguard Worker
41*333d2b36SAndroid Build Coastguard Workerfunc (s *simpleStatusOutput) Message(level status.MsgLevel, message string) {
42*333d2b36SAndroid Build Coastguard Worker	if level >= status.StatusLvl {
43*333d2b36SAndroid Build Coastguard Worker		output := s.formatter.message(level, message)
44*333d2b36SAndroid Build Coastguard Worker		if !s.keepANSI {
45*333d2b36SAndroid Build Coastguard Worker			output = string(stripAnsiEscapes([]byte(output)))
46*333d2b36SAndroid Build Coastguard Worker		}
47*333d2b36SAndroid Build Coastguard Worker		fmt.Fprintln(s.writer, output)
48*333d2b36SAndroid Build Coastguard Worker	}
49*333d2b36SAndroid Build Coastguard Worker}
50*333d2b36SAndroid Build Coastguard Worker
51*333d2b36SAndroid Build Coastguard Workerfunc (s *simpleStatusOutput) StartAction(action *status.Action, counts status.Counts) {
52*333d2b36SAndroid Build Coastguard Worker}
53*333d2b36SAndroid Build Coastguard Worker
54*333d2b36SAndroid Build Coastguard Workerfunc (s *simpleStatusOutput) FinishAction(result status.ActionResult, counts status.Counts) {
55*333d2b36SAndroid Build Coastguard Worker	str := result.Description
56*333d2b36SAndroid Build Coastguard Worker	if str == "" {
57*333d2b36SAndroid Build Coastguard Worker		str = result.Command
58*333d2b36SAndroid Build Coastguard Worker	}
59*333d2b36SAndroid Build Coastguard Worker
60*333d2b36SAndroid Build Coastguard Worker	progress := s.formatter.progress(counts) + str
61*333d2b36SAndroid Build Coastguard Worker
62*333d2b36SAndroid Build Coastguard Worker	output := s.formatter.result(result)
63*333d2b36SAndroid Build Coastguard Worker	if !s.keepANSI {
64*333d2b36SAndroid Build Coastguard Worker		output = string(stripAnsiEscapes([]byte(output)))
65*333d2b36SAndroid Build Coastguard Worker	}
66*333d2b36SAndroid Build Coastguard Worker
67*333d2b36SAndroid Build Coastguard Worker	if output != "" {
68*333d2b36SAndroid Build Coastguard Worker		fmt.Fprint(s.writer, progress, "\n", output)
69*333d2b36SAndroid Build Coastguard Worker	} else {
70*333d2b36SAndroid Build Coastguard Worker		fmt.Fprintln(s.writer, progress)
71*333d2b36SAndroid Build Coastguard Worker	}
72*333d2b36SAndroid Build Coastguard Worker}
73*333d2b36SAndroid Build Coastguard Worker
74*333d2b36SAndroid Build Coastguard Workerfunc (s *simpleStatusOutput) Flush() {}
75*333d2b36SAndroid Build Coastguard Worker
76*333d2b36SAndroid Build Coastguard Workerfunc (s *simpleStatusOutput) Write(p []byte) (int, error) {
77*333d2b36SAndroid Build Coastguard Worker	fmt.Fprint(s.writer, string(p))
78*333d2b36SAndroid Build Coastguard Worker	return len(p), nil
79*333d2b36SAndroid Build Coastguard Worker}
80