2015-05-18 14:47:08 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-10-07 12:43:10 +00:00
|
|
|
"encoding/json"
|
2015-05-18 14:47:08 +00:00
|
|
|
"fmt"
|
2020-04-08 08:09:44 +00:00
|
|
|
"path/filepath"
|
2015-05-18 14:47:08 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2015-09-17 16:49:57 +00:00
|
|
|
|
2019-10-17 15:49:08 +00:00
|
|
|
termboxUtil "github.com/br0xen/termbox-util"
|
2015-09-17 16:49:57 +00:00
|
|
|
"github.com/nsf/termbox-go"
|
2015-05-18 14:47:08 +00:00
|
|
|
)
|
|
|
|
|
2015-09-17 16:49:57 +00:00
|
|
|
/*
|
|
|
|
ViewPort helps keep track of what's being displayed on the screen
|
|
|
|
*/
|
2015-05-18 14:47:08 +00:00
|
|
|
type ViewPort struct {
|
2015-09-17 16:49:57 +00:00
|
|
|
bytesPerRow int
|
|
|
|
numberOfRows int
|
|
|
|
firstRow int
|
2019-10-17 15:49:08 +00:00
|
|
|
scrollRow int
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
|
|
|
|
2015-09-17 16:49:57 +00:00
|
|
|
/*
|
|
|
|
BrowserScreen holds all that's going on :D
|
|
|
|
*/
|
2015-05-18 14:47:08 +00:00
|
|
|
type BrowserScreen struct {
|
2015-09-17 16:49:57 +00:00
|
|
|
db *BoltDB
|
2019-10-17 15:49:08 +00:00
|
|
|
leftViewPort ViewPort
|
|
|
|
rightViewPort ViewPort
|
2015-09-17 16:49:57 +00:00
|
|
|
queuedCommand string
|
|
|
|
currentPath []string
|
|
|
|
currentType int
|
|
|
|
message string
|
2019-12-23 13:31:44 +00:00
|
|
|
filter string
|
2015-09-17 16:49:57 +00:00
|
|
|
mode BrowserMode
|
2015-10-21 16:47:34 +00:00
|
|
|
inputModal *termboxUtil.InputModal
|
|
|
|
confirmModal *termboxUtil.ConfirmModal
|
2015-09-17 16:49:57 +00:00
|
|
|
messageTimeout time.Duration
|
|
|
|
messageTime time.Time
|
2017-04-05 17:49:05 +00:00
|
|
|
|
2019-10-16 22:41:29 +00:00
|
|
|
leftPaneBuffer []Line
|
|
|
|
rightPaneBuffer []Line
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
|
|
|
|
2015-09-17 16:49:57 +00:00
|
|
|
/*
|
|
|
|
BrowserMode is just for designating the mode that we're in
|
|
|
|
*/
|
2015-05-18 14:47:08 +00:00
|
|
|
type BrowserMode int
|
|
|
|
|
|
|
|
const (
|
2015-09-17 16:49:57 +00:00
|
|
|
modeBrowse = 16 // 0000 0001 0000
|
|
|
|
modeChange = 32 // 0000 0010 0000
|
|
|
|
modeChangeKey = 33 // 0000 0010 0001
|
|
|
|
modeChangeVal = 34 // 0000 0010 0010
|
2019-12-23 13:31:44 +00:00
|
|
|
modeFilter = 35 // 0100 0010 0011
|
2015-09-17 16:49:57 +00:00
|
|
|
modeInsert = 64 // 0000 0100 0000
|
|
|
|
modeInsertBucket = 65 // 0000 0100 0001
|
|
|
|
modeInsertPair = 68 // 0000 0100 0100
|
|
|
|
modeInsertPairKey = 69 // 0000 0100 0101
|
2016-12-29 21:23:58 +00:00
|
|
|
modeInsertPairVal = 70 // 0000 0100 0110
|
2015-09-17 16:49:57 +00:00
|
|
|
modeDelete = 256 // 0001 0000 0000
|
|
|
|
modeModToParent = 8 // 0000 0000 1000
|
2023-05-12 13:27:33 +00:00
|
|
|
modeIO = 512 // 0010 0000 0000
|
|
|
|
modeIOExportValue = 513 // 0010 0000 0001
|
|
|
|
modeIOExportJSON = 514 // 0010 0000 0010
|
|
|
|
modeIOImportValue = 516 // 0010 0000 0100
|
2015-05-18 14:47:08 +00:00
|
|
|
)
|
|
|
|
|
2015-09-17 16:49:57 +00:00
|
|
|
/*
|
|
|
|
BoltType is just for tracking what type of db item we're looking at
|
|
|
|
*/
|
2015-05-18 14:47:08 +00:00
|
|
|
type BoltType int
|
|
|
|
|
|
|
|
const (
|
2015-09-17 16:49:57 +00:00
|
|
|
typeBucket = iota
|
|
|
|
typePair
|
2015-05-18 14:47:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (screen *BrowserScreen) handleKeyEvent(event termbox.Event) int {
|
|
|
|
if screen.mode == 0 {
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.mode = modeBrowse
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
2015-09-17 16:49:57 +00:00
|
|
|
if screen.mode == modeBrowse {
|
2015-05-18 14:47:08 +00:00
|
|
|
return screen.handleBrowseKeyEvent(event)
|
2015-09-17 16:49:57 +00:00
|
|
|
} else if screen.mode&modeChange == modeChange {
|
2015-05-18 14:47:08 +00:00
|
|
|
return screen.handleInputKeyEvent(event)
|
2015-09-17 16:49:57 +00:00
|
|
|
} else if screen.mode&modeInsert == modeInsert {
|
2015-05-18 14:47:08 +00:00
|
|
|
return screen.handleInsertKeyEvent(event)
|
2015-09-17 16:49:57 +00:00
|
|
|
} else if screen.mode == modeDelete {
|
2015-05-18 14:47:08 +00:00
|
|
|
return screen.handleDeleteKeyEvent(event)
|
2023-05-12 13:27:33 +00:00
|
|
|
} else if screen.mode&modeIO == modeIO {
|
|
|
|
return screen.handleIOKeyEvent(event)
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
2015-09-17 16:49:57 +00:00
|
|
|
return BrowserScreenIndex
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (screen *BrowserScreen) handleBrowseKeyEvent(event termbox.Event) int {
|
|
|
|
if event.Ch == '?' {
|
|
|
|
// About
|
2015-09-17 16:49:57 +00:00
|
|
|
return AboutScreenIndex
|
2015-05-18 14:47:08 +00:00
|
|
|
|
|
|
|
} else if event.Ch == 'q' || event.Key == termbox.KeyEsc || event.Key == termbox.KeyCtrlC {
|
|
|
|
// Quit
|
2015-09-17 16:49:57 +00:00
|
|
|
return ExitScreenIndex
|
2015-05-18 14:47:08 +00:00
|
|
|
|
|
|
|
} else if event.Ch == 'g' {
|
|
|
|
// Jump to Beginning
|
2019-12-23 13:31:44 +00:00
|
|
|
screen.currentPath = screen.db.getNextVisiblePath(nil, screen.filter)
|
2015-05-18 14:47:08 +00:00
|
|
|
|
|
|
|
} else if event.Ch == 'G' {
|
|
|
|
// Jump to End
|
2019-12-23 13:31:44 +00:00
|
|
|
screen.currentPath = screen.db.getPrevVisiblePath(nil, screen.filter)
|
2015-05-18 14:47:08 +00:00
|
|
|
|
|
|
|
} else if event.Key == termbox.KeyCtrlR {
|
|
|
|
screen.refreshDatabase()
|
|
|
|
|
|
|
|
} else if event.Key == termbox.KeyCtrlF {
|
|
|
|
// Jump forward half a screen
|
|
|
|
_, h := termbox.Size()
|
|
|
|
half := h / 2
|
|
|
|
screen.jumpCursorDown(half)
|
|
|
|
|
|
|
|
} else if event.Key == termbox.KeyCtrlB {
|
|
|
|
_, h := termbox.Size()
|
|
|
|
half := h / 2
|
|
|
|
screen.jumpCursorUp(half)
|
|
|
|
|
|
|
|
} else if event.Ch == 'j' || event.Key == termbox.KeyArrowDown {
|
|
|
|
screen.moveCursorDown()
|
|
|
|
|
|
|
|
} else if event.Ch == 'k' || event.Key == termbox.KeyArrowUp {
|
|
|
|
screen.moveCursorUp()
|
|
|
|
|
2017-08-30 13:55:08 +00:00
|
|
|
} else if event.Ch == 'J' {
|
2017-04-05 17:49:05 +00:00
|
|
|
screen.moveRightPaneDown()
|
|
|
|
|
2017-08-30 13:55:08 +00:00
|
|
|
} else if event.Ch == 'K' {
|
2017-04-05 17:49:05 +00:00
|
|
|
screen.moveRightPaneUp()
|
|
|
|
|
2015-05-18 14:47:08 +00:00
|
|
|
} else if event.Ch == 'p' {
|
|
|
|
// p creates a new pair at the current level
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.startInsertItem(typePair)
|
2015-05-18 14:47:08 +00:00
|
|
|
} else if event.Ch == 'P' {
|
|
|
|
// P creates a new pair at the parent level
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.startInsertItemAtParent(typePair)
|
2015-05-18 14:47:08 +00:00
|
|
|
|
|
|
|
} else if event.Ch == 'b' {
|
|
|
|
// b creates a new bucket at the current level
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.startInsertItem(typeBucket)
|
2015-05-18 14:47:08 +00:00
|
|
|
} else if event.Ch == 'B' {
|
|
|
|
// B creates a new bucket at the parent level
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.startInsertItemAtParent(typeBucket)
|
2015-05-18 14:47:08 +00:00
|
|
|
|
|
|
|
} else if event.Ch == 'e' {
|
2015-09-17 16:49:57 +00:00
|
|
|
b, p, _ := screen.db.getGenericFromPath(screen.currentPath)
|
2015-05-18 14:47:08 +00:00
|
|
|
if b != nil {
|
|
|
|
screen.setMessage("Cannot edit a bucket, did you mean to (r)ename?")
|
|
|
|
} else if p != nil {
|
|
|
|
screen.startEditItem()
|
|
|
|
}
|
2019-12-23 13:31:44 +00:00
|
|
|
} else if event.Ch == '/' {
|
|
|
|
screen.startFilter()
|
2015-05-18 14:47:08 +00:00
|
|
|
|
|
|
|
} else if event.Ch == 'r' {
|
|
|
|
screen.startRenameItem()
|
|
|
|
|
|
|
|
} else if event.Key == termbox.KeyEnter {
|
2015-09-17 16:49:57 +00:00
|
|
|
b, p, _ := screen.db.getGenericFromPath(screen.currentPath)
|
2015-05-18 14:47:08 +00:00
|
|
|
if b != nil {
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.db.toggleOpenBucket(screen.currentPath)
|
2015-05-18 14:47:08 +00:00
|
|
|
} else if p != nil {
|
|
|
|
screen.startEditItem()
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if event.Ch == 'l' || event.Key == termbox.KeyArrowRight {
|
2015-09-17 16:49:57 +00:00
|
|
|
b, p, _ := screen.db.getGenericFromPath(screen.currentPath)
|
2015-05-18 14:47:08 +00:00
|
|
|
// Select the current item
|
|
|
|
if b != nil {
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.db.toggleOpenBucket(screen.currentPath)
|
2015-05-18 14:47:08 +00:00
|
|
|
} else if p != nil {
|
|
|
|
screen.startEditItem()
|
|
|
|
} else {
|
|
|
|
screen.setMessage("Not sure what to do here...")
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if event.Ch == 'h' || event.Key == termbox.KeyArrowLeft {
|
|
|
|
// If we are _on_ a bucket that's open, close it
|
2015-09-17 16:49:57 +00:00
|
|
|
b, _, e := screen.db.getGenericFromPath(screen.currentPath)
|
2015-05-18 14:47:08 +00:00
|
|
|
if e == nil && b != nil && b.expanded {
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.db.closeBucket(screen.currentPath)
|
2015-05-18 14:47:08 +00:00
|
|
|
} else {
|
2015-09-17 16:49:57 +00:00
|
|
|
if len(screen.currentPath) > 1 {
|
|
|
|
parentBucket, err := screen.db.getBucketFromPath(screen.currentPath[:len(screen.currentPath)-1])
|
2015-05-18 14:47:08 +00:00
|
|
|
if err == nil {
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.db.closeBucket(parentBucket.GetPath())
|
2015-05-18 14:47:08 +00:00
|
|
|
// Figure out how far up we need to move the cursor
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.currentPath = parentBucket.GetPath()
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.db.closeBucket(screen.currentPath)
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if event.Ch == 'D' {
|
|
|
|
screen.startDeleteItem()
|
2016-12-29 21:23:58 +00:00
|
|
|
} else if event.Ch == 'x' {
|
2023-05-12 13:27:33 +00:00
|
|
|
// Export Value to a file
|
2016-12-29 21:23:58 +00:00
|
|
|
screen.startExportValue()
|
|
|
|
} else if event.Ch == 'X' {
|
|
|
|
// Export Key/Value (or Bucket) as JSON
|
|
|
|
screen.startExportJSON()
|
2023-05-12 13:27:33 +00:00
|
|
|
} else if event.Ch == 'i' {
|
|
|
|
// Import value from a file
|
|
|
|
screen.startImportValue()
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
2015-09-17 16:49:57 +00:00
|
|
|
return BrowserScreenIndex
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (screen *BrowserScreen) handleInputKeyEvent(event termbox.Event) int {
|
|
|
|
if event.Key == termbox.KeyEsc {
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.mode = modeBrowse
|
|
|
|
screen.inputModal.Clear()
|
2015-05-18 14:47:08 +00:00
|
|
|
} else {
|
2016-02-19 14:48:51 +00:00
|
|
|
screen.inputModal.HandleEvent(event)
|
2015-09-17 16:49:57 +00:00
|
|
|
if screen.inputModal.IsDone() {
|
|
|
|
b, p, _ := screen.db.getGenericFromPath(screen.currentPath)
|
2019-12-23 13:31:44 +00:00
|
|
|
if screen.mode == modeFilter {
|
|
|
|
screen.filter = screen.inputModal.GetValue()
|
|
|
|
if !screen.db.isVisiblePath(screen.currentPath, screen.filter) {
|
|
|
|
screen.currentPath = screen.currentPath[:len(screen.currentPath)-1]
|
|
|
|
}
|
|
|
|
}
|
2015-05-18 14:47:08 +00:00
|
|
|
if b != nil {
|
2015-09-17 16:49:57 +00:00
|
|
|
if screen.mode == modeChangeKey {
|
|
|
|
newName := screen.inputModal.GetValue()
|
|
|
|
if renameBucket(screen.currentPath, newName) != nil {
|
2015-05-18 14:47:08 +00:00
|
|
|
screen.setMessage("Error renaming bucket.")
|
|
|
|
} else {
|
2015-09-17 16:49:57 +00:00
|
|
|
b.name = newName
|
|
|
|
screen.currentPath[len(screen.currentPath)-1] = b.name
|
2015-05-18 14:47:08 +00:00
|
|
|
screen.setMessage("Bucket Renamed!")
|
|
|
|
screen.refreshDatabase()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if p != nil {
|
2015-09-17 16:49:57 +00:00
|
|
|
if screen.mode == modeChangeKey {
|
|
|
|
newKey := screen.inputModal.GetValue()
|
2021-05-31 14:29:09 +00:00
|
|
|
if err := updatePairKey(screen.currentPath, newKey); err != nil {
|
2015-05-18 14:47:08 +00:00
|
|
|
screen.setMessage("Error occurred updating Pair.")
|
|
|
|
} else {
|
2015-09-17 16:49:57 +00:00
|
|
|
p.key = newKey
|
|
|
|
screen.currentPath[len(screen.currentPath)-1] = p.key
|
2015-05-18 14:47:08 +00:00
|
|
|
screen.setMessage("Pair updated!")
|
|
|
|
screen.refreshDatabase()
|
|
|
|
}
|
2015-09-17 16:49:57 +00:00
|
|
|
} else if screen.mode == modeChangeVal {
|
|
|
|
newVal := screen.inputModal.GetValue()
|
2021-05-31 14:29:09 +00:00
|
|
|
if err := updatePairValue(screen.currentPath, newVal); err != nil {
|
2015-05-18 14:47:08 +00:00
|
|
|
screen.setMessage("Error occurred updating Pair.")
|
|
|
|
} else {
|
2015-09-17 16:49:57 +00:00
|
|
|
p.val = newVal
|
2015-05-18 14:47:08 +00:00
|
|
|
screen.setMessage("Pair updated!")
|
|
|
|
screen.refreshDatabase()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.mode = modeBrowse
|
|
|
|
screen.inputModal.Clear()
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-17 16:49:57 +00:00
|
|
|
return BrowserScreenIndex
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (screen *BrowserScreen) handleDeleteKeyEvent(event termbox.Event) int {
|
2016-02-19 14:48:51 +00:00
|
|
|
screen.confirmModal.HandleEvent(event)
|
2015-09-17 16:49:57 +00:00
|
|
|
if screen.confirmModal.IsDone() {
|
|
|
|
if screen.confirmModal.IsAccepted() {
|
2019-12-23 13:31:44 +00:00
|
|
|
holdNextPath := screen.db.getNextVisiblePath(screen.currentPath, screen.filter)
|
|
|
|
holdPrevPath := screen.db.getPrevVisiblePath(screen.currentPath, screen.filter)
|
2015-09-17 16:49:57 +00:00
|
|
|
if deleteKey(screen.currentPath) == nil {
|
2015-05-18 14:47:08 +00:00
|
|
|
screen.refreshDatabase()
|
|
|
|
// Move the current path endpoint appropriately
|
|
|
|
//found_new_path := false
|
2015-09-17 16:49:57 +00:00
|
|
|
if holdNextPath != nil {
|
|
|
|
if len(holdNextPath) > 2 {
|
|
|
|
if holdNextPath[len(holdNextPath)-2] == screen.currentPath[len(screen.currentPath)-2] {
|
|
|
|
screen.currentPath = holdNextPath
|
|
|
|
} else if holdPrevPath != nil {
|
|
|
|
screen.currentPath = holdPrevPath
|
2015-05-18 14:47:08 +00:00
|
|
|
} else {
|
|
|
|
// Otherwise, go to the parent
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.currentPath = screen.currentPath[:(len(holdNextPath) - 2)]
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Root bucket deleted, set to next
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.currentPath = holdNextPath
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
2015-09-17 16:49:57 +00:00
|
|
|
} else if holdPrevPath != nil {
|
|
|
|
screen.currentPath = holdPrevPath
|
2015-05-18 14:47:08 +00:00
|
|
|
} else {
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.currentPath = screen.currentPath[:0]
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.mode = modeBrowse
|
|
|
|
screen.confirmModal.Clear()
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
2015-09-17 16:49:57 +00:00
|
|
|
return BrowserScreenIndex
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (screen *BrowserScreen) handleInsertKeyEvent(event termbox.Event) int {
|
|
|
|
if event.Key == termbox.KeyEsc {
|
|
|
|
if len(screen.db.buckets) == 0 {
|
2015-09-17 16:49:57 +00:00
|
|
|
return ExitScreenIndex
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.mode = modeBrowse
|
|
|
|
screen.inputModal.Clear()
|
2015-05-18 14:47:08 +00:00
|
|
|
} else {
|
2016-02-19 14:48:51 +00:00
|
|
|
screen.inputModal.HandleEvent(event)
|
2015-09-17 16:49:57 +00:00
|
|
|
if screen.inputModal.IsDone() {
|
|
|
|
newVal := screen.inputModal.GetValue()
|
|
|
|
screen.inputModal.Clear()
|
|
|
|
var insertPath []string
|
|
|
|
if len(screen.currentPath) > 0 {
|
|
|
|
_, p, e := screen.db.getGenericFromPath(screen.currentPath)
|
2015-05-18 14:47:08 +00:00
|
|
|
if e != nil {
|
|
|
|
screen.setMessage("Error Inserting new item. Invalid Path.")
|
|
|
|
}
|
2015-09-17 16:49:57 +00:00
|
|
|
insertPath = screen.currentPath
|
2015-05-18 14:47:08 +00:00
|
|
|
// where are we inserting?
|
|
|
|
if p != nil {
|
|
|
|
// If we're sitting on a pair, we have to go to it's parent
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.mode = screen.mode | modeModToParent
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
2015-09-17 16:49:57 +00:00
|
|
|
if screen.mode&modeModToParent == modeModToParent {
|
|
|
|
if len(screen.currentPath) > 1 {
|
|
|
|
insertPath = screen.currentPath[:len(screen.currentPath)-1]
|
2015-05-18 14:47:08 +00:00
|
|
|
} else {
|
2015-09-17 16:49:57 +00:00
|
|
|
insertPath = make([]string, 0)
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-17 16:49:57 +00:00
|
|
|
parentB, _, _ := screen.db.getGenericFromPath(insertPath)
|
|
|
|
if screen.mode&modeInsertBucket == modeInsertBucket {
|
|
|
|
err := insertBucket(insertPath, newVal)
|
2015-05-18 14:47:08 +00:00
|
|
|
if err != nil {
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.setMessage(fmt.Sprintf("%s => %s", err, insertPath))
|
2015-05-18 14:47:08 +00:00
|
|
|
} else {
|
2015-09-17 16:49:57 +00:00
|
|
|
if parentB != nil {
|
|
|
|
parentB.expanded = true
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.currentPath = append(insertPath, newVal)
|
2015-05-18 14:47:08 +00:00
|
|
|
|
|
|
|
screen.refreshDatabase()
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.mode = modeBrowse
|
|
|
|
screen.inputModal.Clear()
|
|
|
|
} else if screen.mode&modeInsertPair == modeInsertPair {
|
|
|
|
err := insertPair(insertPath, newVal, "")
|
2015-05-18 14:47:08 +00:00
|
|
|
if err != nil {
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.setMessage(fmt.Sprintf("%s => %s", err, insertPath))
|
2015-05-18 14:47:08 +00:00
|
|
|
screen.refreshDatabase()
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.mode = modeBrowse
|
|
|
|
screen.inputModal.Clear()
|
2015-05-18 14:47:08 +00:00
|
|
|
} else {
|
2015-09-17 16:49:57 +00:00
|
|
|
if parentB != nil {
|
|
|
|
parentB.expanded = true
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.currentPath = append(insertPath, newVal)
|
2015-05-18 14:47:08 +00:00
|
|
|
screen.refreshDatabase()
|
|
|
|
screen.startEditItem()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-17 16:49:57 +00:00
|
|
|
return BrowserScreenIndex
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:27:33 +00:00
|
|
|
func (screen *BrowserScreen) handleIOKeyEvent(event termbox.Event) int {
|
2016-12-29 21:23:58 +00:00
|
|
|
if event.Key == termbox.KeyEsc {
|
|
|
|
screen.mode = modeBrowse
|
|
|
|
screen.inputModal.Clear()
|
|
|
|
} else {
|
|
|
|
screen.inputModal.HandleEvent(event)
|
|
|
|
if screen.inputModal.IsDone() {
|
|
|
|
b, p, _ := screen.db.getGenericFromPath(screen.currentPath)
|
|
|
|
fileName := screen.inputModal.GetValue()
|
2023-05-12 13:27:33 +00:00
|
|
|
if screen.mode&modeIOExportValue == modeIOExportValue {
|
2016-12-29 21:23:58 +00:00
|
|
|
// Exporting the value
|
|
|
|
if p != nil {
|
|
|
|
if err := exportValue(screen.currentPath, fileName); err != nil {
|
2023-05-12 13:27:33 +00:00
|
|
|
//screen.setMessage("Error exporting to file " + fileName + ".")
|
2016-12-29 21:23:58 +00:00
|
|
|
screen.setMessage(err.Error())
|
|
|
|
} else {
|
|
|
|
screen.setMessage("Value exported to file: " + fileName)
|
|
|
|
}
|
|
|
|
}
|
2023-05-12 13:27:33 +00:00
|
|
|
} else if screen.mode&modeIOExportJSON == modeIOExportJSON {
|
2016-12-29 21:23:58 +00:00
|
|
|
if b != nil || p != nil {
|
|
|
|
if exportJSON(screen.currentPath, fileName) != nil {
|
2023-05-12 13:27:33 +00:00
|
|
|
screen.setMessage("Error exporting to file " + fileName + ".")
|
2016-12-29 21:23:58 +00:00
|
|
|
} else {
|
|
|
|
screen.setMessage("Value exported to file: " + fileName)
|
|
|
|
}
|
|
|
|
}
|
2023-05-12 13:27:33 +00:00
|
|
|
} else if screen.mode&modeIOImportValue == modeIOImportValue {
|
|
|
|
if p != nil {
|
|
|
|
if err := importValue(screen.currentPath, fileName); err != nil {
|
|
|
|
screen.setMessage(err.Error())
|
|
|
|
} else {
|
|
|
|
screen.setMessage("Value imported from file: " + fileName)
|
|
|
|
screen.refreshDatabase()
|
|
|
|
}
|
|
|
|
}
|
2016-12-29 21:23:58 +00:00
|
|
|
}
|
|
|
|
screen.mode = modeBrowse
|
|
|
|
screen.inputModal.Clear()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return BrowserScreenIndex
|
|
|
|
}
|
|
|
|
|
2015-05-18 14:47:08 +00:00
|
|
|
func (screen *BrowserScreen) jumpCursorUp(distance int) bool {
|
|
|
|
// Jump up 'distance' lines
|
2019-12-23 13:31:44 +00:00
|
|
|
visPaths, err := screen.db.buildVisiblePathSlice(screen.filter)
|
2015-05-18 14:47:08 +00:00
|
|
|
if err == nil {
|
2017-04-05 16:29:28 +00:00
|
|
|
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
|
|
|
|
}
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
2015-09-17 16:49:57 +00:00
|
|
|
if startJump {
|
|
|
|
distance--
|
2015-05-18 14:47:08 +00:00
|
|
|
if distance == 0 {
|
2017-04-05 16:29:28 +00:00
|
|
|
screen.currentPath = visPaths[len(visPaths)-1-idx]
|
2015-05-18 14:47:08 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-05 16:29:28 +00:00
|
|
|
isCurPath := true
|
|
|
|
for i := range screen.currentPath {
|
|
|
|
if screen.currentPath[i] != findPath[i] {
|
|
|
|
isCurPath = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if isCurPath {
|
2019-12-23 13:31:44 +00:00
|
|
|
screen.currentPath = screen.db.getNextVisiblePath(nil, screen.filter)
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
func (screen *BrowserScreen) jumpCursorDown(distance int) bool {
|
2019-12-23 13:31:44 +00:00
|
|
|
visPaths, err := screen.db.buildVisiblePathSlice(screen.filter)
|
2015-05-18 14:47:08 +00:00
|
|
|
if err == nil {
|
2017-04-05 16:29:28 +00:00
|
|
|
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
|
|
|
|
}
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
2015-09-17 16:49:57 +00:00
|
|
|
if startJump {
|
|
|
|
distance--
|
2015-05-18 14:47:08 +00:00
|
|
|
if distance == 0 {
|
2017-04-05 16:29:28 +00:00
|
|
|
screen.currentPath = visPaths[idx]
|
2015-05-18 14:47:08 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-05 16:29:28 +00:00
|
|
|
isCurPath := true
|
|
|
|
for i := range screen.currentPath {
|
|
|
|
if screen.currentPath[i] != findPath[i] {
|
|
|
|
isCurPath = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if isCurPath {
|
2019-12-23 13:31:44 +00:00
|
|
|
screen.currentPath = screen.db.getNextVisiblePath(nil, screen.filter)
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (screen *BrowserScreen) moveCursorUp() bool {
|
2019-12-23 13:31:44 +00:00
|
|
|
newPath := screen.db.getPrevVisiblePath(screen.currentPath, screen.filter)
|
2015-09-17 16:49:57 +00:00
|
|
|
if newPath != nil {
|
|
|
|
screen.currentPath = newPath
|
2015-05-18 14:47:08 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
func (screen *BrowserScreen) moveCursorDown() bool {
|
2019-12-23 13:31:44 +00:00
|
|
|
newPath := screen.db.getNextVisiblePath(screen.currentPath, screen.filter)
|
2015-09-17 16:49:57 +00:00
|
|
|
if newPath != nil {
|
|
|
|
screen.currentPath = newPath
|
2015-05-18 14:47:08 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2017-04-05 17:49:05 +00:00
|
|
|
func (screen *BrowserScreen) moveRightPaneUp() bool {
|
2019-10-17 15:49:08 +00:00
|
|
|
if screen.rightViewPort.scrollRow > 0 {
|
|
|
|
screen.rightViewPort.scrollRow--
|
|
|
|
return true
|
|
|
|
}
|
2017-04-05 17:49:05 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
func (screen *BrowserScreen) moveRightPaneDown() bool {
|
2019-10-17 15:49:08 +00:00
|
|
|
screen.rightViewPort.scrollRow++
|
|
|
|
return true
|
2017-04-05 17:49:05 +00:00
|
|
|
}
|
2015-05-18 14:47:08 +00:00
|
|
|
|
|
|
|
func (screen *BrowserScreen) performLayout() {}
|
|
|
|
|
|
|
|
func (screen *BrowserScreen) drawScreen(style Style) {
|
2017-04-05 03:13:50 +00:00
|
|
|
if screen.db == nil {
|
|
|
|
screen.drawHeader(style)
|
|
|
|
screen.setMessage("Invalid DB. Press 'q' to quit, '?' for help")
|
|
|
|
screen.drawFooter(style)
|
|
|
|
return
|
|
|
|
}
|
2015-09-17 16:49:57 +00:00
|
|
|
if len(screen.db.buckets) == 0 && screen.mode&modeInsertBucket != modeInsertBucket {
|
2015-05-18 14:47:08 +00:00
|
|
|
// Force a bucket insert
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.startInsertItemAtParent(typeBucket)
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
|
|
|
if screen.message == "" {
|
|
|
|
screen.setMessageWithTimeout("Press '?' for help", -1)
|
|
|
|
}
|
|
|
|
screen.drawLeftPane(style)
|
|
|
|
screen.drawRightPane(style)
|
|
|
|
screen.drawHeader(style)
|
|
|
|
screen.drawFooter(style)
|
|
|
|
|
2015-09-17 16:49:57 +00:00
|
|
|
if screen.inputModal != nil {
|
|
|
|
screen.inputModal.Draw()
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
2015-09-17 16:49:57 +00:00
|
|
|
if screen.mode == modeDelete {
|
|
|
|
screen.confirmModal.Draw()
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (screen *BrowserScreen) drawHeader(style Style) {
|
|
|
|
width, _ := termbox.Size()
|
2020-04-08 08:09:44 +00:00
|
|
|
headerStringLen := func(fileName string) int {
|
|
|
|
return len(ProgramName) + len(fileName) + 1
|
|
|
|
}
|
|
|
|
headerFileName := currentFilename
|
|
|
|
if headerStringLen(headerFileName) > width {
|
|
|
|
headerFileName = filepath.Base(headerFileName)
|
|
|
|
}
|
|
|
|
headerString := ProgramName + ": " + headerFileName
|
2021-05-31 14:29:09 +00:00
|
|
|
count := ((width - len(headerString)) / 2) + 1
|
2020-04-08 08:09:44 +00:00
|
|
|
if count < 0 {
|
|
|
|
count = 0
|
|
|
|
}
|
|
|
|
spaces := strings.Repeat(" ", count)
|
2017-04-04 21:41:15 +00:00
|
|
|
termboxUtil.DrawStringAtPoint(fmt.Sprintf("%s%s%s", spaces, headerString, spaces), 0, 0, style.titleFg, style.titleBg)
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
2020-04-08 08:09:44 +00:00
|
|
|
|
2015-05-18 14:47:08 +00:00
|
|
|
func (screen *BrowserScreen) drawFooter(style Style) {
|
2015-09-17 16:49:57 +00:00
|
|
|
if screen.messageTimeout > 0 && time.Since(screen.messageTime) > screen.messageTimeout {
|
2015-05-18 14:47:08 +00:00
|
|
|
screen.clearMessage()
|
|
|
|
}
|
|
|
|
_, height := termbox.Size()
|
2015-10-21 16:47:34 +00:00
|
|
|
termboxUtil.DrawStringAtPoint(screen.message, 0, height-1, style.defaultFg, style.defaultBg)
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
2019-10-16 22:41:29 +00:00
|
|
|
|
|
|
|
func (screen *BrowserScreen) buildLeftPane(style Style) {
|
2019-10-17 15:49:08 +00:00
|
|
|
screen.leftPaneBuffer = nil
|
2019-10-16 22:41:29 +00:00
|
|
|
if len(screen.currentPath) == 0 {
|
2019-12-23 13:31:44 +00:00
|
|
|
screen.currentPath = screen.db.getNextVisiblePath(nil, screen.filter)
|
2019-10-16 22:41:29 +00:00
|
|
|
}
|
2019-10-17 15:49:08 +00:00
|
|
|
for i := range screen.db.buckets {
|
|
|
|
screen.leftPaneBuffer = append(screen.leftPaneBuffer, screen.bucketToLines(&screen.db.buckets[i], style)...)
|
|
|
|
}
|
2019-12-23 13:31:44 +00:00
|
|
|
// Find the cursor in the leftPane
|
|
|
|
for k, v := range screen.leftPaneBuffer {
|
|
|
|
if v.Fg == style.cursorFg {
|
|
|
|
screen.leftViewPort.scrollRow = k
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-10-16 22:41:29 +00:00
|
|
|
}
|
|
|
|
|
2015-05-18 14:47:08 +00:00
|
|
|
func (screen *BrowserScreen) drawLeftPane(style Style) {
|
2019-10-17 15:49:08 +00:00
|
|
|
screen.buildLeftPane(style)
|
2015-05-18 14:47:08 +00:00
|
|
|
w, h := termbox.Size()
|
|
|
|
if w > 80 {
|
|
|
|
w = w / 2
|
|
|
|
}
|
2019-10-17 15:49:08 +00:00
|
|
|
screen.leftViewPort.bytesPerRow = w
|
2019-10-16 22:41:29 +00:00
|
|
|
screen.leftViewPort.numberOfRows = h - 2
|
2015-10-21 16:47:34 +00:00
|
|
|
termboxUtil.FillWithChar('=', 0, 1, w, 1, style.defaultFg, style.defaultBg)
|
2019-12-23 13:31:44 +00:00
|
|
|
startX, startY := 0, 3
|
2019-10-17 15:49:08 +00:00
|
|
|
screen.leftViewPort.firstRow = startY
|
2019-12-23 13:31:44 +00:00
|
|
|
treeOffset := 0
|
|
|
|
maxCursor := screen.leftViewPort.numberOfRows * 2 / 3
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
2019-10-16 22:41:29 +00:00
|
|
|
|
|
|
|
func (screen *BrowserScreen) buildRightPane(style Style) {
|
|
|
|
screen.rightPaneBuffer = nil
|
|
|
|
b, p, err := screen.db.getGenericFromPath(screen.currentPath)
|
|
|
|
if err == nil {
|
|
|
|
if b != nil {
|
|
|
|
screen.rightPaneBuffer = append(screen.rightPaneBuffer,
|
|
|
|
Line{fmt.Sprintf("Path: %s", strings.Join(stringifyPath(b.GetPath()), " → ")), style.defaultFg, style.defaultBg})
|
|
|
|
screen.rightPaneBuffer = append(screen.rightPaneBuffer,
|
|
|
|
Line{fmt.Sprintf("Buckets: %d", len(b.buckets)), style.defaultFg, style.defaultBg})
|
|
|
|
screen.rightPaneBuffer = append(screen.rightPaneBuffer,
|
|
|
|
Line{fmt.Sprintf("Pairs: %d", len(b.pairs)), style.defaultFg, style.defaultBg})
|
|
|
|
} else if p != nil {
|
|
|
|
screen.rightPaneBuffer = append(screen.rightPaneBuffer,
|
|
|
|
Line{fmt.Sprintf("Path: %s", strings.Join(stringifyPath(p.GetPath()), " → ")), style.defaultFg, style.defaultBg})
|
|
|
|
screen.rightPaneBuffer = append(screen.rightPaneBuffer,
|
|
|
|
Line{fmt.Sprintf("Key: %s", stringify([]byte(p.key))), style.defaultFg, style.defaultBg})
|
|
|
|
|
2019-10-17 15:49:08 +00:00
|
|
|
value := strings.Split(string(formatValue([]byte(p.val))), "\n")
|
|
|
|
if len(value) == 1 {
|
|
|
|
screen.rightPaneBuffer = append(screen.rightPaneBuffer,
|
|
|
|
Line{fmt.Sprintf("Value: %s", value[0]), style.defaultFg, style.defaultBg})
|
|
|
|
} else {
|
|
|
|
screen.rightPaneBuffer = append(screen.rightPaneBuffer,
|
|
|
|
Line{"Value:", style.defaultFg, style.defaultBg})
|
|
|
|
for _, v := range value {
|
|
|
|
screen.rightPaneBuffer = append(screen.rightPaneBuffer,
|
|
|
|
Line{v, style.defaultFg, style.defaultBg})
|
|
|
|
}
|
|
|
|
}
|
2019-10-16 22:41:29 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
screen.rightPaneBuffer = append(screen.rightPaneBuffer,
|
|
|
|
Line{fmt.Sprintf("Path: %s", strings.Join(stringifyPath(screen.currentPath), " → ")), style.defaultFg, style.defaultBg})
|
|
|
|
screen.rightPaneBuffer = append(screen.rightPaneBuffer,
|
|
|
|
Line{err.Error(), termbox.ColorRed, termbox.ColorBlack})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-18 14:47:08 +00:00
|
|
|
func (screen *BrowserScreen) drawRightPane(style Style) {
|
2019-10-16 22:41:29 +00:00
|
|
|
screen.buildRightPane(style)
|
2015-05-18 14:47:08 +00:00
|
|
|
w, h := termbox.Size()
|
|
|
|
if w > 80 {
|
2019-10-17 15:49:08 +00:00
|
|
|
screen.rightViewPort.bytesPerRow = w / 2
|
|
|
|
screen.rightViewPort.numberOfRows = h - 2
|
2015-05-18 14:47:08 +00:00
|
|
|
// Screen is wide enough, split it
|
2015-10-21 16:47:34 +00:00
|
|
|
termboxUtil.FillWithChar('=', 0, 1, w, 1, style.defaultFg, style.defaultBg)
|
2019-10-16 22:41:29 +00:00
|
|
|
termboxUtil.FillWithChar('|', (w / 2), screen.rightViewPort.firstRow-1, (w / 2), h, style.defaultFg, style.defaultBg)
|
2015-12-01 21:20:52 +00:00
|
|
|
// Clear the right pane
|
2019-10-17 15:49:08 +00:00
|
|
|
termboxUtil.FillWithChar(' ', (w/2)+1, screen.rightViewPort.firstRow+2, w, h, style.defaultFg, style.defaultBg)
|
2015-05-18 14:47:08 +00:00
|
|
|
|
2017-04-05 16:29:28 +00:00
|
|
|
startX := (w / 2) + 2
|
2019-10-17 15:49:08 +00:00
|
|
|
startY := 3
|
|
|
|
maxScroll := len(screen.rightPaneBuffer) - screen.rightViewPort.numberOfRows
|
|
|
|
if maxScroll < 0 {
|
|
|
|
maxScroll = 0
|
|
|
|
}
|
|
|
|
if screen.rightViewPort.scrollRow > maxScroll {
|
|
|
|
screen.rightViewPort.scrollRow = maxScroll
|
|
|
|
}
|
|
|
|
if len(screen.rightPaneBuffer) > 0 {
|
|
|
|
for k, v := range screen.rightPaneBuffer[screen.rightViewPort.scrollRow:] {
|
|
|
|
termboxUtil.DrawStringAtPoint(v.Text, startX, (startY + k - 1), v.Fg, v.Bg)
|
|
|
|
}
|
|
|
|
}
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-07 12:43:10 +00:00
|
|
|
func formatValue(val []byte) []byte {
|
|
|
|
// Attempt JSON parsing and formatting
|
|
|
|
out, err := formatValueJSON(val)
|
|
|
|
if err == nil {
|
|
|
|
return out
|
|
|
|
}
|
2019-10-16 20:33:16 +00:00
|
|
|
return []byte(stringify([]byte(val)))
|
2019-10-07 12:43:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-10-17 15:49:08 +00:00
|
|
|
func (screen *BrowserScreen) bucketToLines(bkt *BoltBucket, style Style) []Line {
|
|
|
|
var ret []Line
|
|
|
|
bfg, bbg := style.defaultFg, style.defaultBg
|
2015-09-17 16:49:57 +00:00
|
|
|
if comparePaths(screen.currentPath, bkt.GetPath()) {
|
2019-10-17 15:49:08 +00:00
|
|
|
bfg, bbg = style.cursorFg, style.cursorBg
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
2019-10-17 15:49:08 +00:00
|
|
|
bktPrefix := strings.Repeat(" ", len(bkt.GetPath())*2)
|
2015-12-01 21:20:52 +00:00
|
|
|
if bkt.expanded {
|
2019-10-17 15:49:08 +00:00
|
|
|
ret = append(ret, Line{bktPrefix + "- " + stringify([]byte(bkt.name)), bfg, bbg})
|
2015-05-18 14:47:08 +00:00
|
|
|
for i := range bkt.buckets {
|
2019-10-17 15:49:08 +00:00
|
|
|
ret = append(ret, screen.bucketToLines(&bkt.buckets[i], style)...)
|
2016-12-29 18:43:10 +00:00
|
|
|
}
|
2019-10-17 15:49:08 +00:00
|
|
|
for _, bp := range bkt.pairs {
|
2019-12-23 13:31:44 +00:00
|
|
|
if screen.filter != "" && !strings.Contains(bp.key, screen.filter) {
|
|
|
|
continue
|
|
|
|
}
|
2019-10-17 15:49:08 +00:00
|
|
|
pfg, pbg := style.defaultFg, style.defaultBg
|
|
|
|
if comparePaths(screen.currentPath, bp.GetPath()) {
|
|
|
|
pfg, pbg = style.cursorFg, style.cursorBg
|
|
|
|
}
|
|
|
|
prPrefix := strings.Repeat(" ", len(bp.GetPath())*2)
|
2019-12-23 11:26:45 +00:00
|
|
|
var pairString string
|
|
|
|
if AppArgs.NoValue {
|
|
|
|
pairString = fmt.Sprintf("%s%s", prPrefix, stringify([]byte(bp.key)))
|
|
|
|
} else {
|
|
|
|
pairString = fmt.Sprintf("%s%s: %s", prPrefix, stringify([]byte(bp.key)), stringify([]byte(bp.val)))
|
|
|
|
}
|
2019-10-17 15:49:08 +00:00
|
|
|
ret = append(ret, Line{pairString, pfg, pbg})
|
2015-12-01 21:20:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-10-17 15:49:08 +00:00
|
|
|
ret = append(ret, Line{bktPrefix + "+ " + stringify([]byte(bkt.name)), bfg, bbg})
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
2019-10-17 15:49:08 +00:00
|
|
|
return ret
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (screen *BrowserScreen) startDeleteItem() bool {
|
2015-09-17 16:49:57 +00:00
|
|
|
b, p, e := screen.db.getGenericFromPath(screen.currentPath)
|
2015-05-18 14:47:08 +00:00
|
|
|
if e == nil {
|
|
|
|
w, h := termbox.Size()
|
2015-09-17 16:49:57 +00:00
|
|
|
inpW, inpH := (w / 2), 6
|
|
|
|
inpX, inpY := ((w / 2) - (inpW / 2)), ((h / 2) - inpH)
|
2015-10-21 16:47:34 +00:00
|
|
|
mod := termboxUtil.CreateConfirmModal("", inpX, inpY, inpW, inpH, termbox.ColorWhite, termbox.ColorBlack)
|
2015-05-18 14:47:08 +00:00
|
|
|
if b != nil {
|
2015-10-21 16:47:34 +00:00
|
|
|
mod.SetTitle(termboxUtil.AlignText(fmt.Sprintf("Delete Bucket '%s'?", b.name), inpW-1, termboxUtil.AlignCenter))
|
2015-05-18 14:47:08 +00:00
|
|
|
} else if p != nil {
|
2015-10-21 16:47:34 +00:00
|
|
|
mod.SetTitle(termboxUtil.AlignText(fmt.Sprintf("Delete Pair '%s'?", p.key), inpW-1, termboxUtil.AlignCenter))
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
|
|
|
mod.Show()
|
2015-10-21 16:47:34 +00:00
|
|
|
mod.SetText(termboxUtil.AlignText("This cannot be undone!", inpW-1, termboxUtil.AlignCenter))
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.confirmModal = mod
|
|
|
|
screen.mode = modeDelete
|
2015-05-18 14:47:08 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-12-23 13:31:44 +00:00
|
|
|
func (screen *BrowserScreen) startFilter() bool {
|
|
|
|
_, _, e := screen.db.getGenericFromPath(screen.currentPath)
|
|
|
|
if e == nil {
|
|
|
|
w, h := termbox.Size()
|
|
|
|
inpW, inpH := (w / 2), 6
|
|
|
|
inpX, inpY := ((w / 2) - (inpW / 2)), ((h / 2) - inpH)
|
|
|
|
mod := termboxUtil.CreateInputModal("", inpX, inpY, inpW, inpH, termbox.ColorWhite, termbox.ColorBlack)
|
|
|
|
mod.SetTitle(termboxUtil.AlignText("Filter", inpW, termboxUtil.AlignCenter))
|
|
|
|
mod.SetValue(screen.filter)
|
|
|
|
mod.Show()
|
|
|
|
screen.inputModal = mod
|
|
|
|
screen.mode = modeFilter
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-05-18 14:47:08 +00:00
|
|
|
func (screen *BrowserScreen) startEditItem() bool {
|
2015-09-17 16:49:57 +00:00
|
|
|
_, p, e := screen.db.getGenericFromPath(screen.currentPath)
|
2015-05-18 14:47:08 +00:00
|
|
|
if e == nil {
|
|
|
|
w, h := termbox.Size()
|
2015-09-17 16:49:57 +00:00
|
|
|
inpW, inpH := (w / 2), 6
|
|
|
|
inpX, inpY := ((w / 2) - (inpW / 2)), ((h / 2) - inpH)
|
2015-10-21 16:47:34 +00:00
|
|
|
mod := termboxUtil.CreateInputModal("", inpX, inpY, inpW, inpH, termbox.ColorWhite, termbox.ColorBlack)
|
2015-05-18 14:47:08 +00:00
|
|
|
if p != nil {
|
2015-10-21 16:47:34 +00:00
|
|
|
mod.SetTitle(termboxUtil.AlignText(fmt.Sprintf("Input new value for '%s'", p.key), inpW, termboxUtil.AlignCenter))
|
2015-05-18 14:47:08 +00:00
|
|
|
mod.SetValue(p.val)
|
|
|
|
}
|
|
|
|
mod.Show()
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.inputModal = mod
|
|
|
|
screen.mode = modeChangeVal
|
2015-05-18 14:47:08 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (screen *BrowserScreen) startRenameItem() bool {
|
2015-09-17 16:49:57 +00:00
|
|
|
b, p, e := screen.db.getGenericFromPath(screen.currentPath)
|
2015-05-18 14:47:08 +00:00
|
|
|
if e == nil {
|
|
|
|
w, h := termbox.Size()
|
2015-09-17 16:49:57 +00:00
|
|
|
inpW, inpH := (w / 2), 6
|
|
|
|
inpX, inpY := ((w / 2) - (inpW / 2)), ((h / 2) - inpH)
|
2015-10-21 16:47:34 +00:00
|
|
|
mod := termboxUtil.CreateInputModal("", inpX, inpY, inpW, inpH, termbox.ColorWhite, termbox.ColorBlack)
|
2015-05-18 14:47:08 +00:00
|
|
|
if b != nil {
|
2015-10-21 16:47:34 +00:00
|
|
|
mod.SetTitle(termboxUtil.AlignText(fmt.Sprintf("Rename Bucket '%s' to:", b.name), inpW, termboxUtil.AlignCenter))
|
2015-05-18 14:47:08 +00:00
|
|
|
mod.SetValue(b.name)
|
|
|
|
} else if p != nil {
|
2015-10-21 16:47:34 +00:00
|
|
|
mod.SetTitle(termboxUtil.AlignText(fmt.Sprintf("Rename Key '%s' to:", p.key), inpW, termboxUtil.AlignCenter))
|
2015-05-18 14:47:08 +00:00
|
|
|
mod.SetValue(p.key)
|
|
|
|
}
|
|
|
|
mod.Show()
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.inputModal = mod
|
|
|
|
screen.mode = modeChangeKey
|
2015-05-18 14:47:08 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (screen *BrowserScreen) startInsertItemAtParent(tp BoltType) bool {
|
|
|
|
w, h := termbox.Size()
|
2015-09-17 16:49:57 +00:00
|
|
|
inpW, inpH := w-1, 7
|
2015-05-18 14:47:08 +00:00
|
|
|
if w > 80 {
|
2015-09-17 16:49:57 +00:00
|
|
|
inpW, inpH = (w / 2), 7
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
2015-09-17 16:49:57 +00:00
|
|
|
inpX, inpY := ((w / 2) - (inpW / 2)), ((h / 2) - inpH)
|
2015-10-21 16:47:34 +00:00
|
|
|
mod := termboxUtil.CreateInputModal("", inpX, inpY, inpW, inpH, termbox.ColorWhite, termbox.ColorBlack)
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.inputModal = mod
|
|
|
|
if len(screen.currentPath) <= 0 {
|
2015-05-18 14:47:08 +00:00
|
|
|
// in the root directory
|
2015-09-17 16:49:57 +00:00
|
|
|
if tp == typeBucket {
|
2015-10-21 16:47:34 +00:00
|
|
|
mod.SetTitle(termboxUtil.AlignText("Create Root Bucket", inpW, termboxUtil.AlignCenter))
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.mode = modeInsertBucket | modeModToParent
|
2015-05-18 14:47:08 +00:00
|
|
|
mod.Show()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
} else {
|
2015-09-17 16:49:57 +00:00
|
|
|
var insPath string
|
|
|
|
_, p, e := screen.db.getGenericFromPath(screen.currentPath[:len(screen.currentPath)-1])
|
2015-05-18 14:47:08 +00:00
|
|
|
if e == nil && p != nil {
|
2017-04-05 17:49:05 +00:00
|
|
|
insPath = strings.Join(screen.currentPath[:len(screen.currentPath)-2], " → ") + " → "
|
2015-05-18 14:47:08 +00:00
|
|
|
} else {
|
2017-04-05 17:49:05 +00:00
|
|
|
insPath = strings.Join(screen.currentPath[:len(screen.currentPath)-1], " → ") + " → "
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
2015-09-17 16:49:57 +00:00
|
|
|
titlePrfx := ""
|
|
|
|
if tp == typeBucket {
|
|
|
|
titlePrfx = "New Bucket: "
|
|
|
|
} else if tp == typePair {
|
|
|
|
titlePrfx = "New Pair: "
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
2015-09-17 16:49:57 +00:00
|
|
|
titleText := titlePrfx + insPath
|
|
|
|
if len(titleText) > inpW {
|
|
|
|
truncW := len(titleText) - inpW
|
|
|
|
titleText = titlePrfx + "..." + insPath[truncW+3:]
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
2015-09-17 16:49:57 +00:00
|
|
|
if tp == typeBucket {
|
2015-10-21 16:47:34 +00:00
|
|
|
mod.SetTitle(termboxUtil.AlignText(titleText, inpW, termboxUtil.AlignCenter))
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.mode = modeInsertBucket | modeModToParent
|
2015-05-18 14:47:08 +00:00
|
|
|
mod.Show()
|
|
|
|
return true
|
2015-09-17 16:49:57 +00:00
|
|
|
} else if tp == typePair {
|
2015-10-21 16:47:34 +00:00
|
|
|
mod.SetTitle(termboxUtil.AlignText(titleText, inpW, termboxUtil.AlignCenter))
|
2015-05-18 14:47:08 +00:00
|
|
|
mod.Show()
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.mode = modeInsertPair | modeModToParent
|
2015-05-18 14:47:08 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (screen *BrowserScreen) startInsertItem(tp BoltType) bool {
|
|
|
|
w, h := termbox.Size()
|
2015-09-17 16:49:57 +00:00
|
|
|
inpW, inpH := w-1, 7
|
2015-05-18 14:47:08 +00:00
|
|
|
if w > 80 {
|
2015-09-17 16:49:57 +00:00
|
|
|
inpW, inpH = (w / 2), 7
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
2015-09-17 16:49:57 +00:00
|
|
|
inpX, inpY := ((w / 2) - (inpW / 2)), ((h / 2) - inpH)
|
2015-10-21 16:47:34 +00:00
|
|
|
mod := termboxUtil.CreateInputModal("", inpX, inpY, inpW, inpH, termbox.ColorWhite, termbox.ColorBlack)
|
2015-12-01 21:20:52 +00:00
|
|
|
//mod.SetInputWrap(true)
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.inputModal = mod
|
|
|
|
var insPath string
|
|
|
|
_, p, e := screen.db.getGenericFromPath(screen.currentPath)
|
2015-05-18 14:47:08 +00:00
|
|
|
if e == nil && p != nil {
|
2017-04-05 17:49:05 +00:00
|
|
|
insPath = strings.Join(screen.currentPath[:len(screen.currentPath)-1], " → ") + " → "
|
2015-05-18 14:47:08 +00:00
|
|
|
} else {
|
2017-04-05 17:49:05 +00:00
|
|
|
insPath = strings.Join(screen.currentPath, " → ") + " → "
|
2015-09-17 16:49:57 +00:00
|
|
|
}
|
|
|
|
titlePrfx := ""
|
|
|
|
if tp == typeBucket {
|
|
|
|
titlePrfx = "New Bucket: "
|
|
|
|
} else if tp == typePair {
|
|
|
|
titlePrfx = "New Pair: "
|
|
|
|
}
|
|
|
|
titleText := titlePrfx + insPath
|
|
|
|
if len(titleText) > inpW {
|
|
|
|
truncW := len(titleText) - inpW
|
|
|
|
titleText = titlePrfx + "..." + insPath[truncW+3:]
|
|
|
|
}
|
|
|
|
if tp == typeBucket {
|
2015-10-21 16:47:34 +00:00
|
|
|
mod.SetTitle(termboxUtil.AlignText(titleText, inpW, termboxUtil.AlignCenter))
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.mode = modeInsertBucket
|
2015-05-18 14:47:08 +00:00
|
|
|
mod.Show()
|
|
|
|
return true
|
2015-09-17 16:49:57 +00:00
|
|
|
} else if tp == typePair {
|
2015-10-21 16:47:34 +00:00
|
|
|
mod.SetTitle(termboxUtil.AlignText(titleText, inpW, termboxUtil.AlignCenter))
|
2015-05-18 14:47:08 +00:00
|
|
|
mod.Show()
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.mode = modeInsertPair
|
2015-05-18 14:47:08 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-12-29 21:23:58 +00:00
|
|
|
func (screen *BrowserScreen) startExportValue() bool {
|
|
|
|
_, p, e := screen.db.getGenericFromPath(screen.currentPath)
|
|
|
|
if e == nil && p != nil {
|
|
|
|
w, h := termbox.Size()
|
|
|
|
inpW, inpH := (w / 2), 6
|
|
|
|
inpX, inpY := ((w / 2) - (inpW / 2)), ((h / 2) - inpH)
|
|
|
|
mod := termboxUtil.CreateInputModal("", inpX, inpY, inpW, inpH, termbox.ColorWhite, termbox.ColorBlack)
|
|
|
|
mod.SetTitle(termboxUtil.AlignText(fmt.Sprintf("Export value of '%s' to:", p.key), inpW, termboxUtil.AlignCenter))
|
|
|
|
mod.SetValue("")
|
|
|
|
mod.Show()
|
|
|
|
screen.inputModal = mod
|
2023-05-12 13:27:33 +00:00
|
|
|
screen.mode = modeIOExportValue
|
2016-12-29 21:23:58 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
screen.setMessage("Couldn't do string export on " + screen.currentPath[len(screen.currentPath)-1] + "(did you mean 'X'?)")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (screen *BrowserScreen) startExportJSON() bool {
|
|
|
|
b, p, e := screen.db.getGenericFromPath(screen.currentPath)
|
|
|
|
if e == nil {
|
|
|
|
w, h := termbox.Size()
|
|
|
|
inpW, inpH := (w / 2), 6
|
|
|
|
inpX, inpY := ((w / 2) - (inpW / 2)), ((h / 2) - inpH)
|
|
|
|
mod := termboxUtil.CreateInputModal("", inpX, inpY, inpW, inpH, termbox.ColorWhite, termbox.ColorBlack)
|
|
|
|
if b != nil {
|
|
|
|
mod.SetTitle(termboxUtil.AlignText(fmt.Sprintf("Export JSON of '%s' to:", b.name), inpW, termboxUtil.AlignCenter))
|
|
|
|
mod.SetValue("")
|
|
|
|
} else if p != nil {
|
|
|
|
mod.SetTitle(termboxUtil.AlignText(fmt.Sprintf("Export JSON of '%s' to:", p.key), inpW, termboxUtil.AlignCenter))
|
|
|
|
mod.SetValue("")
|
|
|
|
}
|
|
|
|
mod.Show()
|
|
|
|
screen.inputModal = mod
|
2023-05-12 13:27:33 +00:00
|
|
|
screen.mode = modeIOExportJSON
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (screen *BrowserScreen) startImportValue() bool {
|
|
|
|
_, p, e := screen.db.getGenericFromPath(screen.currentPath)
|
|
|
|
if e == nil && p != nil {
|
|
|
|
w, h := termbox.Size()
|
|
|
|
inpW, inpH := (w / 2), 6
|
|
|
|
inpX, inpY := ((w / 2) - (inpW / 2)), ((h / 2) - inpH)
|
|
|
|
mod := termboxUtil.CreateInputModal("", inpX, inpY, inpW, inpH, termbox.ColorWhite, termbox.ColorBlack)
|
|
|
|
mod.SetTitle(termboxUtil.AlignText(fmt.Sprintf("Import value of '%s' from:", p.key), inpW, termboxUtil.AlignCenter))
|
|
|
|
mod.SetValue("")
|
|
|
|
mod.Show()
|
|
|
|
screen.inputModal = mod
|
|
|
|
screen.mode = modeIOImportValue
|
2016-12-29 21:23:58 +00:00
|
|
|
return true
|
|
|
|
}
|
2023-05-12 13:27:33 +00:00
|
|
|
screen.setMessage("Couldn't do import on " + screen.currentPath[len(screen.currentPath)-1] + ", must be a pair.")
|
2016-12-29 21:23:58 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-05-18 14:47:08 +00:00
|
|
|
func (screen *BrowserScreen) setMessage(msg string) {
|
|
|
|
screen.message = msg
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.messageTime = time.Now()
|
|
|
|
screen.messageTimeout = time.Second * 2
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* setMessageWithTimeout lets you specify the timeout for the message
|
|
|
|
* setting it to -1 means it won't timeout
|
|
|
|
*/
|
|
|
|
func (screen *BrowserScreen) setMessageWithTimeout(msg string, timeout time.Duration) {
|
|
|
|
screen.message = msg
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.messageTime = time.Now()
|
|
|
|
screen.messageTimeout = timeout
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (screen *BrowserScreen) clearMessage() {
|
|
|
|
screen.message = ""
|
2015-09-17 16:49:57 +00:00
|
|
|
screen.messageTimeout = -1
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (screen *BrowserScreen) refreshDatabase() {
|
|
|
|
shadowDB := screen.db
|
|
|
|
screen.db = screen.db.refreshDatabase()
|
|
|
|
screen.db.syncOpenBuckets(shadowDB)
|
|
|
|
}
|
|
|
|
|
|
|
|
func comparePaths(p1, p2 []string) bool {
|
2017-04-05 17:49:05 +00:00
|
|
|
return strings.Join(p1, " → ") == strings.Join(p2, " → ")
|
2015-05-18 14:47:08 +00:00
|
|
|
}
|