summaryrefslogtreecommitdiffstats
path: root/internal/formatter/x.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/formatter/x.go')
-rw-r--r--internal/formatter/x.go43
1 files changed, 43 insertions, 0 deletions
diff --git a/internal/formatter/x.go b/internal/formatter/x.go
index f701797..a79d173 100644
--- a/internal/formatter/x.go
+++ b/internal/formatter/x.go
@@ -377,3 +377,46 @@ func wrapText(text string, width int) []string {
return lines
}
+
+func FormatXGeneric(resp *models.XResponse, asJSON bool) (string, error) {
+ if asJSON {
+ data, err := json.MarshalIndent(resp, "", " ")
+ if err != nil {
+ return "", fmt.Errorf("failed to marshal JSON: %w", err)
+ }
+ return string(data), nil
+ }
+
+ var buf bytes.Buffer
+
+ if resp.Error != "" {
+ fmt.Fprintf(&buf, "%s\n", Red(fmt.Sprintf("Error: %s", resp.Error)))
+ return buf.String(), nil
+ }
+
+ if resp.Message != "" {
+ fmt.Fprintf(&buf, "%s\n\n", Bold(Cyan(resp.Message)))
+ }
+
+ if resp.Response != "" {
+ fmt.Fprintf(&buf, "%s\n\n", resp.Response)
+ }
+
+ if resp.Data != nil {
+ dataJSON, err := json.MarshalIndent(resp.Data, "", " ")
+ if err == nil {
+ fmt.Fprintf(&buf, "%s\n", string(dataJSON))
+ }
+ }
+
+ if resp.Credits.Remaining > 0 || resp.Credits.Unlimited {
+ fmt.Fprintf(&buf, "\n%s ", Blue("Credits Remaining:"))
+ if resp.Credits.Unlimited {
+ fmt.Fprintf(&buf, "%s\n", Green("Unlimited"))
+ } else {
+ fmt.Fprintf(&buf, "%s\n", FormatCredits(int64(resp.Credits.Remaining)))
+ }
+ }
+
+ return buf.String(), nil
+}