Files
kb/client/internal/output/format.go
T
steve 9aab79d49b v2 restructure: Go client, Docker engine, release tooling
- Remove v1 Python CLI (src/kb_search/, tests/, root pyproject.toml, uv.lock, .venv)
- Add Go client with cross-platform build (client/)
- Add FastAPI engine with NVIDIA and multi-stage ROCm Dockerfiles (engine/)
- Add VERSION files for client and engine, wired into builds
- Add release.sh for automated build, tag, release, and Docker push
- Update README with build/release docs and ROCm migration note
- Clean up .gitignore for v2 project structure

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 21:52:25 +00:00

91 lines
1.6 KiB
Go

package output
import (
"encoding/json"
"fmt"
"os"
"strings"
"github.com/kb-search/kb/internal/config"
)
// IsJSON returns true if the configured output format is "json".
func IsJSON() bool {
return config.Get().DefaultFormat == "json"
}
// PrintJSON pretty-prints data as JSON to stdout.
func PrintJSON(data interface{}) {
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
_ = enc.Encode(data)
}
// PrintTable prints a simple aligned table with headers and rows.
func PrintTable(headers []string, rows [][]string) {
if len(headers) == 0 {
return
}
// Calculate column widths
widths := make([]int, len(headers))
for i, h := range headers {
widths[i] = len(h)
}
for _, row := range rows {
for i := 0; i < len(row) && i < len(widths); i++ {
if len(row[i]) > widths[i] {
widths[i] = len(row[i])
}
}
}
// Print header
for i, h := range headers {
if i > 0 {
fmt.Print(" ")
}
fmt.Printf("%-*s", widths[i], h)
}
fmt.Println()
// Print separator
for i, w := range widths {
if i > 0 {
fmt.Print(" ")
}
fmt.Print(strings.Repeat("-", w))
}
fmt.Println()
// Print rows
for _, row := range rows {
for i := 0; i < len(headers); i++ {
if i > 0 {
fmt.Print(" ")
}
val := ""
if i < len(row) {
val = row[i]
}
fmt.Printf("%-*s", widths[i], val)
}
fmt.Println()
}
}
// PrintKeyValue prints aligned key-value pairs.
func PrintKeyValue(pairs [][]string) {
maxKey := 0
for _, p := range pairs {
if len(p) >= 2 && len(p[0]) > maxKey {
maxKey = len(p[0])
}
}
for _, p := range pairs {
if len(p) >= 2 {
fmt.Printf("%-*s %s\n", maxKey, p[0]+":", p[1])
}
}
}