Skip to content

Commit b58ed37

Browse files
committed
fix: return JSONRenderer when JSON output is requested
1 parent 78138ea commit b58ed37

2 files changed

Lines changed: 23 additions & 16 deletions

File tree

internal/renderer/json.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,26 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7-
8-
"github.com/go-openapi/swag"
97
)
108

119
type JSONRenderer struct{}
1210

1311
func (jr JSONRenderer) Render(data []ResponseData) {
14-
if !swag.IsZero(data) {
15-
JSONString, err := json.Marshal(data)
16-
if err != nil {
17-
fmt.Println("Could not decode the result as JSON.")
18-
}
12+
if len(data) == 0 {
13+
return
14+
}
1915

20-
var prettyJSON bytes.Buffer
21-
if err := json.Indent(&prettyJSON, JSONString, "", " "); err != nil {
22-
fmt.Println("JSON format error")
23-
}
16+
JSONString, err := json.Marshal(data)
17+
if err != nil {
18+
fmt.Println("Could not decode the result as JSON.")
19+
return
20+
}
2421

25-
fmt.Println(prettyJSON.String())
22+
var prettyJSON bytes.Buffer
23+
if err := json.Indent(&prettyJSON, JSONString, "", " "); err != nil {
24+
fmt.Println("JSON format error")
25+
return
2626
}
27+
28+
fmt.Println(prettyJSON.String())
2729
}

internal/renderer/main.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
outputTable "github.com/latitudesh/lsh/internal/output/table"
77
"github.com/spf13/viper"
8+
"golang.org/x/term"
89
)
910

1011
type ResponseData interface {
@@ -22,10 +23,14 @@ func GetRenderer() Renderer {
2223
return TableRenderer{} // Old ASCII
2324
}
2425

25-
// Check if JSON was requested
26-
if viper.GetBool("json") {
27-
// You can create a JSONRenderer later
28-
return TableRenderer{} // fallback
26+
// Check if JSON was requested via --json flag or -o json
27+
if viper.GetBool("json") || viper.GetString("output") == "json" {
28+
return JSONRenderer{}
29+
}
30+
31+
// If stdout is not a terminal (e.g., pipe), use table output
32+
if !term.IsTerminal(int(os.Stdout.Fd())) {
33+
return TableRenderer{}
2934
}
3035

3136
// Default: use interactive Bubble Tea

0 commit comments

Comments
 (0)