Add better value printer

pull/295/head
Anton Medvedev 3 months ago
parent a06e514a09
commit 83a3fa3733
No known key found for this signature in database

@ -161,7 +161,7 @@ func main() {
search: newSearch(), search: newSearch(),
} }
p := tea.NewProgram(m, tea.WithAltScreen(), tea.WithMouseCellMotion()) p := tea.NewProgram(m, tea.WithAltScreen(), tea.WithMouseCellMotion(), tea.WithOutput(os.Stderr))
_, err = p.Run() _, err = p.Run()
if err != nil { if err != nil {
panic(err) panic(err)
@ -361,7 +361,8 @@ func (m *model) handlePreviewKey(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd var cmd tea.Cmd
if msg, ok := msg.(tea.KeyMsg); ok { if msg, ok := msg.(tea.KeyMsg); ok {
switch { switch {
case key.Matches(msg, keyMap.Quit): case key.Matches(msg, keyMap.Quit),
key.Matches(msg, keyMap.Preview):
m.showPreview = false m.showPreview = false
} }
} }
@ -564,21 +565,9 @@ func (m *model) handleKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
m.yank = true m.yank = true
case key.Matches(msg, keyMap.Preview): case key.Matches(msg, keyMap.Preview):
at := m.cursorPointsTo() m.showPreview = true
parent := at.parent() m.preview.SetContent(m.cursorValue())
if parent != nil && parent.chunk != nil { m.preview.GotoTop()
at = parent
}
if at != nil && len(at.value) > 0 && at.value[0] == '"' {
m.showPreview = true
str, err := strconv.Unquote(string(at.value))
if err == nil {
m.preview.SetContent(str)
} else {
m.preview.SetContent(err.Error())
}
m.preview.GotoTop()
}
case key.Matches(msg, keyMap.Dig): case key.Matches(msg, keyMap.Dig):
m.digInput.SetValue(m.cursorPath() + ".") m.digInput.SetValue(m.cursorPath() + ".")
@ -919,17 +908,29 @@ func (m *model) cursorValue() string {
if at == nil { if at == nil {
return "" return ""
} }
var out strings.Builder parent := at.parent()
if at.chunk != nil && at.value == nil { if parent != nil && at.chunk != nil {
at = at.parent() at = parent
}
if len(at.value) > 0 && at.value[0] == '"' {
str, err := strconv.Unquote(string(at.value))
if err == nil {
return str
}
return string(at.value)
} }
var out strings.Builder
out.Write(at.value) out.Write(at.value)
out.WriteString("\n")
if at.hasChildren() { if at.hasChildren() {
it := at.next it := at.next
if at.isCollapsed() { if at.isCollapsed() {
it = at.collapsed it = at.collapsed
} }
for it != nil { for it != nil {
out.WriteString(strings.Repeat(" ", int(it.depth-at.depth)))
if it.key != nil { if it.key != nil {
out.Write(it.key) out.Write(it.key)
out.WriteString(": ") out.WriteString(": ")
@ -943,8 +944,9 @@ func (m *model) cursorValue() string {
break break
} }
if it.comma { if it.comma {
out.WriteString(", ") out.WriteString(",")
} }
out.WriteString("\n")
if it.isCollapsed() { if it.isCollapsed() {
it = it.collapsed it = it.collapsed
} else { } else {

Loading…
Cancel
Save