Skip to content

Commit

Permalink
interp: Cleanup unfinished/broken preview
Browse files Browse the repository at this point in the history
  • Loading branch information
wader committed Oct 2, 2021
1 parent 5a5f01e commit b641c77
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 145 deletions.
3 changes: 2 additions & 1 deletion doc/TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
- Safe mode interpreter?
- Allow/deny `open` in autocomplete
- `open` leak, file and ctxreadseeker
- Summary tree with format specific summaries for each format, sample count etc etc?
- List all unique paths in some compact form?

### Tests

Expand Down Expand Up @@ -120,7 +122,6 @@

#### Scripts

- Summary script, tree with format specific summaries like codec, sample count etc etc?
- Probe tool with common field names
- MIME codec encode/decoder "avc1.PPCCLL" etc https://tools.ietf.org/html/rfc6381#section-3.3
- Validate scripts for mp4, matroska
Expand Down
19 changes: 0 additions & 19 deletions pkg/interp/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func (i *Interp) makeFunctions() []Function {

{[]string{"format"}, 0, 0, i.format, nil},
{[]string{"_display"}, 1, 1, nil, i._display},
{[]string{"preview", "p"}, 0, 1, nil, i.preview},

{[]string{"tobytes"}, 0, 0, i.toBytes, nil},
{[]string{"tobits"}, 0, 0, i.toBits, nil},
Expand Down Expand Up @@ -696,24 +695,6 @@ func (i *Interp) _display(c interface{}, a []interface{}) gojq.Iter {
}
}

// TODO: opts and colors?
func (i *Interp) preview(c interface{}, a []interface{}) gojq.Iter {
opts, err := i.Options(a...)
if err != nil {
return gojq.NewIter(err)
}

switch v := c.(type) {
case Preview:
if err := v.Preview(i.evalContext.output, opts); err != nil {
return gojq.NewIter(err)
}
return gojq.NewIter()
default:
return gojq.NewIter(fmt.Errorf("%v: not previewable", c))
}
}

func (i *Interp) toBytes(c interface{}, a []interface{}) interface{} {
bb, err := toBuffer(c)
if err != nil {
Expand Down
4 changes: 0 additions & 4 deletions pkg/interp/interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,6 @@ type Display interface {
Display(w io.Writer, opts Options) error
}

type Preview interface {
Preview(w io.Writer, opts Options) error
}

type ToBuffer interface {
ToBuffer() (*bitio.Buffer, error)
}
Expand Down
120 changes: 0 additions & 120 deletions pkg/interp/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,13 @@ package interp
import (
"encoding/hex"
"fmt"
"io"
"sort"
"strconv"
"strings"

"github.com/wader/fq/internal/num"
"github.com/wader/fq/pkg/bitio"
"github.com/wader/fq/pkg/decode"
)

type previewNode struct {
name string
pos int64
count int
previews map[string]struct{}
nodes map[string]*previewNode
}

func previewValue(v *decode.Value) string {
switch vv := v.V.(type) {
case decode.Array:
Expand Down Expand Up @@ -60,116 +49,7 @@ func previewValue(v *decode.Value) string {
return hex.EncodeToString(bs)
case nil:
return "none"

case []interface{}:
// TODO: remove?
return "json []"
case map[string]interface{}:
// TODO: remove?
return "json {}"
default:
panic("unreachable")
}
}

func preview(v *decode.Value, w io.Writer, _ Options) error {
switch v.V.(type) {
case decode.Struct:
sn := &previewNode{name: ".", count: -1, previews: map[string]struct{}{}, nodes: map[string]*previewNode{}}
err := previewEx(v, sn, 0)
if err != nil {
return err
}

var printFn func(s *previewNode, depth int)
printFn = func(s *previewNode, depth int) {
indent := strings.Repeat(" ", depth)
fmt.Fprint(w, indent)
fmt.Fprint(w, s.name)
if s.count != -1 {
fmt.Fprintf(w, "[%d]", s.count)
}

var sortedPreviews []string
for p := range s.previews {
sortedPreviews = append(sortedPreviews, p)
}
sort.Strings(sortedPreviews)
if len(sortedPreviews) > 10 {
sortedPreviews = sortedPreviews[0:10]
sortedPreviews = append(sortedPreviews, "...")
}
if len(sortedPreviews) > 0 {
fmt.Fprintf(w, " (%s)", strings.Join(sortedPreviews, ","))
}
fmt.Fprintln(w)

var sortedNodes []*previewNode
for _, n := range s.nodes {
sortedNodes = append(sortedNodes, n)
}
// sort.Slice(sorted, func(i, j int) bool {
// return sorted[i].name < sorted[j].name
// })
sort.Slice(sortedNodes, func(i, j int) bool {
return sortedNodes[i].pos < sortedNodes[j].pos
})

for _, n := range sortedNodes {
printFn(n, depth+1)
}
}

printFn(sn, 0)
default:
fmt.Fprintln(w, previewValue(v))
}
return nil
}

func previewEx(v *decode.Value, n *previewNode, depth int) error {

if _, ok := v.V.(decode.Array); !ok {
p := previewValue(v)
if p != "" {
if _, ok := n.previews[p]; !ok {
n.previews[p] = struct{}{}
}
}
}

switch vv := v.V.(type) {
case decode.Struct:
for _, sv := range vv {
sn, ok := n.nodes[sv.Name]
if !ok {
sn = &previewNode{
name: sv.Name,
pos: sv.Range.Start,
count: -1,
previews: map[string]struct{}{},
nodes: map[string]*previewNode{},
}
if a, ok := sv.V.(decode.Array); ok {
sn.count = len(a)
}
n.nodes[sv.Name] = sn
}

err := previewEx(sv, sn, depth+1)
if err != nil {
return err
}
}
case decode.Array:
for _, av := range vv {
err := previewEx(av, n, depth+1)
_ = n
if err != nil {
return err
}
}
}

return nil
}
1 change: 0 additions & 1 deletion pkg/interp/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ func (dvb decodeValueBase) DisplayName() string {
}

func (dvb decodeValueBase) Display(w io.Writer, opts Options) error { return dump(dvb.dv, w, opts) }
func (dvb decodeValueBase) Preview(w io.Writer, opts Options) error { return preview(dvb.dv, w, opts) }
func (dvb decodeValueBase) ToBuffer() (*bitio.Buffer, error) {
return dvb.dv.RootBitBuf.Copy().BitBufRange(dvb.dv.Range.Start, dvb.dv.Range.Len)
}
Expand Down

0 comments on commit b641c77

Please sign in to comment.