2020-06-08 03:29:51 +03:00
|
|
|
package interp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-01-14 03:58:42 +03:00
|
|
|
"math/big"
|
2020-06-08 03:29:51 +03:00
|
|
|
"strconv"
|
2021-08-17 13:06:32 +03:00
|
|
|
|
2022-01-15 20:55:57 +03:00
|
|
|
"github.com/wader/fq/internal/mathextra"
|
2021-08-17 13:06:32 +03:00
|
|
|
"github.com/wader/fq/pkg/bitio"
|
2021-12-02 00:48:25 +03:00
|
|
|
"github.com/wader/fq/pkg/scalar"
|
2020-06-08 03:29:51 +03:00
|
|
|
)
|
|
|
|
|
2021-12-02 00:48:25 +03:00
|
|
|
func previewValue(v interface{}, df scalar.DisplayFormat) string {
|
2021-11-03 19:19:33 +03:00
|
|
|
switch vv := v.(type) {
|
2020-06-08 03:29:51 +03:00
|
|
|
case bool:
|
|
|
|
if vv {
|
|
|
|
return "true"
|
|
|
|
}
|
2021-09-27 12:01:14 +03:00
|
|
|
return "false"
|
2021-11-05 17:04:26 +03:00
|
|
|
case int:
|
|
|
|
// TODO: DisplayFormat is weird
|
2022-01-15 20:55:57 +03:00
|
|
|
return mathextra.PadFormatInt(int64(vv), df.FormatBase(), true, 0)
|
2020-06-08 03:29:51 +03:00
|
|
|
case int64:
|
|
|
|
// TODO: DisplayFormat is weird
|
2022-01-15 20:55:57 +03:00
|
|
|
return mathextra.PadFormatInt(vv, df.FormatBase(), true, 0)
|
2020-06-08 03:29:51 +03:00
|
|
|
case uint64:
|
2022-01-15 20:55:57 +03:00
|
|
|
return mathextra.PadFormatUint(vv, df.FormatBase(), true, 0)
|
2020-06-08 03:29:51 +03:00
|
|
|
case float64:
|
|
|
|
// TODO: float32? better truncated to significant digits?
|
|
|
|
return strconv.FormatFloat(vv, 'g', -1, 64)
|
|
|
|
case string:
|
|
|
|
if len(vv) > 50 {
|
|
|
|
return fmt.Sprintf("%q", vv[0:50]) + "..."
|
|
|
|
}
|
2021-09-27 12:01:14 +03:00
|
|
|
return fmt.Sprintf("%q", vv)
|
2022-01-17 12:49:43 +03:00
|
|
|
case nil:
|
|
|
|
return "null"
|
2022-01-24 23:21:48 +03:00
|
|
|
case bitio.Reader:
|
2021-11-05 17:04:26 +03:00
|
|
|
return "raw bits"
|
2022-01-14 03:58:42 +03:00
|
|
|
case *big.Int:
|
2022-01-15 20:55:57 +03:00
|
|
|
return mathextra.PadFormatBigInt(vv, df.FormatBase(), true, 0)
|
2020-06-08 03:29:51 +03:00
|
|
|
default:
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
}
|