1
1
mirror of https://github.com/wader/fq.git synced 2024-12-24 13:52:02 +03:00
fq/pkg/interp/preview.go

44 lines
1019 B
Go
Raw Normal View History

2020-06-08 03:29:51 +03:00
package interp
import (
"fmt"
"math/big"
2020-06-08 03:29:51 +03:00
"strconv"
"github.com/wader/fq/internal/mathex"
"github.com/wader/fq/internal/stringsex"
"github.com/wader/fq/pkg/bitio"
"github.com/wader/fq/pkg/scalar"
2020-06-08 03:29:51 +03:00
)
func previewValue(v any, df scalar.DisplayFormat) string {
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"
case int:
// TODO: DisplayFormat is weird
return mathex.PadFormatInt(int64(vv), df.FormatBase(), true, 0)
2020-06-08 03:29:51 +03:00
case int64:
// TODO: DisplayFormat is weird
return mathex.PadFormatInt(vv, df.FormatBase(), true, 0)
2020-06-08 03:29:51 +03:00
case uint64:
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:
return fmt.Sprintf("%q", stringsex.TrimN(vv, 50, "..."))
case nil:
return "null"
case bitio.Reader:
return "raw bits"
case *big.Int:
return mathex.PadFormatBigInt(vv, df.FormatBase(), true, 0)
2020-06-08 03:29:51 +03:00
default:
panic("unreachable")
}
}