2021-04-23 18:35:28 +02:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
json "encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
"os"
|
2021-04-05 23:30:48 +02:00
|
|
|
|
"sort"
|
2021-12-18 14:03:15 +01:00
|
|
|
|
"strings"
|
2021-04-23 18:35:28 +02:00
|
|
|
|
|
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
|
lipgloss "github.com/charmbracelet/lipgloss"
|
|
|
|
|
// termenv "github.com/muesli/termenv"
|
|
|
|
|
// isatty "github.com/mattn/go-isatty"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Keeps the full data structure and a path that indexes our current position into it.
|
|
|
|
|
type model struct {
|
2021-12-18 14:03:15 +01:00
|
|
|
|
path []index
|
|
|
|
|
data val
|
2021-04-23 18:35:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// an index into a value, uint for lists and string for maps.
|
|
|
|
|
// nil for any scalar value.
|
|
|
|
|
// TODO: use an actual interface for these
|
|
|
|
|
type index interface{}
|
|
|
|
|
|
|
|
|
|
/// recursive value that we can represent.
|
|
|
|
|
type val struct {
|
|
|
|
|
// the “type” of value; see tag const belove
|
|
|
|
|
tag tag
|
2021-04-06 02:06:01 +02:00
|
|
|
|
// last known position of our cursor
|
|
|
|
|
last_index index
|
2021-04-23 18:35:28 +02:00
|
|
|
|
// documentation (TODO)
|
|
|
|
|
doc string
|
|
|
|
|
// the actual value;
|
2021-04-06 02:06:01 +02:00
|
|
|
|
// the actual structure is behind a pointer so we can replace the struct.
|
2021-04-23 18:35:28 +02:00
|
|
|
|
// determined by the tag
|
2021-04-06 02:06:01 +02:00
|
|
|
|
// tagString -> *string
|
|
|
|
|
// tagFloat -> *float64
|
|
|
|
|
// tagList -> *[]val
|
|
|
|
|
// tagMap -> *map[string]val
|
2021-04-23 18:35:28 +02:00
|
|
|
|
val interface{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type tag string
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
tagString tag = "string"
|
|
|
|
|
tagFloat tag = "float"
|
|
|
|
|
tagList tag = "list"
|
2021-04-05 23:30:48 +02:00
|
|
|
|
tagMap tag = "map"
|
2021-04-23 18:35:28 +02:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// print a value, flat
|
|
|
|
|
func (v val) Render() string {
|
|
|
|
|
s := ""
|
|
|
|
|
switch v.tag {
|
|
|
|
|
case tagString:
|
2021-04-06 02:06:01 +02:00
|
|
|
|
s += *v.val.(*string)
|
2021-04-23 18:35:28 +02:00
|
|
|
|
case tagFloat:
|
2021-04-06 02:06:01 +02:00
|
|
|
|
s += fmt.Sprint(*v.val.(*float64))
|
2021-04-23 18:35:28 +02:00
|
|
|
|
case tagList:
|
|
|
|
|
s += "[ "
|
|
|
|
|
vs := []string{}
|
2021-04-05 23:30:48 +02:00
|
|
|
|
for _, enum := range v.enumerate() {
|
|
|
|
|
vs = append(vs, enum.v.Render())
|
2021-04-23 18:35:28 +02:00
|
|
|
|
}
|
|
|
|
|
s += strings.Join(vs, ", ")
|
|
|
|
|
s += " ]"
|
2021-04-05 23:30:48 +02:00
|
|
|
|
case tagMap:
|
|
|
|
|
s += "{ "
|
|
|
|
|
vs := []string{}
|
|
|
|
|
for _, enum := range v.enumerate() {
|
|
|
|
|
vs = append(vs, fmt.Sprintf("%s: %s", enum.i.(string), enum.v.Render()))
|
|
|
|
|
}
|
|
|
|
|
s += strings.Join(vs, ", ")
|
|
|
|
|
s += " }"
|
2021-04-23 18:35:28 +02:00
|
|
|
|
default:
|
|
|
|
|
s += fmt.Sprintf("<unknown: %v>", v)
|
|
|
|
|
}
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// render an index, depending on the type
|
|
|
|
|
func renderIndex(i index) (s string) {
|
|
|
|
|
switch i := i.(type) {
|
|
|
|
|
case nil:
|
|
|
|
|
s = ""
|
|
|
|
|
// list index
|
|
|
|
|
case uint:
|
|
|
|
|
s = "*"
|
|
|
|
|
// map index
|
|
|
|
|
case string:
|
|
|
|
|
s = i + ":"
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// take an arbitrary (within restrictions) go value and construct a val from it
|
|
|
|
|
func makeVal(i interface{}) val {
|
|
|
|
|
var v val
|
|
|
|
|
switch i := i.(type) {
|
|
|
|
|
case string:
|
|
|
|
|
v = val{
|
2021-12-18 14:03:15 +01:00
|
|
|
|
tag: tagString,
|
2021-04-06 02:06:01 +02:00
|
|
|
|
last_index: index(nil),
|
2021-12-18 14:03:15 +01:00
|
|
|
|
doc: "",
|
|
|
|
|
val: &i,
|
2021-04-23 18:35:28 +02:00
|
|
|
|
}
|
|
|
|
|
case float64:
|
|
|
|
|
v = val{
|
2021-12-18 14:03:15 +01:00
|
|
|
|
tag: tagFloat,
|
2021-04-06 02:06:01 +02:00
|
|
|
|
last_index: index(nil),
|
2021-12-18 14:03:15 +01:00
|
|
|
|
doc: "",
|
|
|
|
|
val: &i,
|
2021-04-23 18:35:28 +02:00
|
|
|
|
}
|
|
|
|
|
case []interface{}:
|
|
|
|
|
ls := []val{}
|
|
|
|
|
for _, i := range i {
|
|
|
|
|
ls = append(ls, makeVal(i))
|
|
|
|
|
}
|
|
|
|
|
v = val{
|
2021-12-18 14:03:15 +01:00
|
|
|
|
tag: tagList,
|
2021-04-06 02:06:01 +02:00
|
|
|
|
last_index: pos1Inner(tagList, &ls),
|
2021-12-18 14:03:15 +01:00
|
|
|
|
doc: "",
|
|
|
|
|
val: &ls,
|
2021-04-23 18:35:28 +02:00
|
|
|
|
}
|
2021-04-05 23:30:48 +02:00
|
|
|
|
case map[string]interface{}:
|
|
|
|
|
ls := map[string]val{}
|
|
|
|
|
for k, i := range i {
|
|
|
|
|
ls[k] = makeVal(i)
|
|
|
|
|
}
|
|
|
|
|
v = val{
|
2021-12-18 14:03:15 +01:00
|
|
|
|
tag: tagMap,
|
2021-04-06 02:06:01 +02:00
|
|
|
|
last_index: pos1Inner(tagMap, &ls),
|
2021-12-18 14:03:15 +01:00
|
|
|
|
doc: "",
|
|
|
|
|
val: &ls,
|
2021-04-05 23:30:48 +02:00
|
|
|
|
}
|
2021-04-23 18:35:28 +02:00
|
|
|
|
default:
|
|
|
|
|
log.Fatalf("makeVal: cannot read json of type %T", i)
|
|
|
|
|
}
|
|
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// return an index that points at the first entry in val
|
|
|
|
|
func (v val) pos1() index {
|
2021-04-05 23:30:48 +02:00
|
|
|
|
return v.enumerate()[0].i
|
2021-04-23 18:35:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-06 02:06:01 +02:00
|
|
|
|
func pos1Inner(tag tag, v interface{}) index {
|
|
|
|
|
return enumerateInner(tag, v)[0].i
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-23 18:35:28 +02:00
|
|
|
|
type enumerate struct {
|
|
|
|
|
i index
|
|
|
|
|
v val
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// enumerate gives us a stable ordering of elements in this val.
|
|
|
|
|
// for scalars it’s just a nil index & the val itself.
|
|
|
|
|
// Guaranteed to always return at least one element.
|
|
|
|
|
func (v val) enumerate() (e []enumerate) {
|
2021-04-06 00:30:32 +02:00
|
|
|
|
e = enumerateInner(v.tag, v.val)
|
|
|
|
|
if e == nil {
|
|
|
|
|
e = append(e, enumerate{
|
|
|
|
|
i: nil,
|
|
|
|
|
v: v,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// like enumerate, but returns an empty slice for scalars without inner vals.
|
|
|
|
|
func enumerateInner(tag tag, v interface{}) (e []enumerate) {
|
|
|
|
|
switch tag {
|
2021-04-23 18:35:28 +02:00
|
|
|
|
case tagString:
|
|
|
|
|
fallthrough
|
|
|
|
|
case tagFloat:
|
2021-04-06 00:30:32 +02:00
|
|
|
|
e = nil
|
2021-04-23 18:35:28 +02:00
|
|
|
|
case tagList:
|
2021-04-06 02:06:01 +02:00
|
|
|
|
for i, v := range *v.(*[]val) {
|
2021-04-23 18:35:28 +02:00
|
|
|
|
e = append(e, enumerate{i: index(uint(i)), v: v})
|
|
|
|
|
}
|
2021-04-05 23:30:48 +02:00
|
|
|
|
case tagMap:
|
|
|
|
|
// map sorting order is not stable (actually randomized thank jabber)
|
|
|
|
|
// so let’s sort them
|
|
|
|
|
keys := []string{}
|
2021-04-06 02:06:01 +02:00
|
|
|
|
m := *v.(*map[string]val)
|
2021-04-05 23:30:48 +02:00
|
|
|
|
for k, _ := range m {
|
|
|
|
|
keys = append(keys, k)
|
|
|
|
|
}
|
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
for _, k := range keys {
|
|
|
|
|
e = append(e, enumerate{i: index(k), v: m[k]})
|
|
|
|
|
}
|
2021-04-23 18:35:28 +02:00
|
|
|
|
default:
|
2021-04-06 00:30:32 +02:00
|
|
|
|
log.Fatalf("unknown val tag %s, %v", tag, v)
|
2021-04-23 18:35:28 +02:00
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m model) PathString() string {
|
|
|
|
|
s := "/ "
|
|
|
|
|
var is []string
|
|
|
|
|
for _, v := range m.path {
|
|
|
|
|
is = append(is, fmt.Sprintf("%v", v))
|
|
|
|
|
}
|
|
|
|
|
s += strings.Join(is, " / ")
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// walk the given path down in data, to get the value at that point.
|
|
|
|
|
// Assumes that all path indexes are valid indexes into data.
|
2021-04-06 02:06:01 +02:00
|
|
|
|
// Returns a pointer to the value at point, in order to be able to change it.
|
|
|
|
|
func walk(data *val, path []index) (*val, bool, error) {
|
|
|
|
|
res := data
|
2021-04-23 18:35:28 +02:00
|
|
|
|
atPath := func(index int) string {
|
|
|
|
|
return fmt.Sprintf("at path %v", path[:index+1])
|
|
|
|
|
}
|
|
|
|
|
errf := func(ty string, val interface{}, index int) error {
|
|
|
|
|
return fmt.Errorf("walk: can’t walk into %s %v %s", ty, val, atPath(index))
|
|
|
|
|
}
|
|
|
|
|
for i, p := range path {
|
2021-04-06 02:06:01 +02:00
|
|
|
|
switch res.tag {
|
2021-04-23 18:35:28 +02:00
|
|
|
|
case tagString:
|
2021-04-06 02:06:01 +02:00
|
|
|
|
return nil, true, nil
|
2021-04-23 18:35:28 +02:00
|
|
|
|
case tagFloat:
|
2021-04-06 02:06:01 +02:00
|
|
|
|
return nil, true, nil
|
2021-04-23 18:35:28 +02:00
|
|
|
|
case tagList:
|
|
|
|
|
switch p := p.(type) {
|
|
|
|
|
case uint:
|
2021-04-06 02:06:01 +02:00
|
|
|
|
list := *res.val.(*[]val)
|
2021-04-23 18:35:28 +02:00
|
|
|
|
if int(p) >= len(list) || p < 0 {
|
2021-04-06 02:06:01 +02:00
|
|
|
|
return nil, false, fmt.Errorf("index out of bounds %s", atPath(i))
|
2021-04-23 18:35:28 +02:00
|
|
|
|
}
|
2021-04-06 02:06:01 +02:00
|
|
|
|
res = &list[p]
|
2021-04-23 18:35:28 +02:00
|
|
|
|
default:
|
2021-04-06 02:06:01 +02:00
|
|
|
|
return nil, false, fmt.Errorf("not a list index %s", atPath(i))
|
2021-04-23 18:35:28 +02:00
|
|
|
|
}
|
2021-04-05 23:30:48 +02:00
|
|
|
|
case tagMap:
|
|
|
|
|
switch p := p.(type) {
|
|
|
|
|
case string:
|
2021-04-06 02:06:01 +02:00
|
|
|
|
m := *res.val.(*map[string]val)
|
|
|
|
|
if a, ok := m[p]; ok {
|
|
|
|
|
res = &a
|
|
|
|
|
} else {
|
2021-12-18 14:03:15 +01:00
|
|
|
|
return nil, false, fmt.Errorf("index %s not in map %s", p, atPath(i))
|
2021-04-05 23:30:48 +02:00
|
|
|
|
}
|
|
|
|
|
default:
|
2021-04-06 02:06:01 +02:00
|
|
|
|
return nil, false, fmt.Errorf("not a map index %v %s", p, atPath(i))
|
2021-04-05 23:30:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-23 18:35:28 +02:00
|
|
|
|
default:
|
2021-04-06 02:06:01 +02:00
|
|
|
|
return nil, false, errf(string(res.tag), res.val, i)
|
2021-04-23 18:35:28 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-06 02:06:01 +02:00
|
|
|
|
return res, false, nil
|
2021-04-23 18:35:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// descend into the selected index. Assumes that the index is valid.
|
|
|
|
|
// Will not descend into scalars.
|
|
|
|
|
func (m model) descend() (model, error) {
|
2021-04-06 02:06:01 +02:00
|
|
|
|
// TODO: two walks?!
|
|
|
|
|
this, _, err := walk(&m.data, m.path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return m, err
|
|
|
|
|
}
|
|
|
|
|
newPath := append(m.path, this.last_index)
|
|
|
|
|
_, bounce, err := walk(&m.data, newPath)
|
2021-04-05 23:30:48 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return m, err
|
|
|
|
|
}
|
|
|
|
|
// only descend if we *can*
|
|
|
|
|
if !bounce {
|
2021-04-23 18:35:28 +02:00
|
|
|
|
m.path = newPath
|
|
|
|
|
}
|
|
|
|
|
return m, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ascend to one level up. stops at the root.
|
|
|
|
|
func (m model) ascend() (model, error) {
|
|
|
|
|
if len(m.path) > 0 {
|
|
|
|
|
m.path = m.path[:len(m.path)-1]
|
2021-04-06 02:06:01 +02:00
|
|
|
|
_, _, err := walk(&m.data, m.path)
|
2021-04-23 18:35:28 +02:00
|
|
|
|
return m, err
|
|
|
|
|
}
|
|
|
|
|
return m, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// go to the next item, or wraparound
|
|
|
|
|
func (min model) next() (m model, err error) {
|
|
|
|
|
m = min
|
2021-04-06 02:06:01 +02:00
|
|
|
|
this, _, err := walk(&m.data, m.path)
|
2021-04-05 23:30:48 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-04-23 18:35:28 +02:00
|
|
|
|
enumL := this.enumerate()
|
|
|
|
|
setNext := false
|
|
|
|
|
for _, enum := range enumL {
|
|
|
|
|
if setNext {
|
2021-04-06 02:06:01 +02:00
|
|
|
|
this.last_index = enum.i
|
2021-04-23 18:35:28 +02:00
|
|
|
|
setNext = false
|
|
|
|
|
break
|
|
|
|
|
}
|
2021-04-06 02:06:01 +02:00
|
|
|
|
if enum.i == this.last_index {
|
2021-04-23 18:35:28 +02:00
|
|
|
|
setNext = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// wraparound
|
|
|
|
|
if setNext {
|
2021-04-06 02:06:01 +02:00
|
|
|
|
this.last_index = enumL[0].i
|
2021-04-23 18:35:28 +02:00
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// go to the previous item, or wraparound
|
|
|
|
|
func (min model) prev() (m model, err error) {
|
|
|
|
|
m = min
|
2021-04-06 02:06:01 +02:00
|
|
|
|
this, _, err := walk(&m.data, m.path)
|
2021-04-05 23:30:48 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-04-23 18:35:28 +02:00
|
|
|
|
enumL := this.enumerate()
|
|
|
|
|
// last element, wraparound
|
|
|
|
|
prevIndex := enumL[len(enumL)-1].i
|
|
|
|
|
for _, enum := range enumL {
|
2021-04-06 02:06:01 +02:00
|
|
|
|
if enum.i == this.last_index {
|
|
|
|
|
this.last_index = prevIndex
|
2021-04-23 18:35:28 +02:00
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
prevIndex = enum.i
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// bubbletea implementations
|
|
|
|
|
|
|
|
|
|
func (m model) Init() tea.Cmd {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func initialModel(v interface{}) model {
|
|
|
|
|
val := makeVal(v)
|
|
|
|
|
return model{
|
2021-12-18 14:03:15 +01:00
|
|
|
|
path: []index{},
|
|
|
|
|
data: val,
|
2021-04-23 18:35:28 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
|
|
|
var err error
|
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
|
case tea.KeyMsg:
|
|
|
|
|
switch msg.String() {
|
|
|
|
|
case "ctrl+c", "q":
|
|
|
|
|
return m, tea.Quit
|
|
|
|
|
|
|
|
|
|
case "up":
|
2021-04-05 23:35:00 +02:00
|
|
|
|
m, err = m.prev()
|
2021-04-23 18:35:28 +02:00
|
|
|
|
|
|
|
|
|
case "down":
|
2021-04-05 23:35:00 +02:00
|
|
|
|
m, err = m.next()
|
2021-04-23 18:35:28 +02:00
|
|
|
|
|
|
|
|
|
case "right":
|
2021-04-05 23:35:00 +02:00
|
|
|
|
m, err = m.descend()
|
2021-04-23 18:35:28 +02:00
|
|
|
|
|
|
|
|
|
case "left":
|
2021-04-05 23:35:00 +02:00
|
|
|
|
m, err = m.ascend()
|
2021-04-23 18:35:28 +02:00
|
|
|
|
|
|
|
|
|
// case "enter":
|
|
|
|
|
// _, ok := m.selected[m.cursor]
|
|
|
|
|
// if ok {
|
|
|
|
|
// delete(m.selected, m.cursor)
|
|
|
|
|
// } else {
|
|
|
|
|
// m.selected[m.cursor] = struct{}{}
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
return m, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var pathColor = lipgloss.NewStyle().
|
|
|
|
|
// light blue
|
|
|
|
|
Foreground(lipgloss.Color("12"))
|
|
|
|
|
|
|
|
|
|
var selectedColor = lipgloss.NewStyle().
|
|
|
|
|
Bold(true)
|
|
|
|
|
|
|
|
|
|
func (m model) View() string {
|
|
|
|
|
s := pathColor.Render(m.PathString())
|
2021-04-06 02:06:01 +02:00
|
|
|
|
cur, _, err := walk(&m.data, m.path)
|
2021-04-23 18:35:28 +02:00
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
2021-04-06 02:06:01 +02:00
|
|
|
|
s += cur.doc + "\n"
|
2021-04-23 18:35:28 +02:00
|
|
|
|
s += "\n"
|
|
|
|
|
for _, enum := range cur.enumerate() {
|
|
|
|
|
is := renderIndex(enum.i)
|
|
|
|
|
if is != "" {
|
|
|
|
|
s += is + " "
|
|
|
|
|
}
|
2021-04-06 02:06:01 +02:00
|
|
|
|
if enum.i == cur.last_index {
|
2021-04-23 18:35:28 +02:00
|
|
|
|
s += selectedColor.Render(enum.v.Render())
|
|
|
|
|
} else {
|
|
|
|
|
s += enum.v.Render()
|
|
|
|
|
}
|
|
|
|
|
s += "\n"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// s += fmt.Sprintf("%v\n", m)
|
|
|
|
|
// s += fmt.Sprintf("%v\n", cur)
|
|
|
|
|
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
var input interface{}
|
|
|
|
|
err := json.NewDecoder(os.Stdin).Decode(&input)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal("json from stdin: ", err)
|
|
|
|
|
}
|
|
|
|
|
p := tea.NewProgram(initialModel(input))
|
|
|
|
|
if err := p.Start(); err != nil {
|
|
|
|
|
log.Fatal("bubbletea TUI error: ", err)
|
|
|
|
|
}
|
|
|
|
|
}
|