formatter.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package explain
  14. import (
  15. "fmt"
  16. "io"
  17. "strings"
  18. )
  19. // Formatter helps you write with indentation, and can wrap text as needed.
  20. type Formatter struct {
  21. IndentLevel int
  22. Wrap int
  23. Writer io.Writer
  24. }
  25. // Indent creates a new Formatter that will indent the code by that much more.
  26. func (f Formatter) Indent(indent int) *Formatter {
  27. f.IndentLevel = f.IndentLevel + indent
  28. return &f
  29. }
  30. // Write writes a string with the indentation set for the
  31. // Formatter. This is not wrapping text.
  32. func (f *Formatter) Write(str string, a ...interface{}) error {
  33. // Don't indent empty lines
  34. if str == "" {
  35. _, err := io.WriteString(f.Writer, "\n")
  36. return err
  37. }
  38. indent := ""
  39. for i := 0; i < f.IndentLevel; i++ {
  40. indent = indent + " "
  41. }
  42. if len(a) > 0 {
  43. str = fmt.Sprintf(str, a...)
  44. }
  45. _, err := io.WriteString(f.Writer, indent+str+"\n")
  46. return err
  47. }
  48. // WriteWrapped writes a string with the indentation set for the
  49. // Formatter, and wraps as needed.
  50. func (f *Formatter) WriteWrapped(str string, a ...interface{}) error {
  51. if f.Wrap == 0 {
  52. return f.Write(str, a...)
  53. }
  54. text := fmt.Sprintf(str, a...)
  55. strs := wrapString(text, f.Wrap-f.IndentLevel)
  56. for _, substr := range strs {
  57. if err := f.Write(substr); err != nil {
  58. return err
  59. }
  60. }
  61. return nil
  62. }
  63. type line struct {
  64. wrap int
  65. words []string
  66. }
  67. func (l *line) String() string {
  68. return strings.Join(l.words, " ")
  69. }
  70. func (l *line) Empty() bool {
  71. return len(l.words) == 0
  72. }
  73. func (l *line) Len() int {
  74. return len(l.String())
  75. }
  76. // Add adds the word to the line, returns true if we could, false if we
  77. // didn't have enough room. It's always possible to add to an empty line.
  78. func (l *line) Add(word string) bool {
  79. newLine := line{
  80. wrap: l.wrap,
  81. words: append(l.words, word),
  82. }
  83. if newLine.Len() <= l.wrap || len(l.words) == 0 {
  84. l.words = newLine.words
  85. return true
  86. }
  87. return false
  88. }
  89. func wrapString(str string, wrap int) []string {
  90. words := strings.Fields(str)
  91. wrapped := []string{}
  92. l := line{wrap: wrap}
  93. for _, word := range words {
  94. if !l.Add(word) {
  95. wrapped = append(wrapped, l.String())
  96. l = line{wrap: wrap}
  97. if !l.Add(word) {
  98. panic("Couldn't add to empty line.")
  99. }
  100. }
  101. }
  102. if !l.Empty() {
  103. wrapped = append(wrapped, l.String())
  104. }
  105. return wrapped
  106. }