wand/output.go

68 lines
1.2 KiB
Go
Raw Normal View History

2025-08-18 14:24:31 +02:00
package wand
import (
2025-08-24 04:46:54 +02:00
"code.squareroundforest.org/arpio/notation"
2025-08-18 14:24:31 +02:00
"fmt"
"io"
2025-08-24 04:46:54 +02:00
"reflect"
2025-08-18 14:24:31 +02:00
)
func printOutput(w io.Writer, o []any) error {
2025-08-24 04:46:54 +02:00
wraperr := func(err error) error {
return fmt.Errorf("error copying output: %w", err)
}
2025-08-18 14:24:31 +02:00
for _, oi := range o {
r, ok := oi.(io.Reader)
if ok {
if _, err := io.Copy(w, r); err != nil {
2025-08-24 04:46:54 +02:00
return wraperr(err)
2025-08-18 14:24:31 +02:00
}
continue
}
2025-08-24 04:46:54 +02:00
t := reflect.TypeOf(oi)
if t.Implements(reflect.TypeFor[fmt.Stringer]()) {
if _, err := fmt.Fprintln(w, oi); err != nil {
return wraperr(err)
}
2025-08-26 05:01:56 +02:00
continue
2025-08-24 04:46:54 +02:00
}
t = unpack(t, reflect.Pointer)
switch t.Kind() {
case reflect.Bool,
reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64,
reflect.Uintptr,
reflect.Float32,
reflect.Float64,
reflect.String,
reflect.UnsafePointer:
if _, err := fmt.Fprintln(w, oi); err != nil {
return wraperr(err)
}
default:
if _, err := notation.Fprintwt(w, oi); err != nil {
return wraperr(err)
}
if _, err := fmt.Fprintln(w); err != nil {
return wraperr(err)
}
2025-08-18 14:24:31 +02:00
}
}
return nil
}