Use a buffer system for building the panes

This commit is contained in:
Brian Buller 2019-10-17 10:49:08 -05:00
parent 72d7cc8280
commit e6f872fbf6

View File

@ -6,7 +6,7 @@ import (
"strings"
"time"
"github.com/br0xen/termbox-util"
termboxUtil "github.com/br0xen/termbox-util"
"github.com/nsf/termbox-go"
)
@ -37,9 +37,6 @@ type BrowserScreen struct {
messageTimeout time.Duration
messageTime time.Time
rightPaneHeight int
rightPaneCursor int
leftPaneBuffer []Line
rightPaneBuffer []Line
}
@ -544,11 +541,14 @@ func (screen *BrowserScreen) buildLeftPane(style Style) {
if len(screen.currentPath) == 0 {
screen.currentPath = screen.db.getNextVisiblePath(nil)
}
curPathSpot := 0
visPaths, err := screen.db.buildVisiblePathSlice()
if err == nil {
for idx, pth := range visPaths {
screen.leftPaneBuffer
for i := range screen.db.buckets {
screen.leftPaneBuffer = append(screen.leftPaneBuffer, screen.bucketToLines(&screen.db.buckets[i], style)...)
}
// Find the cursor in the leftPane
for k, v := range screen.leftPaneBuffer {
if v.Fg == style.cursorFg {
screen.leftViewPort.scrollRow = k
break
}
}
}
@ -562,19 +562,18 @@ func (screen *BrowserScreen) drawLeftPane(style Style) {
screen.leftViewPort.bytesPerRow = w
screen.leftViewPort.numberOfRows = h - 2
termboxUtil.FillWithChar('=', 0, 1, w, 1, style.defaultFg, style.defaultBg)
y := 2
screen.leftViewPort.firstRow = y
startX, startY := 0, 3
screen.leftViewPort.firstRow = startY
treeOffset := 0
maxCursor := screen.leftViewPort.numberOfRows * 2 / 3
if curPathSpot > maxCursor {
treeOffset = curPathSpot - maxCursor
}
for i := range screen.db.buckets {
// The drawBucket function returns how many lines it took up
bktH := screen.drawBucket(&screen.db.buckets[i], style, (y - treeOffset))
y += bktH
if screen.leftViewPort.scrollRow > maxCursor {
treeOffset = screen.leftViewPort.scrollRow - maxCursor
}
if len(screen.leftPaneBuffer) > 0 {
for k, v := range screen.leftPaneBuffer[treeOffset:] {
termboxUtil.DrawStringAtPoint(v.Text, startX, (startY + k - 1), v.Fg, v.Bg)
}
}
}
@ -626,11 +625,11 @@ func (screen *BrowserScreen) drawRightPane(style Style) {
termboxUtil.FillWithChar('=', 0, 1, w, 1, style.defaultFg, style.defaultBg)
termboxUtil.FillWithChar('|', (w / 2), screen.rightViewPort.firstRow-1, (w / 2), h, style.defaultFg, style.defaultBg)
// Clear the right pane
termboxUtil.FillWithChar(' ', (w/2)+1, screen.rightViewPort.firstRow, w, h, style.defaultFg, style.defaultBg)
termboxUtil.FillWithChar(' ', (w/2)+1, screen.rightViewPort.firstRow+2, w, h, style.defaultFg, style.defaultBg)
startX := (w / 2) + 2
startY := 2
maxScroll := len(screen.rightPaneBuffer)-screen.rightViewPort.numberOfRows
startY := 3
maxScroll := len(screen.rightPaneBuffer) - screen.rightViewPort.numberOfRows
if maxScroll < 0 {
maxScroll = 0
}
@ -667,105 +666,31 @@ func formatValueJSON(val []byte) ([]byte, error) {
return out, nil
}
func (screen *BrowserScreen) bucketToStrings(bkt *BoltBucket, style Style) []string {
}
/* drawBucket
* @bkt *BoltBucket - The bucket to draw
* @style Style - The style to use
* @w int - The Width of the lines
* @y int - The Y position to start drawing
* return - The number of lines used
*/
func (screen *BrowserScreen) drawBucket(bkt *BoltBucket, style Style, y int) int {
w, _ := termbox.Size()
if w > 80 {
w = w / 2
}
usedLines := 0
bucketFg := style.defaultFg
bucketBg := style.defaultBg
func (screen *BrowserScreen) bucketToLines(bkt *BoltBucket, style Style) []Line {
var ret []Line
bfg, bbg := style.defaultFg, style.defaultBg
if comparePaths(screen.currentPath, bkt.GetPath()) {
bucketFg = style.cursorFg
bucketBg = style.cursorBg
bfg, bbg = style.cursorFg, style.cursorBg
}
prefixSpaces := strings.Repeat(" ", len(bkt.GetPath())*2)
bktString := prefixSpaces
prefixSpaces = prefixSpaces + " "
padAmt := (len(bkt.GetPath())*2 + 2)
bktPrefix := strings.Repeat(" ", len(bkt.GetPath())*2)
if bkt.expanded {
bktString = bktString + "- " + stringify([]byte(bkt.name))
if len(bktString)+padAmt > w {
bktString = bktString[:w-padAmt-3] + "..."
}
usedLines = screen.drawMultilineText(bktString, (len(bkt.GetPath())*2 + 2), 0, y, (w - 1), bucketFg, bucketBg)
ret = append(ret, Line{bktPrefix + "- " + stringify([]byte(bkt.name)), bfg, bbg})
for i := range bkt.buckets {
usedLines += screen.drawBucket(&bkt.buckets[i], style, y+usedLines)
ret = append(ret, screen.bucketToLines(&bkt.buckets[i], style)...)
}
for i := range bkt.pairs {
usedLines += screen.drawPair(&bkt.pairs[i], style, y+usedLines)
}
} else {
bktString = bktString + "+ " + stringify([]byte(bkt.name))
if len(bktString)+padAmt > w {
bktString = bktString[:w-padAmt-3] + "..."
}
usedLines = screen.drawMultilineText(bktString, (len(bkt.GetPath())*2 + 2), 0, y, (w - 1), bucketFg, bucketBg)
}
return usedLines
}
func (screen *BrowserScreen) drawPair(bp *BoltPair, style Style, y int) int {
w, _ := termbox.Size()
if w > 80 {
w = w / 2
}
usedLines := 0
bucketFg := style.defaultFg
bucketBg := style.defaultBg
for _, bp := range bkt.pairs {
pfg, pbg := style.defaultFg, style.defaultBg
if comparePaths(screen.currentPath, bp.GetPath()) {
bucketFg = style.cursorFg
bucketBg = style.cursorBg
pfg, pbg = style.cursorFg, style.cursorBg
}
prefixSpaces := strings.Repeat(" ", len(bp.GetPath())*2)
pairString := prefixSpaces
pairString = fmt.Sprintf("%s%s: %s", pairString, stringify([]byte(bp.key)), stringify([]byte(bp.val)))
if len(pairString) > w {
}
prefixSpaces = prefixSpaces + " "
if len(pairString) > w {
// The long pair strings are causing a problem, for now, truncate
pairString = pairString[:w-3] + "..."
}
/* TODO: Re-enable this when I figure out the display issue
// Long pair string, wrap it
// We're going to try to wrap it at the :, if we can
if len(bp.GetPath())*2+len(bp.key)+1 > w {
// We can't... So just wrap it
usedLines = screen.drawMultilineText(pairString, (len(bp.GetPath()) * 2), 0, y, (w - 1), style.defaultFg, style.defaultBg)
} else {
// That's convenient, wrap at the :
pairString := strings.Repeat(" ", len(bp.GetPath())*2)
pairString = fmt.Sprintf("%s%s:", pairString, bp.key)
termboxUtil.DrawStringAtPoint(pairString, 0, y, bucketFg, bucketBg)
usedLines++
pairString = strings.Repeat(" ", len(bp.GetPath())*2+2) + bp.val
usedLines += screen.drawMultilineText(pairString, (len(bp.GetPath())*2)+2, 0, y+1, (w - 1), bucketFg, bucketBg)
prPrefix := strings.Repeat(" ", len(bp.GetPath())*2)
pairString := fmt.Sprintf("%s%s: %s", prPrefix, stringify([]byte(bp.key)), stringify([]byte(bp.val)))
ret = append(ret, Line{pairString, pfg, pbg})
}
} else {
*/
if w-len(pairString) > 0 {
pairString = fmt.Sprintf("%s%s", pairString, strings.Repeat(" ", (w-len(pairString))))
ret = append(ret, Line{bktPrefix + "+ " + stringify([]byte(bkt.name)), bfg, bbg})
}
termboxUtil.DrawStringAtPoint(pairString, 0, y, bucketFg, bucketBg)
usedLines = 1
// }
return usedLines
return ret
}
func (screen *BrowserScreen) startDeleteItem() bool {
@ -963,31 +888,6 @@ func (screen *BrowserScreen) startExportJSON() bool {
return false
}
// Print text on multiple lines, if needed
// msg - What to print
// indentPadding - number of spaces to pad lines after the first
// startX - Starting x
// startY - Starting y
// maxWidth - Maximum width
// fg, bg - Colors
// Returns the number of lines used
func (screen *BrowserScreen) drawMultilineText(msg string, indentPadding, startX, startY, maxWidth int, fg, bg termbox.Attribute) int {
var numLines int
spacePadding := strings.Repeat(" ", indentPadding)
// First we need to split 'msg' into the lines it should have (split on '\n')
msgs := strings.Split(msg, "\n")
for _, msg = range msgs {
for len(msg) > maxWidth {
termboxUtil.DrawStringAtPoint(msg[:maxWidth-1], startX, (startY + numLines), fg, bg)
msg = spacePadding + msg[maxWidth-1:]
numLines++
}
termboxUtil.DrawStringAtPoint(msg, startX, (startY + numLines), fg, bg)
numLines++
}
return numLines
}
func (screen *BrowserScreen) setMessage(msg string) {
screen.message = msg
screen.messageTime = time.Now()