simple_status.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. package terminal
  15. import (
  16. "fmt"
  17. "io"
  18. "android/soong/ui/status"
  19. )
  20. type simpleStatusOutput struct {
  21. writer io.Writer
  22. formatter formatter
  23. keepANSI bool
  24. }
  25. // NewSimpleStatusOutput returns a StatusOutput that represents the
  26. // current build status similarly to Ninja's built-in terminal
  27. // output.
  28. func NewSimpleStatusOutput(w io.Writer, formatter formatter, keepANSI bool) status.StatusOutput {
  29. return &simpleStatusOutput{
  30. writer: w,
  31. formatter: formatter,
  32. keepANSI: keepANSI,
  33. }
  34. }
  35. func (s *simpleStatusOutput) Message(level status.MsgLevel, message string) {
  36. if level >= status.StatusLvl {
  37. output := s.formatter.message(level, message)
  38. if !s.keepANSI {
  39. output = string(stripAnsiEscapes([]byte(output)))
  40. }
  41. fmt.Fprintln(s.writer, output)
  42. }
  43. }
  44. func (s *simpleStatusOutput) StartAction(action *status.Action, counts status.Counts) {
  45. }
  46. func (s *simpleStatusOutput) FinishAction(result status.ActionResult, counts status.Counts) {
  47. str := result.Description
  48. if str == "" {
  49. str = result.Command
  50. }
  51. progress := s.formatter.progress(counts) + str
  52. output := s.formatter.result(result)
  53. if !s.keepANSI {
  54. output = string(stripAnsiEscapes([]byte(output)))
  55. }
  56. if output != "" {
  57. fmt.Fprint(s.writer, progress, "\n", output)
  58. } else {
  59. fmt.Fprintln(s.writer, progress)
  60. }
  61. }
  62. func (s *simpleStatusOutput) Flush() {}
  63. func (s *simpleStatusOutput) Write(p []byte) (int, error) {
  64. fmt.Fprint(s.writer, string(p))
  65. return len(p), nil
  66. }