piframe-go/cmd/ui/ui.go

91 lines
2.3 KiB
Go

package main
import (
"io"
"log"
"os"
"os/exec"
"os/signal"
"syscall"
"time"
"github.com/gdamore/tcell"
"github.com/rivo/tview"
)
const (
CMD_FIM = "/usr/local/bin/pf-fim.sh"
SLIDESHOW_INTERVAL = 300 * time.Second
)
func main() {
// fim placeholder so we can operate on it when a signal 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) }()
// Goroutine to handle os signals (nuke fim so we can get to config ui)
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)
}
}
}
}()
// 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
}
}
}()
// 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)
}
}
// 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)
}
}