summaryrefslogtreecommitdiffstats
path: root/internal/formatter/files.go
diff options
context:
space:
mode:
authors <[email protected]>2025-11-13 14:43:15 -0500
committers <[email protected]>2025-11-13 14:43:15 -0500
commit344a6f6415c3c1b593677adec3b8844e0839971b (patch)
treeb05291ecdf21917b27e9e234eeb997c2706966d5 /internal/formatter/files.go
parenta5fc01a03753c9a18ddeaf13610dd99b4b311b80 (diff)
downloaddborg-344a6f6415c3c1b593677adec3b8844e0839971b.tar.gz
dborg-344a6f6415c3c1b593677adec3b8844e0839971b.zip
created pretty printing for all commandsv1.0.0
Diffstat (limited to 'internal/formatter/files.go')
-rw-r--r--internal/formatter/files.go79
1 files changed, 79 insertions, 0 deletions
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
+}