Files
expds/widgets/json_content.go
2026-02-19 11:50:25 -06:00

503 lines
13 KiB
Go

/*
Copyright © Brian Buller <brian@bullercodeworks.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package widgets
import (
"encoding/json"
"errors"
"fmt"
"strings"
wd "git.bullercodeworks.com/brian/tcell-widgets"
wh "git.bullercodeworks.com/brian/tcell-widgets/helpers"
"github.com/gdamore/tcell"
)
type JsonContent struct {
id string
valueString string
contents []string
style tcell.Style
x, y int
w, h int
border []rune
visible bool
active bool
keyMap *wd.KeyMap
editable bool
value any
jsonObj map[string]json.RawMessage
jsonArr json.RawMessage
isObj bool
activeNode []string
vimMode bool
cursor int
editKey, editVal bool
editValType int
logger func(string, ...any)
}
const (
JsonTpString = iota
JsonTpBool
JsonTpNumber
JsonTpObject
JsonTpArray
JsonTpErr
)
var _ wd.Widget = (*JsonContent)(nil)
func NewJsonContent(id string, style tcell.Style) *JsonContent {
ret := &JsonContent{}
ret.Init(id, style)
return ret
}
func (w *JsonContent) Init(id string, style tcell.Style) {
w.id = id
w.style = style
w.visible = true
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 }
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) Draw(screen tcell.Screen) {
if !w.visible {
return
}
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)
start, end := 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
end = h + 1
} else if w.cursor > ln-mid {
// contents need to begin at ln-h
start = ln - h + 1
} else {
start = w.cursor - mid
end = start + h + 1
}
}
// ed cannot be higher than ln-1
if start < 0 {
start = 0
}
if end > ln-1 {
end = ln - 1
}
for i := start; i <= end; i++ {
stl := w.style
dim := i != w.cursor
if dim {
stl = stl.Foreground(tcell.ColorGreen)
}
txt := w.contents[i]
if len(txt) > w.w-brdSz && w.w-brdSz >= 0 {
txt = txt[:(w.w - brdSz)]
}
if w.editable && i == w.cursor {
pth, err := w.GetSelectedPath()
if err != nil {
wh.DrawText(x, y, fmt.Sprintf("%s", err.Error()), stl, screen)
} else {
key, val, err := w.getItemKeyVal(w.cursor)
if err != nil {
wh.DrawText(x, y, fmt.Sprintf("%s: %s", pth, err.Error()), stl, screen)
} else {
wh.DrawText(x, y, fmt.Sprintf("%s: %s -> %s", pth, key, val), stl, screen)
}
}
} else {
wh.DrawText(x, y, txt, stl.Dim(dim).Bold(!dim), screen)
}
y++
}
}
func (w *JsonContent) SetStyle(s tcell.Style) { w.style = s }
func (w *JsonContent) Active() bool { return w.active }
func (w *JsonContent) SetActive(a bool) bool {
w.active = a
return w.active
}
func (w *JsonContent) Visible() bool { return w.visible }
func (w *JsonContent) SetVisible(a bool) { w.visible = a }
func (w *JsonContent) SetX(x int) { w.x = x }
func (w *JsonContent) SetY(y int) { w.y = y }
func (w *JsonContent) GetX() int { return w.x }
func (w *JsonContent) GetY() int { return w.y }
func (w *JsonContent) GetPos() wd.Coord { return wd.Coord{X: w.x, Y: w.y} }
func (w *JsonContent) SetPos(c wd.Coord) { w.x, w.y = c.X, c.Y }
func (w *JsonContent) SetW(x int) { w.w = x }
func (w *JsonContent) SetH(y int) { w.h = y }
func (w *JsonContent) GetW() int { return w.w }
func (w *JsonContent) GetH() int { return w.h }
func (w *JsonContent) WantW() int { return wh.Longest(w.contents) }
func (w *JsonContent) WantH() int { return len(w.contents) }
func (w *JsonContent) SetSize(c wd.Coord) { w.w, w.h = c.X, c.Y }
func (w *JsonContent) MinW() int { return wh.Longest(w.contents) }
func (w *JsonContent) MinH() int { return len(w.contents) }
func (w *JsonContent) SetValue(v any) error {
w.value = v
// Go ahead and try to build the json for v
bts, err := json.MarshalIndent(v, "", " ")
if err != nil {
return fmt.Errorf("error unmarshalling value: %w", err)
}
w.valueString = string(bts)
w.contents = strings.Split(w.valueString, "\n")
if bts[0] == '{' {
err = json.Unmarshal(bts, &w.jsonObj)
w.isObj = true
} else if bts[0] == '[' {
err = json.Unmarshal(bts, &w.jsonArr)
w.isObj = false
} else {
// Item is just a flat string/number/boolean
w.cursor = 0
}
if !w.isLineSelectable(w.cursor) {
idx := w.findPrevSelectableLine(w.cursor)
if idx == -1 {
idx = w.findNextSelectableLine(w.cursor)
}
if idx >= 0 {
w.cursor = idx
}
}
return err
}
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 }
func (w *JsonContent) SetVimMode(b bool) { w.vimMode = b }
func (w *JsonContent) MoveUp() bool {
idx := w.findPrevSelectableLine(w.cursor)
if idx < 0 {
return false
}
w.cursor = idx
return true
}
func (w *JsonContent) MoveDown() bool {
idx := w.findNextSelectableLine(w.cursor)
if idx < 0 {
return false
}
w.cursor = idx
return true
}
func (w *JsonContent) PageUp() bool {
idx := w.findPrevSelectableLine(w.cursor - w.h)
if idx < 0 {
return false
}
w.cursor = idx
return true
}
func (w *JsonContent) PageDn() bool {
idx := w.findNextSelectableLine(w.cursor + w.h)
if idx < 0 {
return false
}
w.cursor = idx
return true
}
func (w *JsonContent) GetSelectedPath() ([]string, error) { return w.getItemPath(w.cursor) }
func (w *JsonContent) GetSelectedKey() (string, error) { return w.getItemKey(w.cursor) }
func (w *JsonContent) GetSelectedValue() (string, error) { return w.getItemVal(w.cursor) }
func (w *JsonContent) getSelectedItemDepth() int { return w.getItemDepth(w.cursor) }
func (w *JsonContent) getItemPath(line int) ([]string, error) {
if line < 0 || line >= len(w.contents) {
return []string{}, errors.New("invalid line number")
}
depth := w.getItemDepth(line)
if depth == 1 {
key, err := w.getItemKey(line)
if err != nil {
return []string{}, err
}
return []string{key}, nil
}
pth, err := w.getItemPath(w.findItemParentLine(line))
if err != nil {
return []string{}, err
}
key, err := w.getItemKey(line)
if err != nil {
return []string{}, err
}
return append(pth, key), nil
}
func (w *JsonContent) getItemKeyVal(line int) (string, string, error) {
if line < 0 || line >= len(w.contents) {
return "", "", errors.New("invalid line number")
}
text := w.contents[line]
key, rest := w.getNextQuotedString(text)
if key == "" {
return "", "", errors.New("error finding quoted string")
} else if rest == "" {
// We have a 'key' value, but no 'rest' so this is likely an item in array
return "", key, nil
}
rest = strings.Trim(rest, ":{[}], ")
val, rest := w.getNextQuotedString(rest)
if val != "" {
return key, val, nil
}
return key, rest, nil
}
func (w *JsonContent) getItemKey(line int) (string, error) {
if line < 0 || line >= len(w.contents) {
return "", errors.New("invalid line number")
}
key, _ := w.getNextQuotedString(w.contents[line])
if key == "" {
return "", errors.New("error finding quoted string")
}
return key, nil
}
func (w *JsonContent) getItemVal(line int) (string, error) {
if line < 0 || line >= len(w.contents) {
return "", errors.New("invalid line number")
}
var key, val, rest string
key, rest = w.getNextQuotedString(w.contents[line])
if len(rest) > 0 {
val, rest = w.getNextQuotedString(rest)
}
if len(val) > 0 {
return val, nil
}
if len(key) > 0 {
return key, nil
}
return "", errors.New("error finding value")
}
/*
func (w *JsonContent) getItemValType(line int) int {
if line < 0 || line >= len(w.contents) {
return JsonTypeErr
}
text := w.contents[line]
key, rest := w.getNextQuotedString(text)
rest = strings.Trim(rest, ", ")
if rest == "" {
if key != "" {
// Looks like a string value in an array
return JsonTypeString
} else rest == "true" || rest == "false" {
return JsonTypeBool
}
// see if we can parse this as a number
num, err := strconv.Atoi(rest)
if err == nil {
return JsonTypeNumber
}
return JsonType
}
if key == "" {
return "", "", errors.New("error finding quoted string")
} else if rest == "" {
// We have a 'key' value, but no 'rest' so this is likely an item in array
return "", key, nil
}
rest = strings.Trim(rest, ":, ")
if rest[0] == '{' {
}
val, rest := w.getNextQuotedString(rest)
if val != "" {
return key, val, nil
}
// Val isn't a quoted string,
return key, rest, nil
}
*/
func (w *JsonContent) getItemDepth(line int) int {
if line < 0 || line >= len(w.contents) {
return 0
}
txt := w.contents[line]
// Find the depth, we use two spaces to indent
for i := range txt {
if txt[i] != ' ' {
return i / 2
}
}
return 0
}
func (w *JsonContent) findItemParentLine(line int) int {
depth := w.getItemDepth(line)
if depth <= 1 {
return -1
}
for i := w.cursor; i >= 0; i-- {
if w.getItemDepth(i) < depth {
return i
}
}
return -1
}
func (w *JsonContent) getItemParentKey(line int) (string, error) {
return w.getItemKey(w.findItemParentLine(line))
}
func (w *JsonContent) getItemParentVal(line int) (string, error) {
return w.getItemVal(w.findItemParentLine(line))
}
func (w *JsonContent) getNextQuotedString(line string) (string, string) {
var inQ, slash bool
var ret string
for i := range line {
if inQ {
if line[i] == '"' && !slash {
return ret, line[i+1:]
} else if slash {
slash = false
} else if line[i] == '\\' {
slash = true
}
ret = fmt.Sprintf("%s%c", ret, line[i])
}
if line[i] == '"' {
inQ = true
}
}
// if we're here, we didn't find a quoted string
return "", line
}
func (w *JsonContent) isLineSelectable(line int) bool {
if line < 0 || line >= len(w.contents) {
return false
}
return strings.Contains(w.contents[line], "\"")
}
func (w *JsonContent) findPrevSelectableLine(from int) int {
for i := from - 1; i >= 0; i-- {
if w.isLineSelectable(i) {
return i
}
}
return -1
}
func (w *JsonContent) findNextSelectableLine(from int) int {
for i := from + 1; i < len(w.contents); i++ {
if w.isLineSelectable(i) {
return i
}
}
return -1
}
func (w *JsonContent) SetLogger(l func(string, ...any)) { w.logger = l }
func (w *JsonContent) Log(txt string, args ...any) { w.logger(txt, args...) }