Skip to content

Commit 923f0f9

Browse files
committed
Improve plaintext and json outputs
1 parent 433ceeb commit 923f0f9

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

cmd/namigo/sub/search.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ func SearchPackageAction(c *cli.Context) error {
6565
}
6666

6767
if ptf.isEmpty() {
68-
fmt.Printf("🌧️ No results\n")
68+
fmt.Println("🌧️ No results")
6969
} else {
70-
fmt.Printf("🍺 Prepare results\n\n")
70+
fmt.Printf("🍺 Prepare %s results\n\n", outputMode)
7171
}
7272

7373
time.Sleep(500 * time.Millisecond)
@@ -111,9 +111,9 @@ func SearchDNSAction(c *cli.Context) error {
111111
}
112112

113113
if ptf.isEmpty() {
114-
fmt.Printf("🌧️ No results\n")
114+
fmt.Println("🌧️ No results")
115115
} else {
116-
fmt.Printf("🍺 Prepare results\n\n")
116+
fmt.Printf("🍺 Prepare %s results\n\n", outputMode)
117117
}
118118

119119
time.Sleep(500 * time.Millisecond)

cmd/namigo/sub/search_priv.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,27 @@ type searchFormatter struct{}
1515
func (f *searchFormatter) formatGo(result any) string {
1616
res := result.(model.GoPackageResult)
1717
if len(res.Description) > 80 || len(res.Description) == 0 {
18-
return fmt.Sprintf("\t[golang] %s %s ->\n\t\t%.80s...", res.Name, res.Path, res.Description)
18+
return fmt.Sprintf("📦 [golang] %s (%s) ->\n\t%.80s...", res.Name, res.Path, res.Description)
1919
}
20-
return fmt.Sprintf("\t[golang] %s %s ->\n\t\t%s", res.Name, res.Path, res.Description)
20+
return fmt.Sprintf("📦 [golang] %s (%s) ->\n\t%s", res.Name, res.Path, res.Description)
2121
}
2222

2323
// formatNPM formats NPM package results.
2424
func (f *searchFormatter) formatNPM(result any) string {
2525
res := result.(model.NPMPackageResult)
2626
if len(res.Description) > 80 || len(res.Description) == 0 {
27-
return fmt.Sprintf("\t[npm] %s [exact=%v] ->\n\t\t%.80s...", res.Name, res.IsExactMatch, res.Description)
27+
return fmt.Sprintf("📦 [npm] %s [exact=%v] ->\n\t%.80s...", res.Name, res.IsExactMatch, res.Description)
2828
}
29-
return fmt.Sprintf("\t[npm] %s [exact=%v] ->\n\t\t%s", res.Name, res.IsExactMatch, res.Description)
29+
return fmt.Sprintf("📦 [npm] %s [exact=%v] ->\n\t%s", res.Name, res.IsExactMatch, res.Description)
3030
}
3131

3232
// formatPyPI formats PyPI package results.
3333
func (f *searchFormatter) formatPyPI(result any) string {
3434
res := result.(model.PyPIPackageResult)
3535
if len(res.Description) > 80 || len(res.Description) == 0 {
36-
return fmt.Sprintf("\t[pypi] %s by %s ->\n\t\t%.80s...", res.Name, res.Author, res.Description)
36+
return fmt.Sprintf("📦 [pypi] %s by %s ->\n\t%.80s...", res.Name, res.Author, res.Description)
3737
}
38-
return fmt.Sprintf("\t[pypi] %s by %s ->\n\t\t%s", res.Name, res.Author, res.Description)
38+
return fmt.Sprintf("📦 [pypi] %s by %s ->\n\t%s", res.Name, res.Author, res.Description)
3939
}
4040

4141
// formatDNS formats DNS results.
@@ -47,7 +47,7 @@ func (f *searchFormatter) formatDNS(result any) string {
4747
} else {
4848
content = fmt.Sprintf("The IPs are %v", res.IPList)
4949
}
50-
return fmt.Sprintf("\t[dns] %s w/ %d IPs ->\n\t\t%v", res.FQDN, len(res.IPList), content)
50+
return fmt.Sprintf("🌎 [dns] %s w/ %d IPs ->\n\t%v", res.FQDN, len(res.IPList), content)
5151
}
5252

5353
// searchPortfolio has result slices and task helpers.

internal/util/output.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,18 @@ func PrintResults(results any, label string, format func(any) string, mode Outpu
5555
func printResults[T any](results []T, label string, format func(any) string, mode OutputMode) {
5656
switch mode {
5757
case JSONMode:
58-
jsonData, err := json.MarshalIndent(results, "", " ")
58+
type wrapper struct {
59+
Label string `json:"label"`
60+
Results []T `json:"results"`
61+
}
62+
wrapped := &wrapper{Label: label, Results: results}
63+
jsonData, err := json.MarshalIndent(wrapped, "", " ")
5964
if err != nil {
6065
fmt.Printf("Cannot print %s for %s: %v\n", mode, label, err)
6166
return
6267
}
63-
fmt.Printf("%s: %s\n", label, jsonData)
68+
fmt.Printf("%s\n", jsonData)
6469
case TextMode:
65-
fmt.Printf("%d %s results found:\n", len(results), label)
6670
for _, r := range results {
6771
fmt.Println(format(r))
6872
}

0 commit comments

Comments
 (0)