"io"
"log"
"os"
+ "slices"
"strconv"
"strings"
"time"
)
var (
- MaxStrLen = flag.Uint("max-str-len", 64,
+ MaxStrLen = flag.Uint("max-str-len", 64,
"Maximal string length to print")
MaxParseCycles = flag.Uint64("max-parse-cycles", 1<<63,
"Maximal number of parse cycles to perform")
+ NoTotals = flag.Bool("no-totals", false, "Do not print how many bytes read")
+ onlyV = flag.Bool("v", false, "Print only values")
+ pthStr = flag.String("p", "", "Print only /that/keks/path")
+
+ pth []string
)
func prindent(depth int) {
s = s[:int(*MaxStrLen)]
dots = "..."
}
- fmt.Printf("%s%d:%s%s%s\n", Magenta, sLen, Reset, hexenc(s), dots)
+ if *onlyV {
+ fmt.Println(hexenc(s))
+ } else {
+ fmt.Printf("%s%d:%s%s%s\n", Magenta, sLen, Reset, hexenc(s), dots)
+ }
+}
+
+func isQuiet(where []string) bool {
+ if len(pth) == 0 {
+ return false
+ }
+ if len(pth) > len(where) {
+ return true
+ }
+ return !slices.Equal(where[:len(pth)], pth)
}
-func printer(iter *keks.Iterator, count int, inList, inMap bool) {
+func printer(iter *keks.Iterator, where []string, count int, inList, inMap bool) {
+ var quiet bool
for i := range count {
if !iter.Next() {
panic("unexpected")
}
depth := iter.Depth
- fmt.Print(Blue + strconv.Itoa(depth) + Reset)
var key string
if inMap {
key = iter.Str()
panic("unexpected")
}
}
- fmt.Printf(" %s%03d%s ", Red, iter.Offset(), Reset)
- prindent(depth)
if inList {
- fmt.Printf("%s%d:%s ", Yellow, i, Reset)
+ where[len(where)-1] = strconv.Itoa(i)
} else if inMap {
- fmt.Printf("%s%s:%s ", Green, key, Reset)
+ where[len(where)-1] = key
}
+ quiet = isQuiet(where)
+ if !quiet && !*onlyV {
+ fmt.Print(Blue + strconv.Itoa(depth) + Reset)
+ fmt.Printf(" %s%03d%s ", Red, iter.Offset(), Reset)
+ prindent(depth)
+ if inList {
+ fmt.Printf("%s%d:%s ", Yellow, i, Reset)
+ } else if inMap {
+ fmt.Printf("%s%s:%s ", Green, key, Reset)
+ }
+ }
+ var listOrMap bool
switch iter.T {
case types.List:
- fmt.Printf("[ %s%d%s\n", Cyan, iter.Len(), Reset)
- printer(iter, iter.Len(), true, false)
- prindent(depth)
- fmt.Println(" " + " ]")
+ listOrMap = true
+ if !quiet && !*onlyV {
+ fmt.Printf("[ %s%d%s\n", Cyan, iter.Len(), Reset)
+ }
+ printer(iter, append(where, "0"), iter.Len(), true, false)
+ if !quiet && !*onlyV {
+ prindent(depth)
+ fmt.Println(" " + " ]")
+ }
case types.Map:
- fmt.Printf("{ %s%d%s\n", Cyan, iter.Len(), Reset)
- printer(iter, iter.Len(), false, true)
- prindent(depth)
- fmt.Println(" " + " }")
-
+ listOrMap = true
+ if !quiet && !*onlyV {
+ fmt.Printf("{ %s%d%s\n", Cyan, iter.Len(), Reset)
+ }
+ printer(iter, append(where, key), iter.Len(), false, true)
+ if !quiet && !*onlyV {
+ prindent(depth)
+ fmt.Println(" " + " }")
+ }
+ }
+ if listOrMap || quiet {
+ continue
+ }
+ switch iter.T {
case types.NIL:
fmt.Println("NIL")
case types.Bool:
fmt.Println(iter.BigInt())
case types.Blob:
blob := iter.Blob()
- fmt.Printf("BLOB[ %s%d l=%d%s\n", Cyan, len(blob.Chunks), blob.ChunkLen, Reset)
+ if *onlyV {
+ v, err := io.ReadAll(blob.Reader())
+ if err != nil {
+ log.Fatal(err)
+ }
+ fmt.Println(hex.EncodeToString(v))
+ break
+ }
+ fmt.Printf("BLOB[ %s%d l=%d%s\n",
+ Cyan, len(blob.Chunks), blob.ChunkLen, Reset)
off := iter.Offset() + 1 + 8
for i, chunk := range blob.Chunks {
fmt.Print(Blue + strconv.Itoa(depth+1) + Reset)
case types.Bin:
printbin(iter.Bin())
case types.Str:
- fmt.Print(`"`)
+ if !*onlyV {
+ fmt.Print(`"`)
+ }
s := iter.Str()
sLen := len(s)
if sLen > int(*MaxStrLen) {
} else {
fmt.Print(s)
}
- fmt.Println(`"`)
+ if !*onlyV {
+ fmt.Print(`"`)
+ }
+ fmt.Println("")
case types.Raw:
fmt.Printf("(l=%d v=%s)\n", len(iter.Raw()), hexenc(iter.Raw()))
default:
func main() {
flag.Parse()
+ if *pthStr != "" {
+ pth = strings.Split(strings.TrimLeft(*pthStr, "/"), "/")
+ *NoTotals = true
+ }
+ if *onlyV {
+ *MaxStrLen = 1 << 62
+ }
+ if *onlyV || os.Getenv("NO_COLOR") != "" {
+ noColour()
+ }
br := bufio.NewReader(os.Stdin)
var off int64
var ctx *keks.Decoder
}
log.Fatal(err)
}
- printer(ctx.Iter(), 1, false, false)
+ printer(ctx.Iter(), []string{}, 1, false, false)
off = ctx.Read
}
- fmt.Println(off, "bytes")
+ if !*NoTotals {
+ fmt.Println(off, "bytes")
+ }
}