mirror of
https://github.com/aljazceru/opencode.git
synced 2026-01-03 16:05:00 +01:00
sync
This commit is contained in:
46
packages/tui/internal/format/format.go
Normal file
46
packages/tui/internal/format/format.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package format
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// OutputFormat represents the format for non-interactive mode output
|
||||
type OutputFormat string
|
||||
|
||||
const (
|
||||
// TextFormat is plain text output (default)
|
||||
TextFormat OutputFormat = "text"
|
||||
|
||||
// JSONFormat is output wrapped in a JSON object
|
||||
JSONFormat OutputFormat = "json"
|
||||
)
|
||||
|
||||
// IsValid checks if the output format is valid
|
||||
func (f OutputFormat) IsValid() bool {
|
||||
return f == TextFormat || f == JSONFormat
|
||||
}
|
||||
|
||||
// String returns the string representation of the output format
|
||||
func (f OutputFormat) String() string {
|
||||
return string(f)
|
||||
}
|
||||
|
||||
// FormatOutput formats the given content according to the specified format
|
||||
func FormatOutput(content string, format OutputFormat) (string, error) {
|
||||
switch format {
|
||||
case TextFormat:
|
||||
return content, nil
|
||||
case JSONFormat:
|
||||
jsonData := map[string]string{
|
||||
"response": content,
|
||||
}
|
||||
jsonBytes, err := json.MarshalIndent(jsonData, "", " ")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to marshal JSON: %w", err)
|
||||
}
|
||||
return string(jsonBytes), nil
|
||||
default:
|
||||
return "", fmt.Errorf("unsupported output format: %s", format)
|
||||
}
|
||||
}
|
||||
90
packages/tui/internal/format/format_test.go
Normal file
90
packages/tui/internal/format/format_test.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package format
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestOutputFormat_IsValid(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
format OutputFormat
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "text format",
|
||||
format: TextFormat,
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "json format",
|
||||
format: JSONFormat,
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "invalid format",
|
||||
format: "invalid",
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
if got := tt.format.IsValid(); got != tt.want {
|
||||
t.Errorf("OutputFormat.IsValid() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatOutput(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
content string
|
||||
format OutputFormat
|
||||
want string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "text format",
|
||||
content: "test content",
|
||||
format: TextFormat,
|
||||
want: "test content",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "json format",
|
||||
content: "test content",
|
||||
format: JSONFormat,
|
||||
want: "{\n \"response\": \"test content\"\n}",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "invalid format",
|
||||
content: "test content",
|
||||
format: "invalid",
|
||||
want: "",
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
got, err := FormatOutput(tt.content, tt.format)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("FormatOutput() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if got != tt.want {
|
||||
t.Errorf("FormatOutput() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user