68 lines
1.2 KiB
Go
68 lines
1.2 KiB
Go
package wand
|
|
|
|
import (
|
|
"code.squareroundforest.org/arpio/notation"
|
|
"fmt"
|
|
"io"
|
|
"reflect"
|
|
)
|
|
|
|
func printOutput(w io.Writer, o []any) error {
|
|
wraperr := func(err error) error {
|
|
return fmt.Errorf("error copying output: %w", err)
|
|
}
|
|
|
|
for _, oi := range o {
|
|
r, ok := oi.(io.Reader)
|
|
if ok {
|
|
if _, err := io.Copy(w, r); err != nil {
|
|
return wraperr(err)
|
|
}
|
|
|
|
continue
|
|
}
|
|
|
|
t := reflect.TypeOf(oi)
|
|
if t.Implements(reflect.TypeFor[fmt.Stringer]()) {
|
|
if _, err := fmt.Fprintln(w, oi); err != nil {
|
|
return wraperr(err)
|
|
}
|
|
|
|
continue
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|