RenderTable renders a formatted table using lipgloss/table package
(config TableConfig)
| 188 | |
| 189 | // RenderTable renders a formatted table using lipgloss/table package |
| 190 | func RenderTable(config TableConfig) string { |
| 191 | if len(config.Headers) == 0 { |
| 192 | consoleLog.Print("No headers provided for table rendering") |
| 193 | return "" |
| 194 | } |
| 195 | |
| 196 | consoleLog.Printf("Rendering table: title=%s, columns=%d, rows=%d", config.Title, len(config.Headers), len(config.Rows)) |
| 197 | |
| 198 | // Use caller-supplied TTY detector when provided (e.g. tty.IsStderrTerminal |
| 199 | // for tables written to stderr), otherwise fall back to stdout detection. |
| 200 | ttyCheck := isTTY |
| 201 | if config.TTYFunc != nil { |
| 202 | ttyCheck = config.TTYFunc |
| 203 | } |
| 204 | |
| 205 | var output strings.Builder |
| 206 | |
| 207 | if config.Title != "" { |
| 208 | output.WriteString(applyStyle(styles.TableTitle, config.Title)) |
| 209 | output.WriteString("\n") |
| 210 | } |
| 211 | |
| 212 | allRows := config.Rows |
| 213 | if config.ShowTotal && len(config.TotalRow) > 0 { |
| 214 | allRows = append(allRows, config.TotalRow) |
| 215 | } |
| 216 | |
| 217 | dataRowCount := len(config.Rows) |
| 218 | styleFunc := buildTableStyleFunc(config, ttyCheck, dataRowCount) |
| 219 | |
| 220 | borderStyle := lipgloss.NewStyle() |
| 221 | if ttyCheck() { |
| 222 | borderStyle = styles.TableBorder |
| 223 | } |
| 224 | |
| 225 | t := table.New(). |
| 226 | Headers(config.Headers...). |
| 227 | Rows(allRows...). |
| 228 | Border(styles.RoundedBorder). |
| 229 | BorderStyle(borderStyle). |
| 230 | StyleFunc(styleFunc) |
| 231 | |
| 232 | output.WriteString(t.String()) |
| 233 | output.WriteString("\n") |
| 234 | |
| 235 | return output.String() |
| 236 | } |
| 237 | |
| 238 | // buildTableStyleFunc returns the lipgloss style function used by RenderTable. |
| 239 | // config supplies the ShowTotal/TotalRow flags; ttyCheck detects terminal output; |