format.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. "strings"
  18. "time"
  19. "android/soong/ui/status"
  20. )
  21. type formatter struct {
  22. format string
  23. quiet bool
  24. start time.Time
  25. }
  26. // newFormatter returns a formatter for formatting output to
  27. // the terminal in a format similar to Ninja.
  28. // format takes nearly all the same options as NINJA_STATUS.
  29. // %c is currently unsupported.
  30. func newFormatter(format string, quiet bool) formatter {
  31. return formatter{
  32. format: format,
  33. quiet: quiet,
  34. start: time.Now(),
  35. }
  36. }
  37. func (s formatter) message(level status.MsgLevel, message string) string {
  38. if level >= status.ErrorLvl {
  39. return fmt.Sprintf("FAILED: %s", message)
  40. } else if level > status.StatusLvl {
  41. return fmt.Sprintf("%s%s", level.Prefix(), message)
  42. } else if level == status.StatusLvl {
  43. return message
  44. }
  45. return ""
  46. }
  47. func (s formatter) progress(counts status.Counts) string {
  48. if s.format == "" {
  49. return fmt.Sprintf("[%3d%% %d/%d] ", 100*counts.FinishedActions/counts.TotalActions, counts.FinishedActions, counts.TotalActions)
  50. }
  51. buf := &strings.Builder{}
  52. for i := 0; i < len(s.format); i++ {
  53. c := s.format[i]
  54. if c != '%' {
  55. buf.WriteByte(c)
  56. continue
  57. }
  58. i = i + 1
  59. if i == len(s.format) {
  60. buf.WriteByte(c)
  61. break
  62. }
  63. c = s.format[i]
  64. switch c {
  65. case '%':
  66. buf.WriteByte(c)
  67. case 's':
  68. fmt.Fprintf(buf, "%d", counts.StartedActions)
  69. case 't':
  70. fmt.Fprintf(buf, "%d", counts.TotalActions)
  71. case 'r':
  72. fmt.Fprintf(buf, "%d", counts.RunningActions)
  73. case 'u':
  74. fmt.Fprintf(buf, "%d", counts.TotalActions-counts.StartedActions)
  75. case 'f':
  76. fmt.Fprintf(buf, "%d", counts.FinishedActions)
  77. case 'o':
  78. fmt.Fprintf(buf, "%.1f", float64(counts.FinishedActions)/time.Since(s.start).Seconds())
  79. case 'c':
  80. // TODO: implement?
  81. buf.WriteRune('?')
  82. case 'p':
  83. fmt.Fprintf(buf, "%3d%%", 100*counts.FinishedActions/counts.TotalActions)
  84. case 'e':
  85. fmt.Fprintf(buf, "%.3f", time.Since(s.start).Seconds())
  86. default:
  87. buf.WriteString("unknown placeholder '")
  88. buf.WriteByte(c)
  89. buf.WriteString("'")
  90. }
  91. }
  92. return buf.String()
  93. }
  94. func (s formatter) result(result status.ActionResult) string {
  95. var ret string
  96. if result.Error != nil {
  97. targets := strings.Join(result.Outputs, " ")
  98. if s.quiet || result.Command == "" {
  99. ret = fmt.Sprintf("FAILED: %s\n%s", targets, result.Output)
  100. } else {
  101. ret = fmt.Sprintf("FAILED: %s\n%s\n%s", targets, result.Command, result.Output)
  102. }
  103. } else if result.Output != "" {
  104. ret = result.Output
  105. }
  106. if len(ret) > 0 && ret[len(ret)-1] != '\n' {
  107. ret += "\n"
  108. }
  109. return ret
  110. }