1
1
mirror of https://github.com/wader/fq.git synced 2024-12-01 02:30:32 +03:00
fq/pkg/interp/preview.go
Mattias Wadman 1b32b42f93 decode: Major decode API refactor
Generate more code
More generic and comfortable API
Improve and Update format decoder to new API
Add some more format tests
2021-11-15 21:12:07 +01:00

47 lines
1.1 KiB
Go

package interp
import (
"encoding/hex"
"fmt"
"strconv"
"github.com/wader/fq/internal/num"
"github.com/wader/fq/pkg/bitio"
"github.com/wader/fq/pkg/decode"
)
func previewValue(v interface{}, df decode.DisplayFormat) string {
switch vv := v.(type) {
case bool:
if vv {
return "true"
}
return "false"
case int:
// TODO: DisplayFormat is weird
return num.PadFormatInt(int64(vv), decode.DisplayFormatToBase(df), true, 0)
case int64:
// TODO: DisplayFormat is weird
return num.PadFormatInt(vv, decode.DisplayFormatToBase(df), true, 0)
case uint64:
return num.PadFormatUint(vv, decode.DisplayFormatToBase(df), true, 0)
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]) + "..."
}
return fmt.Sprintf("%q", vv)
case []byte:
if len(vv) > 16 {
return hex.EncodeToString(vv[0:16]) + "..."
}
return hex.EncodeToString(vv)
case *bitio.Buffer:
return "raw bits"
default:
panic("unreachable")
}
}