From 344a6f6415c3c1b593677adec3b8844e0839971b Mon Sep 17 00:00:00 2001 From: s Date: Thu, 13 Nov 2025 14:43:15 -0500 Subject: created pretty printing for all commands --- internal/formatter/files.go | 79 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 internal/formatter/files.go (limited to 'internal/formatter/files.go') diff --git a/internal/formatter/files.go b/internal/formatter/files.go new file mode 100644 index 0000000..7dedb53 --- /dev/null +++ b/internal/formatter/files.go @@ -0,0 +1,79 @@ +package formatter + +import ( + "fmt" + "strings" + + "git.db.org.ai/dborg/internal/models" + "git.db.org.ai/dborg/internal/utils" +) + +func FormatFilesResults(response models.OpenDirectorySearchResponse, asJSON bool) error { + if asJSON { + return utils.PrintJSON(response) + } + + PrintSection("Open Directory File Search Results") + + if len(response) == 0 { + PrintWarning("No results found") + return nil + } + + hits, ok := response["hits"].(map[string]interface{}) + if !ok { + PrintWarning("Invalid response format") + return nil + } + + total, _ := hits["total"].(map[string]interface{}) + totalValue := int64(0) + if val, ok := total["value"].(float64); ok { + totalValue = int64(val) + } + + took := int64(0) + if val, ok := response["took"].(float64); ok { + took = int64(val) + } + + fmt.Printf("Total results: %s%d%s (showing first results)\n", ColorCyan, totalValue, ColorReset) + fmt.Printf("Query time: %s%dms%s\n\n", ColorGray, took, ColorReset) + + hitsArray, ok := hits["hits"].([]interface{}) + if !ok || len(hitsArray) == 0 { + PrintWarning("No files found") + return nil + } + + for i, hit := range hitsArray { + hitMap, ok := hit.(map[string]interface{}) + if !ok { + continue + } + + source, ok := hitMap["_source"].(map[string]interface{}) + if !ok { + continue + } + + filename, _ := source["filename"].(string) + extension, _ := source["extension"].(string) + url, _ := source["url"].(string) + score, _ := hitMap["_score"].(float64) + + fmt.Printf("%s[%d]%s %s%s%s\n", ColorGray, i+1, ColorReset, ColorYellow, filename, ColorReset) + fmt.Printf(" Extension: %s%s%s\n", ColorCyan, extension, ColorReset) + fmt.Printf(" Score: %s%.2f%s\n", ColorGray, score, ColorReset) + + displayURL := strings.ReplaceAll(url, "\u0026", "&") + + fmt.Printf(" URL: %s%s%s\n", ColorBlue, displayURL, ColorReset) + + if i < len(hitsArray)-1 { + fmt.Println() + } + } + + return nil +} -- cgit v1.2.3