formatter.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package cli
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. const (
  7. ColorReset = "\033[0m"
  8. ColorBold = "\033[1m"
  9. ColorDim = "\033[2m"
  10. ColorRed = "\033[31m"
  11. ColorGreen = "\033[32m"
  12. ColorYellow = "\033[33m"
  13. ColorBlue = "\033[34m"
  14. ColorMagenta = "\033[35m"
  15. ColorCyan = "\033[36m"
  16. ColorWhite = "\033[37m"
  17. )
  18. func Banner(version, username, role, hostname string) string {
  19. var sb strings.Builder
  20. sb.WriteString("\n")
  21. sb.WriteString(ColorCyan + ColorBold)
  22. sb.WriteString(" ____ _ _____ _ _ \n")
  23. sb.WriteString(" | _ \\ ___| |_|_ _(_) | __\n")
  24. sb.WriteString(" | | | |/ _ \\ '_ \\| | | | |/ /\n")
  25. sb.WriteString(" | |_| | __/ |_) | | | | < \n")
  26. sb.WriteString(" |____/ \\___|_.__/|_| |_|_|\\_\\\n")
  27. sb.WriteString(ColorReset)
  28. sb.WriteString(ColorBold + " DebTik Network Appliance Controller" + ColorReset + "\n")
  29. sb.WriteString(fmt.Sprintf(ColorDim+" Appliance Hostname : %s%s\n", hostname, ColorReset))
  30. sb.WriteString(fmt.Sprintf(ColorDim+" Authenticated User : %s%s%s (Role: %s%s%s)\n", ColorGreen, username, ColorDim, ColorYellow, strings.ToUpper(role), ColorDim))
  31. sb.WriteString(ColorDim + " Type 'help' or '?' for available commands.\n" + ColorReset)
  32. sb.WriteString("\n")
  33. return sb.String()
  34. }
  35. // TableWriter formats aligned text tables with custom columns
  36. type TableWriter struct {
  37. headers []string
  38. rows [][]string
  39. }
  40. func NewTable(headers ...string) *TableWriter {
  41. return &TableWriter{
  42. headers: headers,
  43. rows: make([][]string, 0),
  44. }
  45. }
  46. func (t *TableWriter) AddRow(cols ...string) {
  47. t.rows = append(t.rows, cols)
  48. }
  49. func (t *TableWriter) String() string {
  50. return t.Render()
  51. }
  52. func (t *TableWriter) Render() string {
  53. if len(t.headers) == 0 {
  54. return ""
  55. }
  56. colWidths := make([]int, len(t.headers))
  57. for i, h := range t.headers {
  58. colWidths[i] = len(h)
  59. }
  60. for _, row := range t.rows {
  61. for i, cell := range row {
  62. if i < len(colWidths) {
  63. cleanLen := len(stripANSI(cell))
  64. if cleanLen > colWidths[i] {
  65. colWidths[i] = cleanLen
  66. }
  67. }
  68. }
  69. }
  70. var sb strings.Builder
  71. // Render Header
  72. sb.WriteString(ColorCyan + ColorBold)
  73. for i, h := range t.headers {
  74. pad := colWidths[i] - len(h)
  75. sb.WriteString(fmt.Sprintf(" %s%s ", h, strings.Repeat(" ", max(0, pad))))
  76. }
  77. sb.WriteString(ColorReset + "\n")
  78. // Render separator
  79. sb.WriteString(ColorDim)
  80. for i := range t.headers {
  81. sb.WriteString(fmt.Sprintf(" %s ", strings.Repeat("-", colWidths[i])))
  82. }
  83. sb.WriteString(ColorReset + "\n")
  84. // Render Rows
  85. for _, row := range t.rows {
  86. for i := range t.headers {
  87. cell := ""
  88. if i < len(row) {
  89. cell = row[i]
  90. }
  91. cleanLen := len(stripANSI(cell))
  92. pad := colWidths[i] - cleanLen
  93. sb.WriteString(fmt.Sprintf(" %s%s ", cell, strings.Repeat(" ", max(0, pad))))
  94. }
  95. sb.WriteString("\n")
  96. }
  97. return sb.String()
  98. }
  99. func stripANSI(str string) string {
  100. var clean strings.Builder
  101. inEsc := false
  102. for _, r := range str {
  103. if r == 0x1b { // ESC
  104. inEsc = true
  105. continue
  106. }
  107. if inEsc {
  108. if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') {
  109. inEsc = false
  110. }
  111. continue
  112. }
  113. clean.WriteRune(r)
  114. }
  115. return clean.String()
  116. }
  117. func max(a, b int) int {
  118. if a > b {
  119. return a
  120. }
  121. return b
  122. }