1
0
mirror of https://github.com/br0xen/termbox-util.git synced 2024-11-22 21:43:14 +00:00
termbox-util/termbox-test/main.go

120 lines
2.6 KiB
Go
Raw Normal View History

2015-05-02 03:15:36 +00:00
package main
import (
2015-05-03 22:11:50 +00:00
"fmt"
2015-05-02 03:15:36 +00:00
"github.com/nsf/termbox-go"
"gogs.bullercodeworks.com/brian/termbox-util"
"os"
"syscall"
)
var keep_running bool
func main() {
keep_running = true
err := termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
termbox.SetOutputMode(termbox.Output256)
mainLoop()
}
2015-05-03 22:11:50 +00:00
//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
2015-05-02 03:15:36 +00:00
func layoutAndDrawScreen() {
termbox.Clear(0, termbox.ColorBlack)
drawScreen()
termbox.Flush()
}
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 {
2015-05-03 22:11:50 +00:00
mw, mh := w/4, h/4
2015-05-02 03:15:36 +00:00
mx, my := (w/2)-(mw/2), (h/2)-(mh/2)
2015-05-03 22:11:50 +00:00
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)
}
2015-05-02 03:15:36 +00:00
}
}
func handleKeyEvent(event termbox.Event) bool {
if event.Key == termbox.KeyEsc {
return false
2015-05-03 22:11:50 +00:00
} 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")
2015-05-02 03:15:36 +00:00
} else {
input.HandleKeyPress(event)
}
return true
}
func mainLoop() {
2015-05-03 22:11:50 +00:00
added_stuff = append(added_stuff, "Ctrl+B = Add Bucket; Ctrl+P = Add Pair")
2015-05-02 03:15:36 +00:00
layoutAndDrawScreen()
for {
event := termbox.PollEvent()
if event.Type == termbox.EventKey {
if event.Key == termbox.KeyCtrlZ {
process, _ := os.FindProcess(os.Getpid())
termbox.Close()
process.Signal(syscall.SIGSTOP)
termbox.Init()
}
keep_running = handleKeyEvent(event)
if !keep_running {
break
}
layoutAndDrawScreen()
}
if event.Type == termbox.EventResize {
layoutAndDrawScreen()
}
}
}