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 return vis, retErr
} }
func (bd *BoltDB) buildVisiblePathSlice() ([]string, error) { func (bd *BoltDB) buildVisiblePathSlice() ([][]string, error) {
var retSlice []string var retSlice [][]string
var retErr error var retErr error
// The root path, recurse for root buckets // The root path, recurse for root buckets
for i := range bd.buckets { for i := range bd.buckets {
@ -143,15 +143,21 @@ func (bd *BoltDB) getPrevVisiblePath(path []string) []string {
visPaths, err := bd.buildVisiblePathSlice() visPaths, err := bd.buildVisiblePathSlice()
if path == nil { if path == nil {
if len(visPaths) > 0 { if len(visPaths) > 0 {
return strings.Split(visPaths[len(visPaths)-1], "/") return visPaths[len(visPaths)-1]
} }
return nil return nil
} }
if err == nil { if err == nil {
findPath := strings.Join(path, "/") for idx, pth := range visPaths {
for i := range visPaths { isCurPath := true
if visPaths[i] == findPath && i > 0 { for i := range path {
return strings.Split(visPaths[i-1], "/") 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() visPaths, err := bd.buildVisiblePathSlice()
if path == nil { if path == nil {
if len(visPaths) > 0 { if len(visPaths) > 0 {
return strings.Split(visPaths[0], "/") return visPaths[0]
} }
return nil return nil
} }
if err == nil { if err == nil {
findPath := strings.Join(path, "/") for idx, pth := range visPaths {
for i := range visPaths { isCurPath := true
if visPaths[i] == findPath && i < len(visPaths)-1 { for i := range path {
return strings.Split(visPaths[i+1], "/") 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} 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 var retErr error
// Add this bucket to the slice // Add this bucket to the prefix slice
prefix = append(prefix, b.name) prefix = append(prefix, b.name)
retSlice = append(retSlice, strings.Join(prefix, "/")) retSlice = append(retSlice, prefix)
if b.expanded { if b.expanded {
// Add subbuckets // Add subbuckets
for i := range b.buckets { for i := range b.buckets {
@ -270,7 +286,7 @@ func (b *BoltBucket) buildVisiblePathSlice(prefix []string) ([]string, error) {
} }
// Add Pairs // Add Pairs
for i := range b.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 return retSlice, retErr

View File

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