piframe-go/cmd/ui/ui.go

103 lines
2.5 KiB
Go
Raw Normal View History

2020-08-25 22:55:05 +00:00
package main
import (
"fmt"
"io"
2020-08-28 22:33:03 +00:00
"log"
"os/exec"
"time"
2020-08-28 22:33:03 +00:00
"github.com/eiannone/keyboard"
2020-08-28 22:33:03 +00:00
"github.com/gdamore/tcell"
"github.com/rivo/tview"
2020-08-25 22:55:05 +00:00
)
const (
CMD_FIM = "/usr/local/bin/pf-fim.sh"
SLIDESHOW_INTERVAL = 300 * time.Second
)
2020-08-25 22:55:05 +00:00
func main() {
// fim placeholder so we can operate on it when a exit slideshow is received
2020-08-28 22:33:03 +00:00
var fim *exec.Cmd = nil
// 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()
}()
2020-08-28 22:33:03 +00:00
fmt.Println("Press ESC to kill slideshow")
2020-08-28 22:33:03 +00:00
go func() {
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)
}
2020-08-28 22:33:03 +00:00
}
break
2020-08-28 22:33:03 +00:00
}
}
}()
2020-08-25 22:55:05 +00:00
// Run slideshow
fim = exec.Command(CMD_FIM)
// Setup stdin for fim to control slideshow
stdin, err := fim.StdinPipe()
if err != nil {
log.Fatalf("Error getting fim stdin : %s", err)
}
// Advance slideshow every interval as defined in const()
ticker := time.NewTicker(SLIDESHOW_INTERVAL)
stop_ticker := make(chan struct{})
go func() {
for {
select {
case <-ticker.C:
_, err = io.WriteString(stdin, "n")
if err != nil {
log.Fatalf("Error advancing slides : %s", err)
}
case <-stop_ticker:
ticker.Stop()
stdin.Close()
return
2020-08-28 22:33:03 +00:00
}
2020-08-26 00:18:29 +00:00
}
}()
2020-08-25 22:55:05 +00:00
// Run fim
if err := fim.Run(); err != nil {
// Unwrap the error a bit so we can find out if a signal killed fim or something else
// An exit code of -1 means the program didn't exit in time or was terminated by a signal (per the docs)
if exitError, ok := err.(*exec.ExitError); ok && exitError.ExitCode() != -1 {
log.Fatalf("Error running fim : %s", err)
2020-08-28 22:33:03 +00:00
}
2020-08-25 22:55:05 +00:00
}
// Stop fim slideshow advancing go routine
close(stop_ticker)
// Run config UI when slideshow stops
app := tview.NewApplication()
frame := tview.NewFrame(tview.NewBox().SetBackgroundColor(tcell.ColorBlack)).
SetBorders(2, 2, 2, 2, 4, 4).
AddText("PiFrame", true, tview.AlignCenter, tcell.ColorWhite).
AddText("Configuration Utility", true, tview.AlignCenter, tcell.ColorRed)
if err := app.SetRoot(frame, true).EnableMouse(true).Run(); err != nil {
log.Fatalf("Error running UI : %s", err)
}
2020-08-25 22:55:05 +00:00
}