diff --git a/main.go b/main.go index 6775c79..323611f 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,11 @@ package main import "log" -import blt "bearlibterminal" +import ( + blt "bearlibterminal" + "ui" + "util" +) func main() { log.Println("Starting...") @@ -9,7 +13,11 @@ func main() { defer blt.Close() blt.Print(1, 1, "Hello, world!") blt.Refresh() - for blt.Read() != blt.TK_CLOSE { - + var exit = false + for !exit { + exit, _ = util.InArray(blt.Read(), []int{blt.TK_CLOSE, blt.TK_ESCAPE}) + blt.Print( 1, 3, "Key: " + ui.ReadKey() ) + blt.Refresh() } + log.Println("Closing...") } \ No newline at end of file diff --git a/src/ui/ui.go b/src/ui/ui.go new file mode 100644 index 0000000..78a9f5f --- /dev/null +++ b/src/ui/ui.go @@ -0,0 +1,21 @@ +package ui + +import blt "bearlibterminal" +import "util" + +func ReadKey() (string) { + var key = blt.Read() + if !blt.HasInput() { + return "" + } + var modifiers = []int{blt.TK_SHIFT, blt.TK_ALT, blt.TK_CONTROL} + modifierPressed, _ := util.InArray(key, modifiers) + var pressed = "" + if (modifierPressed) { + pressed ="modifier" + } else { + pressed = string(key) + } + + return pressed +} diff --git a/src/util/util.go b/src/util/util.go new file mode 100644 index 0000000..4715af2 --- /dev/null +++ b/src/util/util.go @@ -0,0 +1,24 @@ +package util + +import ( + "reflect" +) + +func InArray(val interface{}, array interface{}) (exists bool, index int) { + exists = false + index = -1 + + switch reflect.TypeOf(array).Kind() { + case reflect.Slice: + s := reflect.ValueOf(array) + + for i := 0; i < s.Len(); i++ { + if reflect.DeepEqual(val, s.Index(i).Interface()) == true { + index = i + exists = true + return + } + } + } + return +} \ No newline at end of file