From 769029b9c228f34d4f45fbcfa728bde203d8ebff Mon Sep 17 00:00:00 2001 From: KemoNine Date: Fri, 4 Sep 2020 20:28:32 -0400 Subject: [PATCH] Fill out GUI some more, enable saving of config file on exit --- cmd/gui/README.md | 20 ++++++++----------- cmd/gui/gui.go | 5 +++++ ui/config.go | 50 +++++++++++++++++++++++++++++++++++++++++++---- ui/constants.go | 1 + 4 files changed, 60 insertions(+), 16 deletions(-) diff --git a/cmd/gui/README.md b/cmd/gui/README.md index 086d55e..3612526 100644 --- a/cmd/gui/README.md +++ b/cmd/gui/README.md @@ -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"] ``` diff --git a/cmd/gui/gui.go b/cmd/gui/gui.go index 2184c38..546827b 100644 --- a/cmd/gui/gui.go +++ b/cmd/gui/gui.go @@ -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) } diff --git a/ui/config.go b/ui/config.go index 8a5d5ad..2d5a087 100644 --- a/ui/config.go +++ b/ui/config.go @@ -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 diff --git a/ui/constants.go b/ui/constants.go index 0a49d8c..50f9e88 100644 --- a/ui/constants.go +++ b/ui/constants.go @@ -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"