Copy value to clipboard

This commit is contained in:
2026-01-23 16:43:44 -06:00
parent 0834792c57
commit 3f347d8342
4 changed files with 182 additions and 17 deletions

View File

@@ -31,7 +31,6 @@ import (
"github.com/gdamore/tcell"
)
// TODO: Format contents as json. For now this is a duplice of the Text widget
type JsonContent struct {
id string
@@ -40,15 +39,20 @@ type JsonContent struct {
style tcell.Style
x, y int
w, h int
visible bool
active bool
focusable bool
keyMap *wd.KeyMap
border []rune
visible bool
active bool
focusable bool
keyMap *wd.KeyMap
editable bool
value any
activeNode []string
vimMode bool
cursor int
}
var _ wd.Widget = (*JsonContent)(nil)
@@ -64,7 +68,45 @@ func (w *JsonContent) Init(id string, style tcell.Style) {
w.style = style
w.visible = true
w.focusable = false
w.keyMap = wd.BlankKeyMap()
w.keyMap = wd.NewKeyMap(
wd.NewKey(wd.BuildEK(tcell.KeyUp), func(_ *tcell.EventKey) bool { return w.MoveUp() }),
wd.NewKey(wd.BuildEK(tcell.KeyDown), func(_ *tcell.EventKey) bool { return w.MoveDown() }),
wd.NewKey(wd.BuildEK(tcell.KeyEnter), func(ev *tcell.EventKey) bool {
return false
}),
wd.NewKey(wd.BuildEK(tcell.KeyPgDn), func(_ *tcell.EventKey) bool { return w.PageDn() }),
wd.NewKey(wd.BuildEK(tcell.KeyPgUp), func(_ *tcell.EventKey) bool { return w.PageUp() }),
wd.NewKey(wd.BuildEKr('j'), func(ev *tcell.EventKey) bool {
if !w.vimMode {
return false
}
return w.MoveDown()
}),
wd.NewKey(wd.BuildEKr('k'), func(ev *tcell.EventKey) bool {
if !w.vimMode {
return false
}
return w.MoveUp()
}),
wd.NewKey(wd.BuildEKr('b'), func(ev *tcell.EventKey) bool {
if !w.vimMode {
return false
}
if ev.Modifiers()&tcell.ModCtrl != 0 {
return w.PageUp()
}
return false
}),
wd.NewKey(wd.BuildEKr('f'), func(ev *tcell.EventKey) bool {
if !w.vimMode {
return false
}
if ev.Modifiers()&tcell.ModCtrl != 0 {
return w.PageDn()
}
return false
}),
)
}
func (w *JsonContent) Id() string { return w.id }
@@ -73,15 +115,61 @@ func (w *JsonContent) HandleResize(ev *tcell.EventResize) { w.w, w.h = ev.Size()
func (w *JsonContent) GetKeyMap() *wd.KeyMap { return w.keyMap }
func (w *JsonContent) SetKeyMap(km *wd.KeyMap) { w.keyMap = km }
func (w *JsonContent) HandleKey(ev *tcell.EventKey) bool { return w.keyMap.Handle(ev) }
func (w *JsonContent) HandleTime(ev *tcell.EventTime) {}
func (w *JsonContent) HandleKey(ev *tcell.EventKey) bool {
return w.keyMap.Handle(ev)
}
func (w *JsonContent) HandleTime(ev *tcell.EventTime) {}
func (w *JsonContent) Draw(screen tcell.Screen) {
if !w.visible {
return
}
y := w.y
for i := range w.contents {
wh.DrawText(w.x, y, w.contents[i], w.style, screen)
x, y := w.x, w.y
brdSz := 0
if len(w.border) > 0 {
brdSz = 2
wh.BorderFilled(x, y, x+w.w, y+w.h, w.border, w.style, screen)
}
x, y = x+1, y+1
h := w.h - brdSz
ln := len(w.contents)
st, ed := 0, ln-1
if ln == 0 {
return
}
if ln > w.h-2 {
mid := h / 2
if w.cursor < mid {
// contents need to start at 0
ed = h + 1
} else if w.cursor > ln-mid {
// contents need to begin at ln-h
st = ln - h + 1
} else {
st = w.cursor - mid
ed = st + h + 1
}
}
// ed cannot be higher than ln-1
if st < 0 {
st = 0
}
if ed > ln-1 {
ed = ln - 1
}
for i := st; i <= ed; i++ {
st := w.style
dim := true
if i == w.cursor {
dim = false
} else {
st = st.Foreground(tcell.ColorGreen)
}
txt := w.contents[i]
if len(txt) > w.w-brdSz && w.w-brdSz >= 0 {
txt = txt[:(w.w - brdSz)]
}
wh.DrawText(w.x, y, txt, st.Dim(dim).Bold(!dim), screen)
y++
}
}
@@ -120,6 +208,13 @@ func (w *JsonContent) SetValue(v any) error {
w.contents = strings.Split(w.valueString, "\n")
return nil
}
func (w *JsonContent) SetBorder(brd []rune) {
if len(brd) == 0 {
w.border = wh.BRD_SIMPLE
} else {
w.border = wh.ValidateBorder(brd)
}
}
func (w *JsonContent) SetEditable(v bool) { w.editable = v }
@@ -136,3 +231,44 @@ func (w *JsonContent) SetEditable(v bool) { w.editable = v }
func (w *JsonContent) GetJsonContent() string { return w.text }
func (w *JsonContent) GetContents() []string { return w.contents }
*/
func (w *JsonContent) SetVimMode(b bool) { w.vimMode = b }
func (w *JsonContent) MoveUp() bool {
w.cursor--
if w.cursor < 0 {
w.cursor = 0
}
return true
}
func (w *JsonContent) MoveDown() bool {
w.cursor++
if w.cursor >= len(w.contents) {
w.cursor = len(w.contents) - 1
}
return true
}
func (w *JsonContent) PageUp() bool {
w.cursor -= w.h
if w.cursor < 0 {
w.cursor = 0
}
return true
}
func (w *JsonContent) PageDn() bool {
w.cursor += w.h
if w.cursor > len(w.contents)-1 {
w.cursor = len(w.contents) - 1
}
return true
}
func (w *JsonContent) GetSelectedValue() string {
if w.cursor >= len(w.contents) {
w.cursor = len(w.contents) - 1
}
var ret string
ret = w.contents[w.cursor]
ret = ret[strings.Index(ret, "\": \"")+3:]
ret = ret[1 : len(ret)-1]
return ret
}