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

119 lines
2.7 KiB
Go
Raw Normal View History

2015-05-02 03:15:36 +00:00
package main
import (
"os"
2016-02-09 16:21:57 +00:00
"strconv"
2015-05-02 03:15:36 +00:00
"syscall"
2016-02-09 16:21:57 +00:00
2019-03-25 15:10:25 +00:00
"github.com/br0xen/termbox-util"
2016-02-09 16:21:57 +00:00
"github.com/nsf/termbox-go"
2015-05-02 03:15:36 +00:00
)
2016-02-09 16:21:57 +00:00
var keepRunning bool
var initialized bool
var frame *termboxUtil.Frame
2015-05-02 03:15:36 +00:00
func main() {
2016-02-09 16:21:57 +00:00
keepRunning = true
2015-05-02 03:15:36 +00:00
err := termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
termbox.SetOutputMode(termbox.Output256)
mainLoop()
}
func mainLoop() {
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()
2016-02-09 16:21:57 +00:00
} else if event.Key == termbox.KeyCtrlC {
termbox.Close()
os.Exit(0)
2015-05-02 03:15:36 +00:00
}
2016-02-09 16:21:57 +00:00
keepRunning = handleEvent(event)
if !keepRunning {
2015-05-02 03:15:36 +00:00
break
}
layoutAndDrawScreen()
}
if event.Type == termbox.EventResize {
layoutAndDrawScreen()
}
}
2016-02-09 16:21:57 +00:00
}
func layoutAndDrawScreen() {
w, h := termbox.Size()
if !initialized {
fg, bg := termbox.ColorWhite, termbox.ColorBlack
frame = termboxUtil.CreateFrame(1, 1, w-3, h-3, termbox.ColorWhite, termbox.ColorBlack)
/*
frame.AddControl(termboxUtil.CreateASCIIArt([]string{
"/" + strings.Repeat("=====", 5) + "\\",
"|" + strings.Repeat(".oOo.", 5) + "|",
"\\" + strings.Repeat("=====", 5) + "/",
}, 1, frame.GetBottomY()+1, fg, bg))
frame.AddControl(termboxUtil.CreateAlertModal("AlertModal", 1, 1, w-5, 6, termbox.ColorGreen, bg))
frame.AddControl(termboxUtil.CreateConfirmModal("ConfirmModal", 1, frame.GetBottomY()+1, w-5, 7, fg, bg))
frame.AddControl(termboxUtil.CreateInputModal("InputModal", 1, frame.GetBottomY()+1, w-5, 7, fg, bg))
*/
frame.AddControl(termboxUtil.CreateDropMenu("Add Control", []string{
"AlertModal",
"ASCIIArt",
"ConfirmModal",
"DropMenu",
"Frame",
"InputField",
"InputModal",
"Label",
"Menu",
"ProgressBar",
"ScrollFrame",
},
1, frame.GetBottomY()+1, w-5, 7, fg, bg, termbox.ColorBlack, termbox.ColorGreen))
frame.GetLastControl().SetBordered(true)
frame.SetActiveFlag(true)
initialized = true
}
termbox.Clear(0, termbox.ColorBlack)
drawScreen()
termboxUtil.DrawStringAtPoint(strconv.Itoa(frame.GetBottomY()), 0, h-1, termbox.ColorWhite, termbox.ColorBlack)
termbox.Flush()
}
func drawScreen() {
frame.Draw()
}
func handleEvent(event termbox.Event) bool {
frame.HandleEvent(event)
for _, k := range frame.GetControls() {
switch v := k.(type) {
case *termboxUtil.DropMenu:
if v.IsDone() {
}
case *termboxUtil.AlertModal:
if v.IsDone() {
v.SetText("Finished")
}
case *termboxUtil.ConfirmModal:
if v.IsDone() {
v.SetText("Finished")
}
}
k.SetFgColor(termbox.ColorWhite)
}
frame.GetActiveControl().SetFgColor(termbox.ColorGreen)
return true
2019-03-25 15:10:25 +00:00
}