From ddfec6d58a93797ee1ca0558ed346fe869508e0d5e07bea2f7210d189e50ce0f Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Fri, 21 Feb 2025 17:09:52 +0300 Subject: [PATCH] Ability to extract more or less raw values --- go/cmd/pp/colour.go | 15 ++++-- go/cmd/pp/main.go | 114 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 103 insertions(+), 26 deletions(-) diff --git a/go/cmd/pp/colour.go b/go/cmd/pp/colour.go index 85ca085..f33af83 100644 --- a/go/cmd/pp/colour.go +++ b/go/cmd/pp/colour.go @@ -2,7 +2,6 @@ package main import ( "bytes" - "os" "golang.org/x/term" ) @@ -18,10 +17,18 @@ var ( Reset string ) +func noColour() { + Yellow = "" + Green = "" + Blue = "" + Cyan = "" + Red = "" + Magenta = "" + White = "" + Reset = "" +} + func init() { - if os.Getenv("NO_COLOR") != "" { - return - } var b bytes.Buffer t := term.NewTerminal(&b, "") Yellow = string(t.Escape.Yellow) diff --git a/go/cmd/pp/main.go b/go/cmd/pp/main.go index 5d0d2b6..a53f397 100644 --- a/go/cmd/pp/main.go +++ b/go/cmd/pp/main.go @@ -23,6 +23,7 @@ import ( "io" "log" "os" + "slices" "strconv" "strings" "time" @@ -33,10 +34,15 @@ import ( ) 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) { @@ -54,16 +60,30 @@ func printbin(s []byte) { 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() @@ -71,25 +91,49 @@ func printer(iter *keks.Iterator, count int, inList, inMap bool) { 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: @@ -108,7 +152,16 @@ func printer(iter *keks.Iterator, count int, inList, inMap 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) @@ -156,7 +209,9 @@ func printer(iter *keks.Iterator, count int, inList, inMap bool) { 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) { @@ -165,7 +220,10 @@ func printer(iter *keks.Iterator, count int, inList, inMap bool) { } 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: @@ -176,6 +234,16 @@ func printer(iter *keks.Iterator, count int, inList, inMap bool) { 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 @@ -189,8 +257,10 @@ func main() { } 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") + } } -- 2.48.1