info.go 971 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package project
  2. import (
  3. "bytes"
  4. "io"
  5. "text/tabwriter"
  6. )
  7. // InfoPart holds key/value strings.
  8. type InfoPart struct {
  9. Key, Value string
  10. }
  11. // InfoSet holds a list of Info.
  12. type InfoSet []Info
  13. // Info holds a list of InfoPart.
  14. type Info []InfoPart
  15. func (infos InfoSet) String(titleFlag bool) string {
  16. //no error checking, none of this should fail
  17. buffer := bytes.NewBuffer(make([]byte, 0, 1024))
  18. tabwriter := tabwriter.NewWriter(buffer, 4, 4, 2, ' ', 0)
  19. first := true
  20. for _, info := range infos {
  21. if first && titleFlag {
  22. writeLine(tabwriter, true, info)
  23. }
  24. first = false
  25. writeLine(tabwriter, false, info)
  26. }
  27. tabwriter.Flush()
  28. return buffer.String()
  29. }
  30. func writeLine(writer io.Writer, key bool, info Info) {
  31. first := true
  32. for _, part := range info {
  33. if !first {
  34. writer.Write([]byte{'\t'})
  35. }
  36. first = false
  37. if key {
  38. writer.Write([]byte(part.Key))
  39. } else {
  40. writer.Write([]byte(part.Value))
  41. }
  42. }
  43. writer.Write([]byte{'\n'})
  44. }