Skip to content

Commit

Permalink
Fix Spyder SetTheme method.
Browse files Browse the repository at this point in the history
ini package couldn't handle values starting with '#', even if inside quotes.
Ditched ini package, switched to line-by-line reading and then writing.
  • Loading branch information
sayandipdutta committed Jan 11, 2022
1 parent f5ae50b commit b125a2b
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions themeable/programs.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package themeable

import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"

"github.com/sayandipdutta/themechanger/setup"
"gopkg.in/ini.v1"
Expand Down Expand Up @@ -58,7 +60,7 @@ type Themeable interface {
// Given a themeable program and a theme name, it will set the theme
// If the theme name is not valid, it will return an error
func SetTheme(program Themeable, theme string) error {
fmt.Println("Setting theme to: ", program)
fmt.Println("Setting theme to: ", theme, program)
err := program.SetTheme(theme)
return err
}
Expand Down Expand Up @@ -138,15 +140,36 @@ func (programTheme PythonIDLE) SetTheme(theme string) error {
// Given a theme name, it will set the theme
// If the theme name is not valid, it will return an error
func (programTheme Spyder) SetTheme(theme string) error {
cfg, err := ini.Load(programTheme.ConfigPath)
cfg, err := os.Open(programTheme.ConfigPath)
if err != nil {
return err
}

var NewTheme string = programTheme.GetTheme(theme)
cfg.Section("appearance").Key("selected").SetValue(NewTheme)

if err = cfg.SaveTo(programTheme.ConfigPath); err != nil {
}
defer cfg.Close()
scanner := bufio.NewScanner(cfg)
scanner.Split(bufio.ScanLines)
scanner.Split(bufio.ScanLines)
var text []string
var flag bool = false

for scanner.Scan() {
t := scanner.Text()
if flag {
if strings.HasPrefix(t, "[") {
flag = false
}
if strings.HasPrefix(t, "selected =") {
t = "selected = " + programTheme.GetTheme(theme)
}
}
if !flag && strings.Contains(t, "[appearance]") {
flag = true
}
text = append(text, t)
}

data := []byte(strings.Join(text, "\n"))
if err = ioutil.WriteFile(programTheme.ConfigPath, data, 0644); err != nil {
return err
}
return nil
Expand Down

0 comments on commit b125a2b

Please sign in to comment.