pull/268/head
Anton Medvedev 9 months ago
parent 6671a1c041
commit 6529535cc4
No known key found for this signature in database

@ -3,9 +3,12 @@ module github.com/antonmedv/fx
go 1.20
require (
github.com/antonmedv/clipboard v1.0.1
github.com/charmbracelet/bubbles v0.16.1
github.com/charmbracelet/bubbletea v0.24.2
github.com/charmbracelet/lipgloss v0.8.0
github.com/charmbracelet/x/exp/teatest v0.0.0-20230904163802-ca705a396e0f
github.com/mattn/go-isatty v0.0.18
github.com/mattn/go-runewidth v0.0.15
github.com/mazznoer/colorgrad v0.9.1
github.com/muesli/termenv v0.15.2
@ -16,11 +19,9 @@ require (
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/aymanbagabas/go-udiff v0.1.0 // indirect
github.com/charmbracelet/x/exp/teatest v0.0.0-20230904163802-ca705a396e0f // indirect
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mazznoer/csscolorparser v0.1.2 // indirect
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect

@ -1,3 +1,5 @@
github.com/antonmedv/clipboard v1.0.1 h1:z9rRBhSKt4lDb6uNcMykUmNbspk/6v07JeiTaOfYYOY=
github.com/antonmedv/clipboard v1.0.1/go.mod h1:3jcOUCdraVHehZaOsMaJZoE92MxURt5fovC1gDAiZ2s=
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=

@ -21,6 +21,7 @@ type KeyMap struct {
NextSibling key.Binding
PrevSibling key.Binding
ToggleWrap key.Binding
Yank key.Binding
Search key.Binding
SearchNext key.Binding
SearchPrev key.Binding
@ -103,6 +104,10 @@ func init() {
key.WithKeys("z"),
key.WithHelp("", "toggle strings wrap"),
),
Yank: key.NewBinding(
key.WithKeys("y"),
key.WithHelp("", "yank/copy"),
),
Search: key.NewBinding(
key.WithKeys("/"),
key.WithHelp("", "search regexp"),

@ -10,7 +10,9 @@ import (
"regexp"
"runtime/pprof"
"strconv"
"strings"
"github.com/antonmedv/clipboard"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
@ -150,6 +152,7 @@ type model struct {
digInput textinput.Model
searchInput textinput.Model
search *search
yank bool
}
func (m *model) Init() tea.Cmd {
@ -204,6 +207,9 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if m.searchInput.Focused() {
return m.handleSearchKey(msg)
}
if m.yank {
return m.handleYankKey(msg)
}
return m.handleKey(msg)
}
return m, nil
@ -244,6 +250,17 @@ func (m *model) handleSearchKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
return m, cmd
}
func (m *model) handleYankKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
switch {
case msg.Type == tea.KeyRunes && len(msg.Runes) == 1 && msg.Runes[0] == 'p':
_ = clipboard.WriteAll(m.cursorPath())
case msg.Type == tea.KeyRunes && len(msg.Runes) == 1 && msg.Runes[0] == 'y':
_ = clipboard.WriteAll(m.cursorValue())
}
m.yank = false
return m, nil
}
func (m *model) handleKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
switch {
case key.Matches(msg, keyMap.Quit):
@ -372,6 +389,9 @@ func (m *model) handleKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
m.redoSearch()
m.selectNode(at)
case key.Matches(msg, keyMap.Yank):
m.yank = true
case key.Matches(msg, keyMap.Dig):
m.digInput.SetValue(m.cursorPath())
m.digInput.CursorEnd()
@ -524,6 +544,11 @@ func (m *model) View() string {
}
}
if m.yank {
screen = append(screen, '\n')
screen = append(screen, []byte("(y)value (p)path")...)
}
return string(screen)
}
@ -587,6 +612,9 @@ func (m *model) viewHeight() int {
if m.searchInput.Focused() || m.searchInput.Value() != "" {
return m.termHeight - 2
}
if m.yank {
return m.termHeight - 2
}
return m.termHeight - 1
}
@ -693,6 +721,47 @@ func (m *model) cursorPath() string {
return path
}
func (m *model) cursorValue() string {
at := m.cursorPointsTo()
if at == nil {
return ""
}
var out strings.Builder
if at.chunk != nil && at.value == nil {
at = at.parent()
}
out.Write(at.value)
if at.hasChildren() {
it := at.next
if at.isCollapsed() {
it = at.collapsed
}
for it != nil {
if it.key != nil {
out.Write(it.key)
out.WriteString(": ")
}
if it.chunk != nil {
out.Write(it.chunk)
} else {
out.Write(it.value)
}
if it == at.end {
break
}
if it.comma {
out.WriteString(", ")
}
if it.isCollapsed() {
it = it.collapsed
} else {
it = it.next
}
}
}
return out.String()
}
func (m *model) dig(value string) *node {
p, ok := jsonpath.Split(value)
if !ok {

Loading…
Cancel
Save