Fixed issue with bucket/key/value having '/'

This commit is contained in:
Brian Buller 2017-04-05 11:29:28 -05:00
parent 6b6a241245
commit bbef340848
2 changed files with 86 additions and 40 deletions

View File

@ -123,8 +123,8 @@ func (bd *BoltDB) getVisibleItemCount(path []string) (int, error) {
return vis, retErr
}
func (bd *BoltDB) buildVisiblePathSlice() ([]string, error) {
var retSlice []string
func (bd *BoltDB) buildVisiblePathSlice() ([][]string, error) {
var retSlice [][]string
var retErr error
// The root path, recurse for root buckets
for i := range bd.buckets {
@ -143,15 +143,21 @@ func (bd *BoltDB) getPrevVisiblePath(path []string) []string {
visPaths, err := bd.buildVisiblePathSlice()
if path == nil {
if len(visPaths) > 0 {
return strings.Split(visPaths[len(visPaths)-1], "/")
return visPaths[len(visPaths)-1]
}
return nil
}
if err == nil {
findPath := strings.Join(path, "/")
for i := range visPaths {
if visPaths[i] == findPath && i > 0 {
return strings.Split(visPaths[i-1], "/")
for idx, pth := range visPaths {
isCurPath := true
for i := range path {
if len(pth) <= i || path[i] != pth[i] {
isCurPath = false
break
}
}
if isCurPath && idx > 0 {
return visPaths[idx-1]
}
}
}
@ -161,15 +167,21 @@ func (bd *BoltDB) getNextVisiblePath(path []string) []string {
visPaths, err := bd.buildVisiblePathSlice()
if path == nil {
if len(visPaths) > 0 {
return strings.Split(visPaths[0], "/")
return visPaths[0]
}
return nil
}
if err == nil {
findPath := strings.Join(path, "/")
for i := range visPaths {
if visPaths[i] == findPath && i < len(visPaths)-1 {
return strings.Split(visPaths[i+1], "/")
for idx, pth := range visPaths {
isCurPath := true
for i := range path {
if len(pth) <= i || path[i] != pth[i] {
isCurPath = false
break
}
}
if isCurPath && len(visPaths) > idx+1 {
return visPaths[idx+1]
}
}
}
@ -251,12 +263,16 @@ func (b *BoltBucket) GetPath() []string {
return []string{b.name}
}
func (b *BoltBucket) buildVisiblePathSlice(prefix []string) ([]string, error) {
var retSlice []string
/*
buildVisiblePathSlice builds a slice of string slices containing all visible paths in this bucket
The passed prefix is the path leading to the current bucket
*/
func (b *BoltBucket) buildVisiblePathSlice(prefix []string) ([][]string, error) {
var retSlice [][]string
var retErr error
// Add this bucket to the slice
// Add this bucket to the prefix slice
prefix = append(prefix, b.name)
retSlice = append(retSlice, strings.Join(prefix, "/"))
retSlice = append(retSlice, prefix)
if b.expanded {
// Add subbuckets
for i := range b.buckets {
@ -270,7 +286,7 @@ func (b *BoltBucket) buildVisiblePathSlice(prefix []string) ([]string, error) {
}
// Add Pairs
for i := range b.pairs {
retSlice = append(retSlice, strings.Join(prefix, "/")+"/"+b.pairs[i].key)
retSlice = append(retSlice, append(prefix, b.pairs[i].key))
}
}
return retSlice, retErr

View File

@ -390,21 +390,30 @@ func (screen *BrowserScreen) jumpCursorUp(distance int) bool {
// Jump up 'distance' lines
visPaths, err := screen.db.buildVisiblePathSlice()
if err == nil {
findPath := strings.Join(screen.currentPath, "/")
startJump := false
for i := range visPaths {
if visPaths[len(visPaths)-1-i] == findPath {
startJump = true
findPath := screen.currentPath
for idx, pth := range visPaths {
startJump := true
for i := range pth {
if len(screen.currentPath) > i && pth[i] != screen.currentPath[i] {
startJump = false
}
}
if startJump {
distance--
if distance == 0 {
screen.currentPath = strings.Split(visPaths[len(visPaths)-1-i], "/")
screen.currentPath = visPaths[len(visPaths)-1-idx]
break
}
}
}
if strings.Join(screen.currentPath, "/") == findPath {
isCurPath := true
for i := range screen.currentPath {
if screen.currentPath[i] != findPath[i] {
isCurPath = false
break
}
}
if isCurPath {
screen.currentPath = screen.db.getNextVisiblePath(nil)
}
}
@ -413,22 +422,32 @@ func (screen *BrowserScreen) jumpCursorUp(distance int) bool {
func (screen *BrowserScreen) jumpCursorDown(distance int) bool {
visPaths, err := screen.db.buildVisiblePathSlice()
if err == nil {
findPath := strings.Join(screen.currentPath, "/")
startJump := false
for i := range visPaths {
if visPaths[i] == findPath {
startJump = true
findPath := screen.currentPath
for idx, pth := range visPaths {
startJump := true
for i := range pth {
if len(screen.currentPath) > i && pth[i] != screen.currentPath[i] {
startJump = false
}
}
if startJump {
distance--
if distance == 0 {
screen.currentPath = strings.Split(visPaths[i], "/")
screen.currentPath = visPaths[idx]
break
}
}
}
if strings.Join(screen.currentPath, "/") == findPath {
screen.currentPath = screen.db.getPrevVisiblePath(nil)
isCurPath := true
for i := range screen.currentPath {
if screen.currentPath[i] != findPath[i] {
isCurPath = false
break
}
}
if isCurPath {
screen.currentPath = screen.db.getNextVisiblePath(nil)
}
}
return true
@ -510,11 +529,18 @@ func (screen *BrowserScreen) drawLeftPane(style Style) {
// So we know how much of the tree _wants_ to be visible
// we only have screen.viewPort.numberOfRows of space though
curPathSpot := 0
visSlice, err := screen.db.buildVisiblePathSlice()
visPaths, err := screen.db.buildVisiblePathSlice()
if err == nil {
for i := range visSlice {
if strings.Join(screen.currentPath, "/") == visSlice[i] {
curPathSpot = i
for idx, pth := range visPaths {
isCurPath := true
for i := range pth {
if len(screen.currentPath) > i && pth[i] != screen.currentPath[i] {
isCurPath = false
break
}
}
if isCurPath {
curPathSpot = idx
}
}
}
@ -541,24 +567,28 @@ func (screen *BrowserScreen) drawRightPane(style Style) {
termboxUtil.FillWithChar(' ', (w/2)+1, screen.viewPort.firstRow, w, h, style.defaultFg, style.defaultBg)
b, p, err := screen.db.getGenericFromPath(screen.currentPath)
startX := (w / 2) + 2
startY := 2
if err == nil {
startX := (w / 2) + 2
startY := 2
if b != nil {
pathString := fmt.Sprintf("Path: %s", strings.Join(b.GetPath(), "/"))
pathString := fmt.Sprintf("Path: %s", strings.Join(b.GetPath(), " → "))
startY += screen.drawMultilineText(pathString, 6, startX, startY, (w/2)-1, style.defaultFg, style.defaultBg)
bucketString := fmt.Sprintf("Buckets: %d", len(b.buckets))
startY += screen.drawMultilineText(bucketString, 9, startX, startY, (w/2)-1, style.defaultFg, style.defaultBg)
pairsString := fmt.Sprintf("Pairs: %d", len(b.pairs))
startY += screen.drawMultilineText(pairsString, 7, startX, startY, (w/2)-1, style.defaultFg, style.defaultBg)
} else if p != nil {
pathString := fmt.Sprintf("Path: %s", strings.Join(p.GetPath(), "/"))
pathString := fmt.Sprintf("Path: %s", strings.Join(p.GetPath(), ""))
startY += screen.drawMultilineText(pathString, 6, startX, startY, (w/2)-1, style.defaultFg, style.defaultBg)
keyString := fmt.Sprintf("Key: %s", p.key)
startY += screen.drawMultilineText(keyString, 5, startX, startY, (w/2)-1, style.defaultFg, style.defaultBg)
valString := fmt.Sprintf("Value: %s", p.val)
startY += screen.drawMultilineText(valString, 7, startX, startY, (w/2)-1, style.defaultFg, style.defaultBg)
}
} else {
pathString := fmt.Sprintf("Path: %s", strings.Join(screen.currentPath, " → "))
startY += screen.drawMultilineText(pathString, 6, startX, startY, (w/2)-1, style.defaultFg, style.defaultBg)
startY += screen.drawMultilineText(err.Error(), 6, startX, startY, (w/2)-1, style.defaultFg, style.defaultBg)
}
}
}