Working on Editing
This commit is contained in:
@@ -52,8 +52,19 @@ type JsonContent struct {
|
||||
|
||||
vimMode bool
|
||||
cursor int
|
||||
|
||||
editKey, editVal bool
|
||||
editValType int
|
||||
}
|
||||
|
||||
const (
|
||||
JsonTpString = iota
|
||||
JsonTpBool
|
||||
JsonTpNumber
|
||||
JsonTpObject
|
||||
JsonTpArray
|
||||
)
|
||||
|
||||
var _ wd.Widget = (*JsonContent)(nil)
|
||||
|
||||
func NewJsonContent(id string, style tcell.Style) *JsonContent {
|
||||
@@ -128,7 +139,7 @@ func (w *JsonContent) Draw(screen tcell.Screen) {
|
||||
x, y = x+1, y+1
|
||||
h := w.h - brdSz
|
||||
ln := len(w.contents)
|
||||
st, ed := 0, ln-1
|
||||
start, end := 0, ln-1
|
||||
if ln == 0 {
|
||||
return
|
||||
}
|
||||
@@ -136,50 +147,93 @@ func (w *JsonContent) Draw(screen tcell.Screen) {
|
||||
mid := h / 2
|
||||
if w.cursor < mid {
|
||||
// contents need to start at 0
|
||||
ed = h + 1
|
||||
end = h + 1
|
||||
} else if w.cursor > ln-mid {
|
||||
// contents need to begin at ln-h
|
||||
st = ln - h + 1
|
||||
start = ln - h + 1
|
||||
} else {
|
||||
st = w.cursor - mid
|
||||
ed = st + h + 1
|
||||
start = w.cursor - mid
|
||||
end = start + h + 1
|
||||
}
|
||||
}
|
||||
// ed cannot be higher than ln-1
|
||||
if st < 0 {
|
||||
st = 0
|
||||
if start < 0 {
|
||||
start = 0
|
||||
}
|
||||
if ed > ln-1 {
|
||||
ed = ln - 1
|
||||
if end > ln-1 {
|
||||
end = 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)
|
||||
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 {
|
||||
var key, val string
|
||||
pts := strings.Split(txt, ":")
|
||||
if len(pts) == 2 {
|
||||
key, val = pts[0], pts[1]
|
||||
}
|
||||
wh.DrawText(w.x, y, key, st.Dim(dim).Bold(!dim), screen)
|
||||
st = st.Reverse(true)
|
||||
wh.DrawText(w.x, y, val, st.Dim(dim).Bold(!dim), screen)
|
||||
w.drawEditableLine(screen, i, w.x, y, txt, stl, dim)
|
||||
} else {
|
||||
wh.DrawText(w.x, y, txt, st.Dim(dim).Bold(!dim), screen)
|
||||
w.drawReadOnlyLine(screen, i, w.x, y, txt, stl, dim)
|
||||
}
|
||||
y++
|
||||
}
|
||||
}
|
||||
func (w *JsonContent) drawReadOnlyLine(screen tcell.Screen, idx, x, y int, txt string, stl tcell.Style, dim bool) {
|
||||
wh.DrawText(x, y, txt, stl.Dim(dim).Bold(!dim), screen)
|
||||
}
|
||||
func (w *JsonContent) drawEditableLine(screen tcell.Screen, idx, x, y int, txt string, stl tcell.Style, dim bool) {
|
||||
parseQuoted := func(txt string) (string, string) {
|
||||
var v string
|
||||
var slash bool
|
||||
for i := range txt {
|
||||
if txt[i] == '\\' {
|
||||
slash = true
|
||||
} else if slash {
|
||||
slash = false
|
||||
} else if txt[i] == '"' {
|
||||
return v, txt[i+1:]
|
||||
}
|
||||
v = fmt.Sprintf("%s%c", v, txt[i])
|
||||
}
|
||||
// We didn't hit an ending quote
|
||||
return v, ""
|
||||
}
|
||||
var key, val, wrk string
|
||||
// The key should be a quoted value
|
||||
for i := range txt {
|
||||
if txt[i] == '"' && len(txt) > i {
|
||||
key, wrk = parseQuoted(txt[i:])
|
||||
break
|
||||
}
|
||||
}
|
||||
txt = strings.Trim(wrk, ": ")
|
||||
if len(txt) > 0 {
|
||||
switch txt[0] {
|
||||
case '"':
|
||||
val, wrk = parseQuoted(txt[1:])
|
||||
w.editValType = JsonTpString
|
||||
case '{':
|
||||
w.editValType = JsonTpObject
|
||||
case '[':
|
||||
w.editValType = JsonTpArray
|
||||
default:
|
||||
if txt == "true" || txt == "false" {
|
||||
// Editing a boolean value
|
||||
w.editValType = JsonTpBool
|
||||
} else {
|
||||
// Must be a number, right?
|
||||
w.editValType = JsonTpNumber
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
keyStr := fmt.Sprintf("%s: ", key)
|
||||
wh.DrawText(w.x, y, keyStr, stl.Dim(dim).Bold(!dim), screen)
|
||||
wh.DrawText(w.x, y+len(keyStr), val, stl.Dim(dim).Bold(!dim), screen)
|
||||
}
|
||||
|
||||
func (w *JsonContent) SetStyle(s tcell.Style) { w.style = s }
|
||||
func (w *JsonContent) Active() bool { return w.active }
|
||||
|
||||
Reference in New Issue
Block a user