Fill out GUI some more, enable saving of config file on exit

This commit is contained in:
KemoNine 2020-09-04 20:28:32 -04:00
parent e326984ca6
commit 769029b9c2
4 changed files with 60 additions and 16 deletions

View file

@ -8,22 +8,18 @@ The GUI will work off a config similar to the following. Use the ```generate con
```
[slideshow]
slideinterval = "300s"
restartinterval = "7d"
# Slideshow intervals
slideshow.slideinterval = "300s"
slideshow.restartinterval = "7d"
[hdmi]
# These are SYSTEMD.TIME formatted times
# You probably want to just adjust the actual times here
# See https://www.freedesktop.org/software/systemd/man/systemd.time.html
off = "*-*-* 00:00:00"
on = "*-*-* 06:00:00"
hdmi.off = "*-*-* 00:00:00"
hdmi.on = "*-*-* 06:00:00"
[albums]
root = "/tank/pictures"
selected = [
"/",
"/KemoNine"
]
# Album configuration
albums.root = "/tank/pictures"
albums.selected = ["/", "/KemoNine"]
```

View file

@ -74,5 +74,10 @@ func main() {
ui.Slideshow(pfConfig)
}
// Reset the CLI flag so it's never writted to the config as 'true'
pfConfig.Load(confmap.Provider(map[string]interface{}{
ui.CLI_FLAG_CONFIG_ONLY: false,
}, "."), nil)
ui.ConfigGui(pfConfig)
}

View file

@ -7,12 +7,14 @@ import (
"net"
"os"
"os/exec"
"io/ioutil"
"path/filepath"
"strings"
"github.com/gdamore/tcell"
"github.com/guillermo/go.procmeminfo"
"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/toml"
"github.com/rivo/tview"
"git.kemonine.info/PiFrame/utils"
@ -27,6 +29,7 @@ const (
const (
PAGE_MAIN_UI = "PAGE_MAIN_UI"
PAGE_SAVE_EXIT = "PAGE_SAVE_EXIT"
PAGE_EXIT = "PAGE_EXIT"
PAGE_REBOOT = "PAGE_REBOOT"
PAGE_POWEROFF = "PAGE_POWEROFF"
@ -103,12 +106,20 @@ func ConfigGui(config *koanf.Koanf) {
AddItem(headerSubTitle, 0, 1, false)
// Footer fields (Left Column)
saveExitButton := tview.NewButton("Save & Exit").
SetBackgroundColorActivated(tcell.ColorGray)
saveExitButton.SetLabelColor(tcell.ColorBlack).
SetBorder(true).
SetBorderColor(tcell.ColorBlack).
SetBackgroundColor(tcell.ColorGreen).
SetRect(0, 0, 22, 3)
exitButton := tview.NewButton("Exit").
SetBackgroundColorActivated(tcell.ColorGray)
exitButton.SetLabelColor(tcell.ColorBlack).
SetBorder(true).
SetBorderColor(tcell.ColorBlack).
SetBackgroundColor(tcell.ColorGreen).
SetBackgroundColor(tcell.ColorYellow).
SetRect(0, 0, 22, 3)
rebootButton := tview.NewButton("Reboot").
@ -116,7 +127,7 @@ func ConfigGui(config *koanf.Koanf) {
rebootButton.SetLabelColor(tcell.ColorBlack).
SetBorder(true).
SetBorderColor(tcell.ColorBlack).
SetBackgroundColor(tcell.ColorYellow).
SetBackgroundColor(tcell.ColorFuchsia).
SetRect(0, 0, 22, 3)
powerOffButton := tview.NewButton("Power Off").
@ -129,7 +140,8 @@ func ConfigGui(config *koanf.Koanf) {
// Footer
footer := tview.NewFlex()
footer.AddItem(exitButton, 0, 1, false).
footer.AddItem(saveExitButton, 0, 1, false).
AddItem(exitButton, 0, 1, false).
AddItem(rebootButton, 0, 1, false).
AddItem(powerOffButton, 0, 1, false)
// Setup menu
@ -360,6 +372,36 @@ func ConfigGui(config *koanf.Koanf) {
pages.AddPage(PAGE_MAIN_UI, mainUI, true, true)
// Button modals
saveExitModal := tview.NewModal().
SetText("Are you sure you want to [red]SAVE [red]& [red]EXIT?").
AddButtons([]string{"Yes", "Cancel"}).
SetDoneFunc(func(buttonIndex int, buttonLabel string) {
if buttonLabel == "Yes" {
parser := toml.Parser()
dataForDisk, err := config.Marshal(parser)
if err != nil {
log.Fatalf("Error preparing config for disk write : %s", err)
}
existingStat, err := os.Lstat(CONFIG_FILE_PATH)
if err != nil {
log.Fatalf("Error checking if config exists : %s", err)
}
if err := ioutil.WriteFile(CONFIG_FILE_PATH_TMP, dataForDisk, existingStat.Mode()); err != nil {
log.Fatalf("Error writing temp config file : %s", err)
}
if err := os.Rename(CONFIG_FILE_PATH_TMP, CONFIG_FILE_PATH); err != nil {
log.Fatalf("Error moving new config in place : %s", err)
}
app.Stop()
os.Exit(0)
}
pages.SwitchToPage(PAGE_MAIN_UI)
})
pages.AddPage(PAGE_SAVE_EXIT, saveExitModal, true, false)
saveExitButton.SetSelectedFunc(func() {
pages.ShowPage(PAGE_SAVE_EXIT)
})
exitModal := tview.NewModal().
SetText("Are you sure you want to [red]EXIT?").
AddButtons([]string{"Yes", "Cancel"}).
@ -409,7 +451,7 @@ func ConfigGui(config *koanf.Koanf) {
})
// Setup tracking of which are of the UI can/has focus
primitivesThatCanFocus := []tview.Primitive{menu, exitButton, rebootButton, powerOffButton}
primitivesThatCanFocus := []tview.Primitive{menu, saveExitButton, exitButton, rebootButton, powerOffButton}
currentFocus := 0
// Setup basic switching between main menu and buttons for the UI

View file

@ -6,6 +6,7 @@ const (
const (
CONFIG_FILE_PATH = "/etc/default/pf.toml"
CONFIG_FILE_PATH_TMP = "/etc/default/pf.toml.new"
CONFIG_KEY_SLIDESHOW_INTERVAL = "slideshow.slideinterval"
CONFIG_KEY_SLIDESHOW_RESTART_INTERVAL = "slideshow.restartinterval"
CONFIG_KEY_HDMI_OFF = "hdmi.off"