formatter.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 RouterOS 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) Render() string {
  50. if len(t.headers) == 0 {
  51. return ""
  52. }
  53. colWidths := make([]int, len(t.headers))
  54. for i, h := range t.headers {
  55. colWidths[i] = len(h)
  56. }
  57. for _, row := range t.rows {
  58. for i, cell := range row {
  59. if i < len(colWidths) {
  60. cleanLen := len(stripANSI(cell))
  61. if cleanLen > colWidths[i] {
  62. colWidths[i] = cleanLen
  63. }
  64. }
  65. }
  66. }
  67. var sb strings.Builder
  68. // Render Header
  69. sb.WriteString(ColorCyan + ColorBold)
  70. for i, h := range t.headers {
  71. pad := colWidths[i] - len(h)
  72. sb.WriteString(fmt.Sprintf(" %s%s ", h, strings.Repeat(" ", max(0, pad))))
  73. }
  74. sb.WriteString(ColorReset + "\n")
  75. // Render separator
  76. sb.WriteString(ColorDim)
  77. for i := range t.headers {
  78. sb.WriteString(fmt.Sprintf(" %s ", strings.Repeat("-", colWidths[i])))
  79. }
  80. sb.WriteString(ColorReset + "\n")
  81. // Render Rows
  82. for _, row := range t.rows {
  83. for i := range t.headers {
  84. cell := ""
  85. if i < len(row) {
  86. cell = row[i]
  87. }
  88. cleanLen := len(stripANSI(cell))
  89. pad := colWidths[i] - cleanLen
  90. sb.WriteString(fmt.Sprintf(" %s%s ", cell, strings.Repeat(" ", max(0, pad))))
  91. }
  92. sb.WriteString("\n")
  93. }
  94. return sb.String()
  95. }
  96. func stripANSI(str string) string {
  97. var clean strings.Builder
  98. inEsc := false
  99. for _, r := range str {
  100. if r == 0x1b { // ESC
  101. inEsc = true
  102. continue
  103. }
  104. if inEsc {
  105. if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') {
  106. inEsc = false
  107. }
  108. continue
  109. }
  110. clean.WriteRune(r)
  111. }
  112. return clean.String()
  113. }
  114. func max(a, b int) int {
  115. if a > b {
  116. return a
  117. }
  118. return b
  119. }