From 9f0e3dd98c4f5eecd0a31f3644eb89a7090b773f Mon Sep 17 00:00:00 2001 From: Sr Komodo Date: Mon, 24 Sep 2018 23:31:18 -0300 Subject: [PATCH] add option for automatically enabling firefox's dark theme --- fallbackui.go | 7 ++++++- install.go | 33 ++++++++++++++++++++++++++++++++- ui.go | 14 +++++++++++++- 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/fallbackui.go b/fallbackui.go index b81eb1d..1ae43f0 100644 --- a/fallbackui.go +++ b/fallbackui.go @@ -51,8 +51,13 @@ func createFallbackUI() { fmt.Print("\nWould you like to auto-generate UUIDs? [y/n] ") fmt.Scanln(&choice) + uuids := (choice == "y" || choice == "Y") - message, err := install(profile, (choice == "y" || choice == "Y")) + fmt.Print("\nWould you like to automatically set the Firefox dark theme? [y/n] ") + fmt.Scanln(&choice) + theme := (choice == "y" || choice == "Y") + + message, err := install(profile, uuids, theme) if err != nil { fmt.Printf("%s: %s", message, err.Error()) fmt.Scanln() diff --git a/install.go b/install.go index 2a963ae..6bebd16 100644 --- a/install.go +++ b/install.go @@ -11,6 +11,10 @@ import ( "time" ) +const darkThemeConfig = ` +user_pref("lightweightThemes.selectedThemeID", "firefox-compact-dark@mozilla.org"); +user_pref("devtools.theme", "dark");` + func uninstall(profile string) (string, error) { err := os.RemoveAll(filepath.Join(profile, "chrome", "ShadowFox_customization")) if err != nil { @@ -102,7 +106,7 @@ func addColorOverrides(source, colors string) string { return source[:startI] + colors + source[endI:] } -func install(profilePath string, generateUUIDs bool) (string, error) { +func install(profilePath string, generateUUIDs bool, setTheme bool) (string, error) { // Helper variables to keep things DRY chromePath := filepath.Join(profilePath, "chrome") customPath := filepath.Join(chromePath, "ShadowFox_customization") @@ -215,6 +219,33 @@ func install(profilePath string, generateUUIDs bool) (string, error) { } } + // Set dark theme + if setTheme { + userJs := filepath.Join(profilePath, "user.js") + err = backUp(userJs) + if err != nil { + return "Couldn't backup user.js", err + } + + err = createFile(userJs) + if err != nil { + return "Couldn't create user.js", err + } + + userJsContent, err := ioutil.ReadFile(userJs) + if err != nil { + return "Couldn't read user.js", err + } + + if !strings.Contains(string(userJsContent), darkThemeConfig) { + userJsContent = append(userJsContent, []byte(darkThemeConfig)...) + + if err := ioutil.WriteFile(userJs, userJsContent, 0644); err != nil { + return "Couldn't write user.js", err + } + } + } + // Write new files if err := ioutil.WriteFile(userChromePath, []byte(userChrome), 0644); err != nil { return "Couldn't write userChrome.css to file", err diff --git a/ui.go b/ui.go index 299f092..fce5558 100644 --- a/ui.go +++ b/ui.go @@ -34,8 +34,10 @@ func createUI() error { uuidCheckBox := tview.NewCheckbox().SetLabel("Auto-Generate UUIDs: ").SetChecked(false) + themeCheckBox := tview.NewCheckbox().SetLabel("Set Firefox dark theme: ").SetChecked(false) + installButton := tview.NewButton("Install/Update ShadowFox").SetSelectedFunc(func() { - message, err := install(paths[profileIndex], uuidCheckBox.IsChecked()) + message, err := install(paths[profileIndex], uuidCheckBox.IsChecked(), themeCheckBox.IsChecked()) if err != nil { notifyErr(message, err) } else { @@ -66,6 +68,8 @@ func createUI() error { case uninstallButton.HasFocus(): app.SetFocus(installButton) case installButton.HasFocus(): + app.SetFocus(themeCheckBox) + case themeCheckBox.HasFocus(): app.SetFocus(uuidCheckBox) case uuidCheckBox.HasFocus(): app.SetFocus(profileSelect) @@ -77,6 +81,8 @@ func createUI() error { case profileSelect.HasFocus(): app.SetFocus(uuidCheckBox) case uuidCheckBox.HasFocus(): + app.SetFocus(themeCheckBox) + case themeCheckBox.HasFocus(): app.SetFocus(installButton) case installButton.HasFocus(): app.SetFocus(uninstallButton) @@ -103,6 +109,12 @@ func createUI() error { AddItem(nil, 0, 1, false), 1, 0, true, ). AddItem(nil, 1, 0, false). + AddItem(tview.NewFlex(). + AddItem(nil, 0, 1, false). + AddItem(themeCheckBox, 0, 10, true). + AddItem(nil, 0, 1, false), 1, 0, true, + ). + AddItem(nil, 1, 0, false). AddItem(infoText, 5, 0, false). AddItem(nil, 1, 0, false). AddItem(tview.NewFlex().