pull/268/head
Anton Medvedev 9 months ago
parent 2b9c5e47ac
commit 98107427ad
No known key found for this signature in database

@ -217,6 +217,7 @@ func (p *jsonParser) parseObject() *node {
closeBracket := &node{depth: p.depth}
closeBracket.value = []byte{'}'}
closeBracket.directParent = object
closeBracket.index = -1
object.append(closeBracket)
p.next()
return object
@ -243,10 +244,11 @@ func (p *jsonParser) parseArray() *node {
p.next()
return arr
}
for {
for i := 0; ; i++ {
p.depth++
value := p.parseValue()
value.directParent = arr
value.index = i
p.depth--
arr.append(value)
p.skipWhitespace()
@ -254,6 +256,7 @@ func (p *jsonParser) parseArray() *node {
closeBracket := &node{depth: p.depth}
closeBracket.value = []byte{']'}
closeBracket.directParent = arr
closeBracket.index = -1
arr.append(closeBracket)
p.next()
return arr

@ -5,6 +5,8 @@ import (
"io"
"os"
"runtime/pprof"
"strconv"
"strings"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
@ -58,6 +60,7 @@ type model struct {
head, top *node
cursor int // cursor position [0, termHeight)
wrap bool
fileName string
}
func (m *model) Init() tea.Cmd {
@ -334,6 +337,11 @@ func (m *model) View() string {
screen = append(screen, '\n')
}
statusBar := m.cursorPath() + " "
statusBar += strings.Repeat(" ", max(0, m.termWidth-len(statusBar)-len(m.fileName)))
statusBar += m.fileName
screen = append(screen, currentTheme.StatusBar([]byte(statusBar))...)
return string(screen)
}
@ -409,3 +417,31 @@ func (m *model) selectNode(n *node) {
m.scrollIntoView()
}
}
func (m *model) cursorPath() string {
path := ""
at := m.cursorPointsTo()
for at != nil {
if at.prev != nil {
if at.chunk != nil {
at = at.parent()
}
if at.key != nil {
quoted := string(at.key)
unquoted, err := strconv.Unquote(quoted)
if err != nil {
panic(err)
}
if identifier.MatchString(unquoted) {
path = "." + unquoted + path
} else {
path = "[" + quoted + "]" + path
}
} else if at.index >= 0 {
path = "[" + strconv.Itoa(at.index) + "]" + path
}
}
at = at.parent()
}
return path
}

@ -10,6 +10,7 @@ type node struct {
value []byte
chunk []byte
comma bool
index int
}
func (n *node) append(child *node) {

@ -1,5 +1,11 @@
package main
import (
"regexp"
)
var identifier = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_]*$`)
func isHexDigit(ch byte) bool {
return (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F')
}
@ -8,6 +14,13 @@ func isDigit(ch byte) bool {
return ch >= '0' && ch <= '9'
}
func max(i, j int) int {
if i > j {
return i
}
return j
}
func prettyPrint(b []byte, selected bool, isChunk bool) []byte {
if len(b) == 0 {
return b

Loading…
Cancel
Save