Skip to content

Commit

Permalink
feat: per module theming
Browse files Browse the repository at this point in the history
  • Loading branch information
abenz1267 committed Aug 4, 2024
1 parent 8c736b1 commit 5d6eb25
Show file tree
Hide file tree
Showing 9 changed files with 713 additions and 576 deletions.
1 change: 1 addition & 0 deletions config/config.default.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"builtins": {
"applications": {
"name": "applications",
"theme": "catppuccin",
"placeholder": "Applications",
"actions": true,
"prioritize_new": true,
Expand Down
115 changes: 2 additions & 113 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ import (
"embed"
_ "embed"
"errors"
"fmt"
"io/fs"
"log"
"os"
"os/exec"
"path/filepath"

"github.com/abenz1267/walker/util"
"github.com/spf13/viper"
Expand All @@ -35,8 +32,6 @@ type Config struct {
Theme string `mapstructure:"theme"`
Terminal string `mapstructure:"terminal"`

// internal
UI *UI `mapstructure:"-"`
Available []string `mapstructure:"-"`
IsService bool `mapstructure:"-"`
}
Expand Down Expand Up @@ -78,6 +73,7 @@ type GeneralModule struct {
Prefix string `mapstructure:"prefix"`
Refresh bool `mapstructure:"refresh"`
SwitcherOnly bool `mapstructure:"switcher_only"`
Theme string `mapstructure:"theme"`
Typeahead bool `mapstructure:"typeahead"`
ShowSubWhenSingle bool `mapstructure:"show_sub_when_single"`

Expand Down Expand Up @@ -177,7 +173,7 @@ type List struct {
SingleClick bool `mapstructure:"single_click"`
}

func Get(config string, explicitTheme string) *Config {
func Get(config string) *Config {
os.MkdirAll(util.ThemeDir(), 0755)

defs := viper.New()
Expand Down Expand Up @@ -221,92 +217,13 @@ func Get(config string, explicitTheme string) *Config {
}
}

var layout []byte

theme := viper.GetString("theme")

if explicitTheme != "" {
theme = explicitTheme
}

layoutFt := "json"

file := filepath.Join(util.ThemeDir(), fmt.Sprintf("%s.json", theme))

path := fmt.Sprintf("%s/", util.ThemeDir())

filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error {
if d.IsDir() {
return nil
}

switch d.Name() {
case fmt.Sprintf("%s.json", theme):
layoutFt = "json"
file = path
case fmt.Sprintf("%s.toml", theme):
layoutFt = "toml"
file = path
case fmt.Sprintf("%s.yaml", theme):
layoutFt = "yaml"
file = path
}

return nil
})

if _, err := os.Stat(file); err == nil {
layout, err = os.ReadFile(file)
if err != nil {
log.Panicln(err)
}
} else {
layoutFt = "json"

switch theme {
case "kanagawa":
layout, err = themes.ReadFile("themes/kanagawa.json")
if err != nil {
log.Panicln(err)
}

createLayoutFile(layout)
case "catppuccin":
layout, err = themes.ReadFile("themes/catppuccin.json")
if err != nil {
log.Panicln(err)
}

createLayoutFile(layout)
default:
log.Printf("layout file for theme '%s' not found\n", theme)
os.Exit(1)
}
}

cfg := &Config{}

err = viper.Unmarshal(cfg)
if err != nil {
log.Panic(err)
}

layoutCfg := viper.New()
layoutCfg.SetConfigType(layoutFt)

err = layoutCfg.ReadConfig(bytes.NewBuffer(layout))
if err != nil {
log.Panicln(err)
}

ui := &UICfg{}
err = layoutCfg.Unmarshal(ui)
if err != nil {
log.Panic(err)
}

cfg.UI = ui.UI

go setTerminal(cfg)

return cfg
Expand Down Expand Up @@ -388,31 +305,3 @@ func setTerminal(cfg *Config) {
}
}
}

func createLayoutFile(data []byte) {
ft := "json"

et := os.Getenv("WALKER_CONFIG_TYPE")

if et != "" {
ft = et
}

layout := viper.New()
layout.SetConfigType("json")

err := layout.ReadConfig(bytes.NewBuffer(data))
if err != nil {
log.Panicln(err)
}

layout.AddConfigPath(util.ThemeDir())

layout.SetConfigType(ft)
layout.SetConfigName(viper.GetString("theme"))

wErr := layout.SafeWriteConfig()
if wErr != nil {
log.Println(wErr)
}
}
116 changes: 115 additions & 1 deletion config/ui.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
package config

import "github.com/diamondburned/gotk4/pkg/gtk/v4"
import (
"bytes"
"fmt"
"io/fs"
"log"
"os"
"path/filepath"

"github.com/abenz1267/walker/util"
"github.com/diamondburned/gotk4/pkg/gtk/v4"
"github.com/spf13/viper"
)

type UICfg struct {
UI *UI `mapstructure:"ui"`
Expand Down Expand Up @@ -167,3 +178,106 @@ type TextWrapper struct {
Revert *bool `mapstructure:"revert"`
Sub *LabelWidget `mapstructure:"sub"`
}

func GetLayout(theme string) *UI {
var layout []byte

layoutFt := "json"

file := filepath.Join(util.ThemeDir(), fmt.Sprintf("%s.json", theme))

path := fmt.Sprintf("%s/", util.ThemeDir())

filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error {
if d.IsDir() {
return nil
}

switch d.Name() {
case fmt.Sprintf("%s.json", theme):
layoutFt = "json"
file = path
case fmt.Sprintf("%s.toml", theme):
layoutFt = "toml"
file = path
case fmt.Sprintf("%s.yaml", theme):
layoutFt = "yaml"
file = path
}

return nil
})

if _, err := os.Stat(file); err == nil {
layout, err = os.ReadFile(file)
if err != nil {
log.Panicln(err)
}
} else {
layoutFt = "json"

switch theme {
case "kanagawa":
layout, err = themes.ReadFile("themes/kanagawa.json")
if err != nil {
log.Panicln(err)
}

createLayoutFile(layout)
case "catppuccin":
layout, err = themes.ReadFile("themes/catppuccin.json")
if err != nil {
log.Panicln(err)
}

createLayoutFile(layout)
default:
log.Printf("layout file for theme '%s' not found\n", theme)
os.Exit(1)
}
}

layoutCfg := viper.New()
layoutCfg.SetConfigType(layoutFt)

err := layoutCfg.ReadConfig(bytes.NewBuffer(layout))
if err != nil {
log.Panicln(err)
}

ui := &UICfg{}
err = layoutCfg.Unmarshal(ui)
if err != nil {
log.Panic(err)
}

return ui.UI
}

func createLayoutFile(data []byte) {
ft := "json"

et := os.Getenv("WALKER_CONFIG_TYPE")

if et != "" {
ft = et
}

layout := viper.New()
layout.SetConfigType("json")

err := layout.ReadConfig(bytes.NewBuffer(data))
if err != nil {
log.Panicln(err)
}

layout.AddConfigPath(util.ThemeDir())

layout.SetConfigType(ft)
layout.SetConfigName(viper.GetString("theme"))

wErr := layout.SafeWriteConfig()
if wErr != nil {
log.Println(wErr)
}
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func main() {
state.IsService = slices.Contains(args, "--gapplication-service")

if state.IsService {
cfg := config.Get(state.ExplicitConfig, state.ExplicitTheme)
cfg := config.Get(state.ExplicitConfig)
state.StartServiceableModules(cfg)
}

Expand Down
4 changes: 2 additions & 2 deletions ui/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (h *Handler) handle() {

if len(h.entries) > 0 {
glib.IdleAdd(func() {
ui.items.Splice(0, int(ui.items.NItems()), h.entries...)
common.items.Splice(0, int(common.items.NItems()), h.entries...)
})
}

Expand All @@ -53,7 +53,7 @@ func (h *Handler) handle() {

func sortEntries(entries []util.Entry) {
slices.SortFunc(entries, func(a, b util.Entry) int {
text := ui.input.Text()
text := elements.input.Text()

if text == "" {
if a.Matching == util.AlwaysTopOnEmptySearch && b.Matching != util.AlwaysTopOnEmptySearch {
Expand Down
Loading

0 comments on commit 5d6eb25

Please sign in to comment.