Building things out, looking good.
This commit is contained in:
parent
868945cff9
commit
c683fc190c
BIN
boltbrowser
BIN
boltbrowser
Binary file not shown.
@ -9,9 +9,15 @@ import (
|
||||
)
|
||||
|
||||
type BoltBucket struct {
|
||||
name string
|
||||
pairs map[string]string
|
||||
buckets []BoltBucket
|
||||
name string
|
||||
pairs []BoltPair
|
||||
buckets []BoltBucket
|
||||
expanded bool
|
||||
}
|
||||
|
||||
type BoltPair struct {
|
||||
key string
|
||||
val string
|
||||
}
|
||||
|
||||
// A Database, basically a collection of buckets
|
||||
@ -87,26 +93,28 @@ func refreshDatabase() {
|
||||
memBolt = new(BoltDB)
|
||||
db.View(func(tx *bolt.Tx) error {
|
||||
tx.ForEach(func(nm []byte, b *bolt.Bucket) error {
|
||||
bb := readBucket(b)
|
||||
bb := readBucket(*b)
|
||||
bb.name = string(nm)
|
||||
memBolt.buckets = append(memBolt.buckets, *bb)
|
||||
bb.expanded = true
|
||||
memBolt.buckets = append(memBolt.buckets, bb)
|
||||
return nil
|
||||
})
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func readBucket(b *bolt.Bucket) *BoltBucket {
|
||||
bb := new(BoltBucket)
|
||||
func readBucket(b bolt.Bucket) BoltBucket {
|
||||
var bb *BoltBucket
|
||||
b.ForEach(func(k, v []byte) error {
|
||||
if v == nil {
|
||||
tb := readBucket(b.Bucket(k))
|
||||
tb := readBucket(*b.Bucket(k))
|
||||
tb.name = string(k)
|
||||
bb.buckets = append(bb.buckets, *tb)
|
||||
bb.buckets = append(bb.buckets, tb)
|
||||
} else {
|
||||
bb.pairs[string(k)] = string(v)
|
||||
tp := BoltPair{string(k), string(v)}
|
||||
bb.pairs = append(bb.pairs, tp)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return bb
|
||||
return *bb
|
||||
}
|
@ -18,7 +18,7 @@ func defaultScreensForData(memBolt *BoltDB) []Screen {
|
||||
var view_port ViewPort
|
||||
var cursor Cursor
|
||||
|
||||
browser_screen := BrowserScreen{*memBolt, cursor, view_port}
|
||||
browser_screen := BrowserScreen{*memBolt, cursor, view_port, ""}
|
||||
about_screen := AboutScreen(0)
|
||||
screens := [...]Screen{
|
||||
&browser_screen,
|
||||
|
@ -54,7 +54,7 @@ func (screen *AboutScreen) drawScreen(style Style) {
|
||||
bg := default_bg
|
||||
displayRune := ' '
|
||||
if runeValue != ' ' {
|
||||
bg = termbox.Attribute(125)
|
||||
//bg = termbox.Attribute(125)
|
||||
displayRune = runeValue
|
||||
termbox.SetCell(x_pos, y_pos, displayRune, default_fg, bg)
|
||||
}
|
||||
|
@ -1,7 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/nsf/termbox-go"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ViewPort struct {
|
||||
@ -11,9 +13,10 @@ type ViewPort struct {
|
||||
}
|
||||
|
||||
type BrowserScreen struct {
|
||||
memBolt BoltDB
|
||||
cursor Cursor
|
||||
view_port ViewPort
|
||||
memBolt BoltDB
|
||||
cursor Cursor
|
||||
view_port ViewPort
|
||||
queued_command string
|
||||
}
|
||||
|
||||
func (screen *BrowserScreen) handleKeyEvent(event termbox.Event) int {
|
||||
@ -21,52 +24,88 @@ func (screen *BrowserScreen) handleKeyEvent(event termbox.Event) int {
|
||||
return ABOUT_SCREEN_INDEX
|
||||
} else if event.Ch == 'q' || event.Key == termbox.KeyEsc || event.Key == termbox.KeyCtrlC {
|
||||
return EXIT_SCREEN_INDEX
|
||||
} else if event.Ch == 'j' {
|
||||
// Move the cursor down
|
||||
if screen.cursor.y < screen.view_port.number_of_rows-1 {
|
||||
screen.cursor.y++
|
||||
}
|
||||
} else if event.Ch == 'k' {
|
||||
// Move the cursor up
|
||||
if screen.cursor.y > screen.view_port.first_row {
|
||||
screen.cursor.y--
|
||||
}
|
||||
} else if event.Ch == 'e' {
|
||||
screen.queued_command = "edit"
|
||||
} else if event.Key == termbox.KeyEnter {
|
||||
// Select the current item
|
||||
screen.queued_command = "select"
|
||||
}
|
||||
|
||||
return BROWSER_SCREEN_INDEX
|
||||
}
|
||||
|
||||
func (screen *BrowserScreen) performLayout() {
|
||||
/*
|
||||
width, height := termbox.Size()
|
||||
legend_height := heightOfWidgets()
|
||||
line_height := 3
|
||||
cursor := screen.cursor
|
||||
cursor_row_within_view_port := 0
|
||||
|
||||
var new_view_port ViewPort
|
||||
new_view_port.bytes_per_row = (width - 3) / 3
|
||||
new_view_port.number_of_rows = (height - 1 - legend_height) / line_height
|
||||
new_view_port.first_row = screen.view_port.first_row
|
||||
|
||||
if new_view_port.first_row < 0 {
|
||||
new_view_port.first_row = 0
|
||||
}
|
||||
|
||||
screen.view_port = new_view_port
|
||||
*/
|
||||
}
|
||||
func (screen *BrowserScreen) performLayout() {}
|
||||
|
||||
func (screen *BrowserScreen) drawScreen(style Style) {
|
||||
x, y := 2, 1
|
||||
// x_pad := 2
|
||||
//line_height := 1
|
||||
// width, height := termbox.Size()
|
||||
// widget_width, widget_height := drawWidgets(screen.cursor, style)
|
||||
width, _ := termbox.Size()
|
||||
spaces := strings.Repeat(" ", (width/2)-(len(PROGRAM_NAME)-2))
|
||||
drawStringAtPoint(fmt.Sprintf("%s%s%s", spaces, PROGRAM_NAME, spaces), 0, 0, style.title_fg, style.title_bg)
|
||||
|
||||
// cursor := screen.cursor
|
||||
//view_port := screen.view_port
|
||||
|
||||
//last_y := y + view_port.number_of_rows*line_height - 1
|
||||
//last_x := x + view_port.bytes_per_row*3 - 1
|
||||
|
||||
start_x := x
|
||||
for _, bkt := range screen.memBolt.buckets {
|
||||
bucket_fg := style.default_fg
|
||||
bucket_bg := style.default_bg
|
||||
termbox.SetCell(x, y, '+', bucket_fg, bucket_bg)
|
||||
x = drawStringAtPoint(bkt.name, x+1, y, bucket_fg, bucket_bg)
|
||||
y = y + 1
|
||||
x = start_x
|
||||
x, y := 2, 2
|
||||
screen.view_port.first_row = y
|
||||
if screen.cursor.y == 0 {
|
||||
screen.cursor.y = y
|
||||
}
|
||||
for _, bkt := range screen.memBolt.buckets {
|
||||
_, y = screen.drawBucket(&bkt, style, x, y)
|
||||
}
|
||||
screen.view_port.number_of_rows = y
|
||||
}
|
||||
|
||||
func (screen *BrowserScreen) drawBucket(b *BoltBucket, style Style, x, y int) (int, int) {
|
||||
bkt := b
|
||||
bucket_fg := style.default_fg
|
||||
bucket_bg := style.default_bg
|
||||
if y == screen.cursor.y {
|
||||
bucket_fg = style.cursor_fg
|
||||
bucket_bg = style.cursor_bg
|
||||
if screen.queued_command == "select" {
|
||||
// Expand/Collapse the bucket
|
||||
bkt.expanded = !bkt.expanded
|
||||
screen.queued_command = ""
|
||||
}
|
||||
}
|
||||
bkt_string := " "
|
||||
start_x := x
|
||||
if bkt.expanded {
|
||||
bkt_string = bkt_string + "- " + bkt.name + " "
|
||||
x = drawStringAtPoint(bkt_string, x, y, bucket_fg, bucket_bg)
|
||||
y = y + 1
|
||||
|
||||
for _, ib := range bkt.buckets {
|
||||
_, y = screen.drawBucket(&ib, style, start_x+2, y)
|
||||
}
|
||||
for _, ip := range bkt.pairs {
|
||||
_, y = screen.drawPair(ip, style, x, y)
|
||||
}
|
||||
} else {
|
||||
bkt_string = bkt_string + "+ " + bkt.name + " "
|
||||
x = drawStringAtPoint(bkt_string, x, y, bucket_fg, bucket_bg)
|
||||
y = y + 1
|
||||
}
|
||||
|
||||
return x, y
|
||||
}
|
||||
|
||||
func (screen *BrowserScreen) drawPair(bp BoltPair, style Style, x, y int) (int, int) {
|
||||
bucket_fg := style.default_fg
|
||||
bucket_bg := style.default_bg
|
||||
if y == screen.cursor.y {
|
||||
bucket_fg = style.cursor_fg
|
||||
bucket_bg = style.cursor_bg
|
||||
}
|
||||
|
||||
pair_string := fmt.Sprintf("%s: %s", bp.key, bp.val)
|
||||
x = drawStringAtPoint(pair_string, x, y, bucket_fg, bucket_bg)
|
||||
y = y + 1
|
||||
return x, y
|
||||
}
|
||||
|
12
style.go
12
style.go
@ -5,12 +5,20 @@ import "github.com/nsf/termbox-go"
|
||||
type Style struct {
|
||||
default_bg termbox.Attribute
|
||||
default_fg termbox.Attribute
|
||||
title_fg termbox.Attribute
|
||||
title_bg termbox.Attribute
|
||||
cursor_fg termbox.Attribute
|
||||
cursor_bg termbox.Attribute
|
||||
}
|
||||
|
||||
func defaultStyle() Style {
|
||||
var style Style
|
||||
style.default_bg = termbox.Attribute(1)
|
||||
style.default_fg = termbox.Attribute(256)
|
||||
style.default_bg = termbox.ColorBlack
|
||||
style.default_fg = termbox.ColorWhite
|
||||
style.title_fg = termbox.ColorBlack
|
||||
style.title_bg = termbox.ColorRed
|
||||
style.cursor_fg = termbox.ColorBlack
|
||||
style.cursor_bg = termbox.ColorRed
|
||||
|
||||
return style
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user