tables.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "github.com/aquasecurity/table"
  7. isatty "github.com/mattn/go-isatty"
  8. )
  9. func shouldWeColorize() bool {
  10. if csConfig.Cscli.Color == "yes" {
  11. return true
  12. }
  13. if csConfig.Cscli.Color == "no" {
  14. return false
  15. }
  16. return isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd())
  17. }
  18. func newTable(out io.Writer) *table.Table {
  19. if out == nil {
  20. panic("newTable: out is nil")
  21. }
  22. t := table.New(out)
  23. if shouldWeColorize() {
  24. t.SetLineStyle(table.StyleBrightBlack)
  25. t.SetHeaderStyle(table.StyleItalic)
  26. }
  27. if shouldWeColorize() {
  28. t.SetDividers(table.UnicodeRoundedDividers)
  29. } else {
  30. t.SetDividers(table.ASCIIDividers)
  31. }
  32. return t
  33. }
  34. func newLightTable(out io.Writer) *table.Table {
  35. if out == nil {
  36. panic("newTable: out is nil")
  37. }
  38. t := newTable(out)
  39. t.SetRowLines(false)
  40. t.SetBorderLeft(false)
  41. t.SetBorderRight(false)
  42. // This leaves three spaces between columns:
  43. // left padding, invisible border, right padding
  44. // There is no way to make two spaces without
  45. // a SetColumnLines() method, but it's close enough.
  46. t.SetPadding(1)
  47. if shouldWeColorize() {
  48. t.SetDividers(table.Dividers{
  49. ALL: "─",
  50. NES: "─",
  51. NSW: "─",
  52. NEW: "─",
  53. ESW: "─",
  54. NE: "─",
  55. NW: "─",
  56. SW: "─",
  57. ES: "─",
  58. EW: "─",
  59. NS: " ",
  60. })
  61. } else {
  62. t.SetDividers(table.Dividers{
  63. ALL: "-",
  64. NES: "-",
  65. NSW: "-",
  66. NEW: "-",
  67. ESW: "-",
  68. NE: "-",
  69. NW: "-",
  70. SW: "-",
  71. ES: "-",
  72. EW: "-",
  73. NS: " ",
  74. })
  75. }
  76. return t
  77. }
  78. func renderTableTitle(out io.Writer, title string) {
  79. if out == nil {
  80. panic("renderTableTitle: out is nil")
  81. }
  82. if title == "" {
  83. return
  84. }
  85. fmt.Fprintln(out, title)
  86. }