mirror of
https://github.com/br0xen/termbox-util.git
synced 2025-08-18 11:44:06 -05:00
Input Modal/Field Works
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/nsf/termbox-go"
|
||||
"gogs.bullercodeworks.com/brian/termbox-util"
|
||||
"os"
|
||||
@@ -20,9 +21,12 @@ func main() {
|
||||
mainLoop()
|
||||
}
|
||||
|
||||
var input *termbox_util.InputField
|
||||
|
||||
//var input *termbox_util.InputModal
|
||||
//var input *termbox_util.InputField
|
||||
var input *termbox_util.InputModal
|
||||
var new_key, new_val string
|
||||
var mode string
|
||||
var drawY int
|
||||
var added_stuff []string
|
||||
|
||||
func layoutAndDrawScreen() {
|
||||
termbox.Clear(0, termbox.ColorBlack)
|
||||
@@ -34,20 +38,57 @@ func drawScreen() {
|
||||
w, h := termbox.Size()
|
||||
termbox_util.DrawStringAtPoint(termbox_util.AlignText("Termbox Utility Test", w, termbox_util.ALIGN_CENTER), 0, 0, termbox.ColorWhite, termbox.ColorRed)
|
||||
if input == nil {
|
||||
// mw, mh := w/4, h/4
|
||||
// mx, my := w-(mw/2), h-(mh/2)
|
||||
mw, mh := w/4, 2
|
||||
mw, mh := w/4, h/4
|
||||
mx, my := (w/2)-(mw/2), (h/2)-(mh/2)
|
||||
// input = termbox_util.CreateInputModal("Test Input", mx, my, mw, mh, termbox.ColorWhite, termbox.ColorBlack)
|
||||
input = termbox_util.CreateInputField(mx, my, mw, mh, termbox.ColorWhite, termbox.ColorBlack)
|
||||
input.SetBordered(true)
|
||||
input = termbox_util.CreateInputModal("", mx, my, mw, mh, termbox.ColorWhite, termbox.ColorBlack)
|
||||
input.Clear()
|
||||
}
|
||||
if mode == "bucket" {
|
||||
if input.IsDone() {
|
||||
added_stuff = append(added_stuff, fmt.Sprintf("New Bucket %s", input.GetValue()))
|
||||
input.Clear()
|
||||
mode = ""
|
||||
} else {
|
||||
input.Draw()
|
||||
}
|
||||
} else if mode == "pair" {
|
||||
if input.IsDone() {
|
||||
if new_key == "" {
|
||||
new_key = input.GetValue()
|
||||
input.Clear()
|
||||
input.SetTitle("Pair Value")
|
||||
} else {
|
||||
added_stuff = append(added_stuff, fmt.Sprintf("New Pair %s => %s", new_key, input.GetValue()))
|
||||
mode = ""
|
||||
input.Clear()
|
||||
}
|
||||
}
|
||||
if mode == "pair" && !input.IsDone() {
|
||||
input.Draw()
|
||||
}
|
||||
}
|
||||
if mode == "" {
|
||||
for i := range added_stuff {
|
||||
termbox_util.DrawStringAtPoint(added_stuff[i], 1, 3+i, termbox.ColorWhite, termbox.ColorRed)
|
||||
}
|
||||
}
|
||||
input.Draw()
|
||||
}
|
||||
|
||||
func handleKeyEvent(event termbox.Event) bool {
|
||||
if event.Key == termbox.KeyEsc {
|
||||
return false
|
||||
} else if event.Key == termbox.KeyCtrlB {
|
||||
mode = "bucket"
|
||||
new_key = ""
|
||||
new_val = ""
|
||||
input.Clear()
|
||||
input.SetTitle("Bucket Name")
|
||||
} else if event.Key == termbox.KeyCtrlP {
|
||||
mode = "pair"
|
||||
new_key = ""
|
||||
new_val = ""
|
||||
input.Clear()
|
||||
input.SetTitle("Pair Key")
|
||||
} else {
|
||||
input.HandleKeyPress(event)
|
||||
}
|
||||
@@ -55,6 +96,7 @@ func handleKeyEvent(event termbox.Event) bool {
|
||||
}
|
||||
|
||||
func mainLoop() {
|
||||
added_stuff = append(added_stuff, "Ctrl+B = Add Bucket; Ctrl+P = Add Pair")
|
||||
layoutAndDrawScreen()
|
||||
for {
|
||||
event := termbox.PollEvent()
|
||||
|
Reference in New Issue
Block a user