Start reworking keyboard input and no longer trap signals for controls

This commit is contained in:
KemoNine 2020-08-29 19:12:28 -04:00
parent 8b0c907c3d
commit 38817ae4d3
1 changed files with 25 additions and 13 deletions

View File

@ -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
}
}
}()