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" "strings"
"time" "time"
"github.com/br0xen/termbox-util" termboxUtil "github.com/br0xen/termbox-util"
"github.com/nsf/termbox-go" "github.com/nsf/termbox-go"
) )
@ -37,9 +37,6 @@ type BrowserScreen struct {
messageTimeout time.Duration messageTimeout time.Duration
messageTime time.Time messageTime time.Time
rightPaneHeight int
rightPaneCursor int
leftPaneBuffer []Line leftPaneBuffer []Line
rightPaneBuffer []Line rightPaneBuffer []Line
} }
@ -544,11 +541,14 @@ func (screen *BrowserScreen) buildLeftPane(style Style) {
if len(screen.currentPath) == 0 { if len(screen.currentPath) == 0 {
screen.currentPath = screen.db.getNextVisiblePath(nil) screen.currentPath = screen.db.getNextVisiblePath(nil)
} }
curPathSpot := 0 for i := range screen.db.buckets {
visPaths, err := screen.db.buildVisiblePathSlice() screen.leftPaneBuffer = append(screen.leftPaneBuffer, screen.bucketToLines(&screen.db.buckets[i], style)...)
if err == nil { }
for idx, pth := range visPaths { // Find the cursor in the leftPane
screen.leftPaneBuffer 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.bytesPerRow = w
screen.leftViewPort.numberOfRows = h - 2 screen.leftViewPort.numberOfRows = h - 2
termboxUtil.FillWithChar('=', 0, 1, w, 1, style.defaultFg, style.defaultBg) termboxUtil.FillWithChar('=', 0, 1, w, 1, style.defaultFg, style.defaultBg)
y := 2 startX, startY := 0, 3
screen.leftViewPort.firstRow = y screen.leftViewPort.firstRow = startY
treeOffset := 0 treeOffset := 0
maxCursor := screen.leftViewPort.numberOfRows * 2 / 3 maxCursor := screen.leftViewPort.numberOfRows * 2 / 3
if curPathSpot > maxCursor {
treeOffset = curPathSpot - maxCursor
}
for i := range screen.db.buckets { if screen.leftViewPort.scrollRow > maxCursor {
// The drawBucket function returns how many lines it took up treeOffset = screen.leftViewPort.scrollRow - maxCursor
bktH := screen.drawBucket(&screen.db.buckets[i], style, (y - treeOffset)) }
y += bktH 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('=', 0, 1, w, 1, style.defaultFg, style.defaultBg)
termboxUtil.FillWithChar('|', (w / 2), screen.rightViewPort.firstRow-1, (w / 2), h, style.defaultFg, style.defaultBg) termboxUtil.FillWithChar('|', (w / 2), screen.rightViewPort.firstRow-1, (w / 2), h, style.defaultFg, style.defaultBg)
// Clear the right pane // 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 startX := (w / 2) + 2
startY := 2 startY := 3
maxScroll := len(screen.rightPaneBuffer)-screen.rightViewPort.numberOfRows maxScroll := len(screen.rightPaneBuffer) - screen.rightViewPort.numberOfRows
if maxScroll < 0 { if maxScroll < 0 {
maxScroll = 0 maxScroll = 0
} }
@ -667,105 +666,31 @@ func formatValueJSON(val []byte) ([]byte, error) {
return out, nil return out, nil
} }
func (screen *BrowserScreen) bucketToStrings(bkt *BoltBucket, style Style) []string { func (screen *BrowserScreen) bucketToLines(bkt *BoltBucket, style Style) []Line {
var ret []Line
} bfg, bbg := style.defaultFg, style.defaultBg
/* 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
if comparePaths(screen.currentPath, bkt.GetPath()) { if comparePaths(screen.currentPath, bkt.GetPath()) {
bucketFg = style.cursorFg bfg, bbg = style.cursorFg, style.cursorBg
bucketBg = style.cursorBg
} }
bktPrefix := strings.Repeat(" ", len(bkt.GetPath())*2)
prefixSpaces := strings.Repeat(" ", len(bkt.GetPath())*2)
bktString := prefixSpaces
prefixSpaces = prefixSpaces + " "
padAmt := (len(bkt.GetPath())*2 + 2)
if bkt.expanded { if bkt.expanded {
bktString = bktString + "- " + stringify([]byte(bkt.name)) ret = append(ret, Line{bktPrefix + "- " + stringify([]byte(bkt.name)), bfg, bbg})
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)
for i := range bkt.buckets { 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 { for _, bp := range bkt.pairs {
usedLines += screen.drawPair(&bkt.pairs[i], style, y+usedLines) pfg, pbg := style.defaultFg, style.defaultBg
}
} 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
if comparePaths(screen.currentPath, bp.GetPath()) { if comparePaths(screen.currentPath, bp.GetPath()) {
bucketFg = style.cursorFg pfg, pbg = style.cursorFg, style.cursorBg
bucketBg = style.cursorBg
} }
prPrefix := strings.Repeat(" ", len(bp.GetPath())*2)
prefixSpaces := strings.Repeat(" ", len(bp.GetPath())*2) pairString := fmt.Sprintf("%s%s: %s", prPrefix, stringify([]byte(bp.key)), stringify([]byte(bp.val)))
pairString := prefixSpaces ret = append(ret, Line{pairString, pfg, pbg})
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)
} }
} else { } else {
*/ ret = append(ret, Line{bktPrefix + "+ " + stringify([]byte(bkt.name)), bfg, bbg})
if w-len(pairString) > 0 {
pairString = fmt.Sprintf("%s%s", pairString, strings.Repeat(" ", (w-len(pairString))))
} }
termboxUtil.DrawStringAtPoint(pairString, 0, y, bucketFg, bucketBg) return ret
usedLines = 1
// }
return usedLines
} }
func (screen *BrowserScreen) startDeleteItem() bool { func (screen *BrowserScreen) startDeleteItem() bool {
@ -963,31 +888,6 @@ func (screen *BrowserScreen) startExportJSON() bool {
return false 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) { func (screen *BrowserScreen) setMessage(msg string) {
screen.message = msg screen.message = msg
screen.messageTime = time.Now() screen.messageTime = time.Now()