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-08-12 16:27:51 +03:00
|
|
|
"github.com/wader/fq/internal/mathex"
|
|
|
|
"github.com/wader/fq/internal/stringsex"
|
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
|
|
|
)
|
|
|
|
|
2022-05-20 16:10:41 +03:00
|
|
|
func previewValue(v any, 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-08-12 16:27:51 +03:00
|
|
|
return mathex.PadFormatInt(int64(vv), df.FormatBase(), true, 0)
|
2020-06-08 03:29:51 +03:00
|
|
|
case int64:
|
|
|
|
// TODO: DisplayFormat is weird
|
2022-08-12 16:27:51 +03:00
|
|
|
return mathex.PadFormatInt(vv, df.FormatBase(), true, 0)
|
2020-06-08 03:29:51 +03:00
|
|
|
case uint64:
|
2022-08-12 16:27:51 +03:00
|
|
|
return mathex.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:
|
2022-08-12 16:27:51 +03:00
|
|
|
return fmt.Sprintf("%q", stringsex.TrimN(vv, 50, "..."))
|
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-08-12 16:27:51 +03:00
|
|
|
return mathex.PadFormatBigInt(vv, df.FormatBase(), true, 0)
|
2020-06-08 03:29:51 +03:00
|
|
|
default:
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
}
|