2020-06-08 03:29:51 +03:00
|
|
|
package interp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2021-08-17 13:06:32 +03:00
|
|
|
|
|
|
|
"github.com/wader/fq/internal/num"
|
|
|
|
"github.com/wader/fq/pkg/bitio"
|
|
|
|
"github.com/wader/fq/pkg/decode"
|
2020-06-08 03:29:51 +03:00
|
|
|
)
|
|
|
|
|
2021-11-03 19:19:33 +03:00
|
|
|
func previewValue(v interface{}, df decode.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"
|
2020-06-08 03:29:51 +03:00
|
|
|
case int64:
|
|
|
|
// TODO: DisplayFormat is weird
|
2021-11-03 19:19:33 +03:00
|
|
|
return num.PadFormatInt(vv, decode.DisplayFormatToBase(df), true, 0)
|
2020-06-08 03:29:51 +03:00
|
|
|
case uint64:
|
2021-11-03 19:19:33 +03:00
|
|
|
return num.PadFormatUint(vv, decode.DisplayFormatToBase(df), 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)
|
2020-06-08 03:29:51 +03:00
|
|
|
case []byte:
|
|
|
|
if len(vv) > 16 {
|
|
|
|
return hex.EncodeToString(vv[0:16]) + "..."
|
|
|
|
}
|
2021-09-27 12:01:14 +03:00
|
|
|
return hex.EncodeToString(vv)
|
2020-06-08 03:29:51 +03:00
|
|
|
case *bitio.Buffer:
|
|
|
|
vvLen := vv.Len()
|
|
|
|
if vvLen > 16*8 {
|
|
|
|
bs, _ := vv.BytesRange(0, 16)
|
|
|
|
return hex.EncodeToString(bs) + "..."
|
|
|
|
}
|
2021-09-27 12:01:14 +03:00
|
|
|
bs, _ := vv.BytesRange(0, int(bitio.BitsByteCount(vvLen)))
|
|
|
|
return hex.EncodeToString(bs)
|
2020-06-08 03:29:51 +03:00
|
|
|
case nil:
|
|
|
|
return "none"
|
|
|
|
default:
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
}
|