summaryrefslogtreecommitdiff
path: root/raveos-hyprland-theme/theme-data/DankMaterialShell/core/internal/keybinds/discovery.go
diff options
context:
space:
mode:
Diffstat (limited to 'raveos-hyprland-theme/theme-data/DankMaterialShell/core/internal/keybinds/discovery.go')
-rw-r--r--raveos-hyprland-theme/theme-data/DankMaterialShell/core/internal/keybinds/discovery.go107
1 files changed, 107 insertions, 0 deletions
diff --git a/raveos-hyprland-theme/theme-data/DankMaterialShell/core/internal/keybinds/discovery.go b/raveos-hyprland-theme/theme-data/DankMaterialShell/core/internal/keybinds/discovery.go
new file mode 100644
index 0000000..4990975
--- /dev/null
+++ b/raveos-hyprland-theme/theme-data/DankMaterialShell/core/internal/keybinds/discovery.go
@@ -0,0 +1,107 @@
+package keybinds
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+ "strings"
+
+ "github.com/AvengeMedia/DankMaterialShell/core/internal/utils"
+)
+
+type DiscoveryConfig struct {
+ SearchPaths []string
+}
+
+func DefaultDiscoveryConfig() *DiscoveryConfig {
+ var searchPaths []string
+
+ configDir, err := os.UserConfigDir()
+ if err == nil && configDir != "" {
+ searchPaths = append(searchPaths, filepath.Join(configDir, "DankMaterialShell", "cheatsheets"))
+ }
+
+ configDirs := os.Getenv("XDG_CONFIG_DIRS")
+ if configDirs != "" {
+ for dir := range strings.SplitSeq(configDirs, ":") {
+ if dir != "" {
+ searchPaths = append(searchPaths, filepath.Join(dir, "DankMaterialShell", "cheatsheets"))
+ }
+ }
+ }
+
+ return &DiscoveryConfig{
+ SearchPaths: searchPaths,
+ }
+}
+
+func (d *DiscoveryConfig) FindJSONFiles() ([]string, error) {
+ var files []string
+
+ for _, searchPath := range d.SearchPaths {
+ expandedPath, err := utils.ExpandPath(searchPath)
+ if err != nil {
+ continue
+ }
+
+ if _, err := os.Stat(expandedPath); os.IsNotExist(err) {
+ continue
+ }
+
+ entries, err := os.ReadDir(expandedPath)
+ if err != nil {
+ continue
+ }
+
+ for _, entry := range entries {
+ if entry.IsDir() {
+ continue
+ }
+
+ if !strings.HasSuffix(entry.Name(), ".json") {
+ continue
+ }
+
+ fullPath := filepath.Join(expandedPath, entry.Name())
+ files = append(files, fullPath)
+ }
+ }
+
+ return files, nil
+}
+
+type JSONProviderFactory func(filePath string) (Provider, error)
+
+var jsonProviderFactory JSONProviderFactory
+
+func SetJSONProviderFactory(factory JSONProviderFactory) {
+ jsonProviderFactory = factory
+}
+
+func AutoDiscoverProviders(registry *Registry, config *DiscoveryConfig) error {
+ if config == nil {
+ config = DefaultDiscoveryConfig()
+ }
+
+ if jsonProviderFactory == nil {
+ return nil
+ }
+
+ files, err := config.FindJSONFiles()
+ if err != nil {
+ return fmt.Errorf("failed to discover JSON files: %w", err)
+ }
+
+ for _, file := range files {
+ provider, err := jsonProviderFactory(file)
+ if err != nil {
+ continue
+ }
+
+ if err := registry.Register(provider); err != nil {
+ continue
+ }
+ }
+
+ return nil
+}