8
8
"os"
9
9
"queryparser"
10
10
"strings"
11
+
12
+ "github.com/jedib0t/go-pretty/v6/table"
11
13
)
12
14
13
15
func processQuery (input string , graph * CodeGraph , output string ) (string , error ) {
@@ -27,16 +29,29 @@ func processQuery(input string, graph *CodeGraph, output string) (string, error)
27
29
}
28
30
return string (queryResults ), nil
29
31
}
30
- var result strings.Builder
31
- result .WriteString ("------Query Results------\n " )
32
- for _ , entity := range entities {
33
- result .WriteString ("-------------------\n " )
34
- result .WriteString (entity .CodeSnippet + "\n " )
35
- result .WriteString (entity .File + "\n " )
36
- result .WriteString ("-------------------\n " )
32
+
33
+ t := table .NewWriter ()
34
+ t .SetOutputMirror (os .Stdout )
35
+ t .AppendHeader (table.Row {"#" , "File" , "Line Number" , "Type" , "Name" , "Code Snippet" })
36
+ t .SetColumnConfigs ([]table.ColumnConfig {
37
+ {
38
+ Name : "File" ,
39
+ WidthMin : 6 ,
40
+ WidthMax : 40 ,
41
+ },
42
+ {
43
+ Name : "Code Snippet" ,
44
+ WidthMin : 6 ,
45
+ WidthMax : 60 ,
46
+ },
47
+ })
48
+ for i , entity := range entities {
49
+ t .AppendRow ([]interface {}{i + 1 , entity .File , entity .LineNumber , entity .Type , entity .Name , entity .CodeSnippet })
50
+ t .AppendSeparator ()
37
51
}
38
- result .WriteString ("-------------------\n " )
39
- return result .String (), nil
52
+ t .SetStyle (table .StyleLight )
53
+ t .Render ()
54
+ return "" , nil
40
55
}
41
56
42
57
func executeProject (project , query , output string , stdin bool ) (string , error ) {
@@ -59,11 +74,10 @@ func executeProject(project, query, output string, stdin bool) (string, error) {
59
74
if strings .HasPrefix (input , ":quit" ) {
60
75
return "Okay, Bye!" , nil
61
76
}
62
- result , err : = processQuery (input , graph , output )
77
+ _ , err = processQuery (input , graph , output )
63
78
if err != nil {
64
79
return "" , fmt .Errorf ("error processing query: %w" , err )
65
80
}
66
- fmt .Println (result )
67
81
}
68
82
} else if output != "" && query != "" {
69
83
return processQuery (query , graph , output )
0 commit comments