Skip to content

Commit

Permalink
add option for automatically enabling firefox's dark theme
Browse files Browse the repository at this point in the history
  • Loading branch information
arguablykomodo committed Sep 25, 2018
1 parent 699e04a commit 9f0e3dd
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
7 changes: 6 additions & 1 deletion fallbackui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
33 changes: 32 additions & 1 deletion install.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (
"time"
)

const darkThemeConfig = `
user_pref("lightweightThemes.selectedThemeID", "[email protected]");
user_pref("devtools.theme", "dark");`

func uninstall(profile string) (string, error) {
err := os.RemoveAll(filepath.Join(profile, "chrome", "ShadowFox_customization"))
if err != nil {
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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().
Expand Down

0 comments on commit 9f0e3dd

Please sign in to comment.