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

View file

@ -1,14 +1,14 @@
package main package main
import ( import (
"fmt"
"io" "io"
"log" "log"
"os"
"os/exec" "os/exec"
"os/signal"
"syscall"
"time" "time"
"github.com/eiannone/keyboard"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
"github.com/rivo/tview" "github.com/rivo/tview"
) )
@ -19,21 +19,33 @@ const (
) )
func main() { 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 var fim *exec.Cmd = nil
// Setup signal listening // Start watching for key strokes and echo them back to stdout
sigs := make(chan os.Signal) keysEvents, err := keyboard.GetKeys(10)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) if err != nil {
defer func() { close(sigs) }() 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() { go func() {
for range sigs { for {
if fim != nil { // Just in case someone lays on ctrl-c or similar during startup event := <-keysEvents
if err := fim.Process.Kill(); err != nil { if event.Err != nil {
log.Fatalf("failed to kill fim : %s", err) 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
} }
} }
}() }()