diff options
| author | adamdottv <[email protected]> | 2025-06-18 13:56:46 -0500 |
|---|---|---|
| committer | adamdottv <[email protected]> | 2025-06-18 13:56:51 -0500 |
| commit | bd46cf0f868293b501874c1f04632ced3bec7b81 (patch) | |
| tree | bcd5ed78fc41bcf73c0ffd76086bf10c34ed700b /packages/tui/internal/config | |
| parent | d4157d9a9603c099e650af4f6c369a56d3878179 (diff) | |
| download | opencode-bd46cf0f868293b501874c1f04632ced3bec7b81.tar.gz opencode-bd46cf0f868293b501874c1f04632ced3bec7b81.zip | |
feat(tui): configurable keybinds and mouse scroll
Diffstat (limited to 'packages/tui/internal/config')
| -rw-r--r-- | packages/tui/internal/config/config.go | 67 |
1 files changed, 57 insertions, 10 deletions
diff --git a/packages/tui/internal/config/config.go b/packages/tui/internal/config/config.go index ad7b18d07..6474f75eb 100644 --- a/packages/tui/internal/config/config.go +++ b/packages/tui/internal/config/config.go @@ -9,23 +9,57 @@ import ( "github.com/BurntSushi/toml" ) -type Config struct { +type State struct { Theme string `toml:"theme"` Provider string `toml:"provider"` Model string `toml:"model"` } -// NewConfig creates a new Config instance with default values. -// This can be useful for initializing a new configuration file. +type Config struct { + Theme string `toml:"theme"` + Provider string `toml:"provider"` + Model string `toml:"model"` + Keybinds map[string]string `toml:"keybinds"` +} + +func NewState() *State { + return &State{ + Theme: "opencode", + } +} + func NewConfig() *Config { + keybinds := make(map[string]string) + keybinds["leader"] = "ctrl+x" return &Config{ - Theme: "opencode", + Keybinds: keybinds, + } +} + +func ConfigToState(config *Config) *State { + return &State{ + Theme: config.Theme, + Provider: config.Provider, + Model: config.Model, } } -// SaveConfig writes the provided Config struct to the specified TOML file. +func MergeState(state *State, config *Config) *Config { + if config.Theme == "" { + config.Theme = state.Theme + } + if config.Provider == "" { + config.Provider = state.Provider + } + if config.Model == "" { + config.Model = state.Model + } + return config +} + +// SaveState writes the provided Config struct to the specified TOML file. // It will create the file if it doesn't exist, or overwrite it if it does. -func SaveConfig(filePath string, config *Config) error { +func SaveState(filePath string, state *State) error { file, err := os.Create(filePath) if err != nil { return fmt.Errorf("failed to create/open config file %s: %w", filePath, err) @@ -34,14 +68,14 @@ func SaveConfig(filePath string, config *Config) error { writer := bufio.NewWriter(file) encoder := toml.NewEncoder(writer) - if err := encoder.Encode(config); err != nil { - return fmt.Errorf("failed to encode config to TOML file %s: %w", filePath, err) + if err := encoder.Encode(state); err != nil { + return fmt.Errorf("failed to encode state to TOML file %s: %w", filePath, err) } if err := writer.Flush(); err != nil { - return fmt.Errorf("failed to flush writer for config file %s: %w", filePath, err) + return fmt.Errorf("failed to flush writer for state file %s: %w", filePath, err) } - slog.Debug("Configuration saved to file", "file", filePath) + slog.Debug("State saved to file", "file", filePath) return nil } @@ -57,3 +91,16 @@ func LoadConfig(filePath string) (*Config, error) { } return &config, nil } + +// LoadState loads the state from the specified TOML file. +// It returns a pointer to the State struct and an error if any issues occur. +func LoadState(filePath string) (*State, error) { + var state State + if _, err := toml.DecodeFile(filePath, &state); err != nil { + if _, statErr := os.Stat(filePath); os.IsNotExist(statErr) { + return nil, fmt.Errorf("state file not found at %s: %w", filePath, statErr) + } + return nil, fmt.Errorf("failed to decode TOML from file %s: %w", filePath, err) + } + return &state, nil +} |
