summaryrefslogtreecommitdiffstats
path: root/internal/formatter/files.go
blob: 7dedb53cb8f3baecd9cf8263ecea8e5a19d660e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
}