From 38817ae4d36405f11f089821f193ae278f215b3d Mon Sep 17 00:00:00 2001 From: KemoNine Date: Sat, 29 Aug 2020 19:12:28 -0400 Subject: [PATCH] Start reworking keyboard input and no longer trap signals for controls --- cmd/ui/ui.go | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/cmd/ui/ui.go b/cmd/ui/ui.go index f4b459d..c340748 100644 --- a/cmd/ui/ui.go +++ b/cmd/ui/ui.go @@ -1,14 +1,14 @@ package main import ( + "fmt" "io" "log" - "os" "os/exec" - "os/signal" - "syscall" "time" + "github.com/eiannone/keyboard" + "github.com/gdamore/tcell" "github.com/rivo/tview" ) @@ -19,21 +19,33 @@ const ( ) func main() { - // fim placeholder so we can operate on it when a signal is received + // fim placeholder so we can operate on it when a exit slideshow is received var fim *exec.Cmd = nil - // Setup signal listening - sigs := make(chan os.Signal) - signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) - defer func() { close(sigs) }() + // Start watching for key strokes and echo them back to stdout + keysEvents, err := keyboard.GetKeys(10) + if err != nil { + panic(err) + } + defer func() { + _ = keyboard.Close() + }() - // Goroutine to handle os signals (nuke fim so we can get to config ui) + fmt.Println("Press ESC to kill slideshow") go func() { - for range sigs { - if fim != nil { // Just in case someone lays on ctrl-c or similar during startup - if err := fim.Process.Kill(); err != nil { - log.Fatalf("failed to kill fim : %s", err) + for { + event := <-keysEvents + if event.Err != nil { + panic(event.Err) + } + fmt.Printf("You pressed: rune %q, key %X\r\n", event.Rune, event.Key) + if event.Key == keyboard.KeyEsc { + if fim != nil { // Just in case someone lays on exit key or similar during startup + if err := fim.Process.Kill(); err != nil { + log.Fatalf("failed to kill fim : %s", err) + } } + break } } }()