]> Cypherpunks repositories - keks.git/commitdiff
Ability to extract more or less raw values
authorSergey Matveev <stargrave@stargrave.org>
Fri, 21 Feb 2025 14:09:52 +0000 (17:09 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Fri, 21 Feb 2025 14:41:21 +0000 (17:41 +0300)
go/cmd/pp/colour.go
go/cmd/pp/main.go

index 85ca0857d84dd16386c7b6588391702d7def31725209f4dcb16ee2f6e612886b..f33af8350a08178c92e1a457645a98e0568b21cfce66fc8095f3f6b091268bf7 100644 (file)
@@ -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)
index 5d0d2b63f7994153b076cab2c2fc243e71f69a8c4a11dfb2d0665200b9664a4c..a53f397e7ac61e65b95e29c96919cc476557f2c7ff558bca3cadb31e1f2223dc 100644 (file)
@@ -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")
+       }
 }