122 lines
3.4 KiB
Go
122 lines
3.4 KiB
Go
|
package ui
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"strings"
|
||
|
|
||
|
"git.bullercodeworks.com/brian/boltbrowser/models"
|
||
|
"git.bullercodeworks.com/brian/boltbrowser/util"
|
||
|
"git.bullercodeworks.com/brian/wandle"
|
||
|
)
|
||
|
|
||
|
/*
|
||
|
BoltDetailPane shows detail about either a bolt bucket or bolt pair
|
||
|
(as defined in the models subpackage)
|
||
|
*/
|
||
|
type BoltDetailPane struct {
|
||
|
x, y int
|
||
|
width int
|
||
|
height int
|
||
|
bucket *models.BoltBucket
|
||
|
pair *models.BoltPair
|
||
|
scrollRow int
|
||
|
active bool
|
||
|
|
||
|
firstRow int
|
||
|
buffer []string
|
||
|
|
||
|
db *models.BoltDB
|
||
|
}
|
||
|
|
||
|
func (p *BoltDetailPane) SetDB(db *models.BoltDB) { p.db = db }
|
||
|
func (p *BoltDetailPane) Init() wandle.Cmd { return nil }
|
||
|
func (p *BoltDetailPane) Update(wandle.Msg) wandle.Cmd { return nil }
|
||
|
func (p *BoltDetailPane) View(style wandle.Style) {
|
||
|
if len(p.buffer) == 0 {
|
||
|
return
|
||
|
}
|
||
|
for i := range p.buffer {
|
||
|
if i < len(p.buffer) {
|
||
|
wandle.Print(p.x, p.y+i, style, p.buffer[i])
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
func (p *BoltDetailPane) IsActive() bool { return p.active }
|
||
|
func (p *BoltDetailPane) SetActive(b bool) { p.active = b }
|
||
|
func (p *BoltDetailPane) Focusable() bool { return true }
|
||
|
func (p *BoltDetailPane) SetX(x int) { p.x = x }
|
||
|
func (p *BoltDetailPane) SetY(y int) { p.y = y }
|
||
|
func (p *BoltDetailPane) GetX() int { return p.x }
|
||
|
func (p *BoltDetailPane) GetY() int { return p.y }
|
||
|
func (p *BoltDetailPane) GetHeight() int { return p.height }
|
||
|
func (p *BoltDetailPane) SetHeight(h int) { p.height = h }
|
||
|
func (p *BoltDetailPane) GetWidth() int { return p.width }
|
||
|
func (p *BoltDetailPane) SetWidth(w int) { p.width = w }
|
||
|
func (p *BoltDetailPane) SetBuffer(buffer []string) { p.buffer = buffer }
|
||
|
func (p *BoltDetailPane) refresh() {
|
||
|
p.buffer = []string{}
|
||
|
if p.pair != nil {
|
||
|
p.buffer = append(p.buffer, []string{
|
||
|
fmt.Sprintf("Path: %s", pathToString(util.StringifyPath(p.pair.GetPath()))),
|
||
|
fmt.Sprintf("Key: %s", util.Stringify([]byte(p.pair.GetKey()))),
|
||
|
}...)
|
||
|
value := strings.Split(string(p.formatValue([]byte(p.pair.GetValue()))), "\n")
|
||
|
if len(value) == 1 {
|
||
|
p.buffer = append(p.buffer, fmt.Sprintf("Value: %s", value[0]))
|
||
|
} else {
|
||
|
p.buffer = append(p.buffer, "Value:")
|
||
|
p.buffer = append(p.buffer, value...)
|
||
|
}
|
||
|
//fmt.Sprintf("Value: %s", ),
|
||
|
} else if p.bucket != nil {
|
||
|
p.buffer = append(p.buffer, []string{
|
||
|
fmt.Sprintf("Path: %s", pathToString(util.StringifyPath(p.bucket.GetPath()))),
|
||
|
fmt.Sprintf("Buckets: %d", len(p.bucket.GetBuckets())),
|
||
|
fmt.Sprintf("Pairs: %d", len(p.bucket.GetPairs())),
|
||
|
}...)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (p *BoltDetailPane) SetActiveItem(bucket *models.BoltBucket, pair *models.BoltPair, e error) {
|
||
|
if bucket != nil {
|
||
|
p.SetBucket(bucket)
|
||
|
} else if pair != nil {
|
||
|
p.SetPair(pair)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (p *BoltDetailPane) SetBucket(b *models.BoltBucket) {
|
||
|
p.pair = nil
|
||
|
p.bucket = b
|
||
|
p.refresh()
|
||
|
}
|
||
|
|
||
|
func (p *BoltDetailPane) SetPair(pair *models.BoltPair) {
|
||
|
p.bucket = nil
|
||
|
p.pair = pair
|
||
|
p.refresh()
|
||
|
}
|
||
|
|
||
|
func (p *BoltDetailPane) formatValue(val []byte) []byte {
|
||
|
// Attempt JSON parsing and formatting
|
||
|
out, err := formatValueJSON(val)
|
||
|
if err == nil {
|
||
|
return out
|
||
|
}
|
||
|
return []byte(util.Stringify([]byte(val)))
|
||
|
}
|
||
|
|
||
|
func formatValueJSON(val []byte) ([]byte, error) {
|
||
|
var jsonOut interface{}
|
||
|
err := json.Unmarshal(val, &jsonOut)
|
||
|
if err != nil {
|
||
|
return val, err
|
||
|
}
|
||
|
out, err := json.MarshalIndent(jsonOut, "", " ")
|
||
|
if err != nil {
|
||
|
return val, err
|
||
|
}
|
||
|
return out, nil
|
||
|
}
|