Added custom printers
This commit is contained in:
parent
62724e3f37
commit
7a92eeddac
7 changed files with 287 additions and 63 deletions
20
repl/typeprinter.go
Normal file
20
repl/typeprinter.go
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
package repl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func AddTypePrinter[T any](r *REPL, p func(w io.Writer, t T, concise bool) error) {
|
||||
var t T
|
||||
|
||||
tt := reflect.TypeOf(t)
|
||||
r.typePrinters[tt] = func(w io.Writer, v any, concise bool) error {
|
||||
vt, ok := v.(T)
|
||||
if !ok {
|
||||
return fmt.Errorf("cannot convert %v to T", v)
|
||||
}
|
||||
return p(w, vt, concise)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue