GeezerVison is showing github.com — feat(tui): configurable keybinds and mouse scroll · klevo/opencode@bd46cf0
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .gitignore -2Lines changed: 0 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -5,5 +5,3 @@ node_modules | 5 | 5 | .env | 6 | 6 | .idea | 7 | 7 | .vscode | 8 | | -app.log | 9 | | -gopls.log |
Collapse file Copy file name to clipboardExpand all lines: README.md +42-2Lines changed: 42 additions & 2 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -71,8 +71,49 @@ theme = "opencode" | 71 | 71 | provider = "anthropic" | 72 | 72 | model = "claude-sonnet-4-20250514" | 73 | 73 | autoupdate = true | | 74 | + | | 75 | +keybinds.leader = "ctrl+x" | | 76 | +keybinds.session_new = "<leader>n" | | 77 | +keybinds.editor_open = "<leader>e" | | 78 | +``` | | 79 | + | | 80 | +#### Keybinds | | 81 | + | | 82 | +You can configure the keybinds in the global config file. (Note: values listed below are the defaults.) | | 83 | + | | 84 | +```toml | | 85 | +# ~/.config/opencode/config | | 86 | +keybinds.leader = "ctrl+x" | | 87 | +keybinds.help = "<leader>h" | | 88 | +keybinds.editor_open = "<leader>e" | | 89 | +keybinds.session_new = "<leader>n" | | 90 | +keybinds.session_list = "<leader>l" | | 91 | +keybinds.session_share = "<leader>s" | | 92 | +keybinds.session_interrupt = "esc" | | 93 | +keybinds.session_compact = "<leader>c" | | 94 | +keybinds.tool_details = "<leader>d" | | 95 | +keybinds.model_list = "<leader>m" | | 96 | +keybinds.theme_list = "<leader>t" | | 97 | +keybinds.project_init = "<leader>i" | | 98 | +keybinds.input_clear = "ctrl+c" | | 99 | +keybinds.input_paste = "ctrl+v" | | 100 | +keybinds.input_submit = "enter" | | 101 | +keybinds.input_newline = "shift+enter" | | 102 | +keybinds.history_previous = "up" | | 103 | +keybinds.history_next = "down" | | 104 | +keybinds.messages_page_up = "pgup" | | 105 | +keybinds.messages_page_down = "pgdown" | | 106 | +keybinds.messages_half_page_up = "ctrl+alt+u" | | 107 | +keybinds.messages_half_page_down = "ctrl+alt+d" | | 108 | +keybinds.messages_previous = "ctrl+alt+k" | | 109 | +keybinds.messages_next = "ctrl+alt+j" | | 110 | +keybinds.messages_first = "ctrl+g" | | 111 | +keybinds.messages_last = "ctrl+alt+g" | | 112 | +keybinds.app_exit = "ctrl+c,<leader>q" | 74 | 113 | ``` | 75 | 114 |
| | 115 | +#### Models.dev | | 116 | + | 76 | 117 | You can also extend the models.dev database with your own providers by mirroring the structure found [here](https://github.com/sst/models.dev/tree/dev/providers/anthropic) | 77 | 118 |
| 78 | 119 | Start with a `provider.toml` file in `~/.config/opencode/providers` | @@ -171,8 +212,7 @@ To run. | 171 | 212 |
| 172 | 213 | ```bash | 173 | 214 | $ bun install | 174 | | -$ cd packages/opencode | 175 | | -$ bun run src/index.ts | | 215 | +$ bun run packages/opencode/src/index.ts | 176 | 216 | ``` | 177 | 217 |
| 178 | 218 | ### FAQ | |
Collapse file Copy file name to clipboardExpand all lines: packages/tui/cmd/opencode/main.go +19-26Lines changed: 19 additions & 26 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -23,10 +23,28 @@ func main() { | 23 | 23 | } | 24 | 24 |
| 25 | 25 | url := os.Getenv("OPENCODE_SERVER") | | 26 | + | 26 | 27 | appInfoStr := os.Getenv("OPENCODE_APP_INFO") | 27 | 28 | var appInfo client.AppInfo | 28 | 29 | json.Unmarshal([]byte(appInfoStr), &appInfo) | 29 | 30 |
| | 31 | + logfile := filepath.Join(appInfo.Path.Data, "log", "tui.log") | | 32 | + if _, err := os.Stat(filepath.Dir(logfile)); os.IsNotExist(err) { | | 33 | + err := os.MkdirAll(filepath.Dir(logfile), 0755) | | 34 | + if err != nil { | | 35 | + slog.Error("Failed to create log directory", "error", err) | | 36 | + os.Exit(1) | | 37 | + } | | 38 | + } | | 39 | + file, err := os.Create(logfile) | | 40 | + if err != nil { | | 41 | + slog.Error("Failed to create log file", "error", err) | | 42 | + os.Exit(1) | | 43 | + } | | 44 | + defer file.Close() | | 45 | + logger := slog.New(slog.NewTextHandler(file, &slog.HandlerOptions{Level: slog.LevelDebug})) | | 46 | + slog.SetDefault(logger) | | 47 | + | 30 | 48 | httpClient, err := client.NewClientWithResponses(url) | 31 | 49 | if err != nil { | 32 | 50 | slog.Error("Failed to create client", "error", err) | @@ -46,7 +64,7 @@ func main() { | 46 | 64 | tui.NewModel(app_), | 47 | 65 | tea.WithAltScreen(), | 48 | 66 | tea.WithKeyboardEnhancements(), | 49 | | - // tea.WithMouseCellMotion(), | | 67 | + tea.WithMouseCellMotion(), | 50 | 68 | ) | 51 | 69 |
| 52 | 70 | eventClient, err := client.NewClient(url) | @@ -67,35 +85,10 @@ func main() { | 67 | 85 | } | 68 | 86 | }() | 69 | 87 |
| 70 | | - go func() { | 71 | | - paths, err := httpClient.PostPathGetWithResponse(context.Background()) | 72 | | - if err != nil { | 73 | | - panic(err) | 74 | | - } | 75 | | - logfile := filepath.Join(paths.JSON200.Data, "log", "tui.log") | 76 | | - | 77 | | - if _, err := os.Stat(filepath.Dir(logfile)); os.IsNotExist(err) { | 78 | | - err := os.MkdirAll(filepath.Dir(logfile), 0755) | 79 | | - if err != nil { | 80 | | - slog.Error("Failed to create log directory", "error", err) | 81 | | - os.Exit(1) | 82 | | - } | 83 | | - } | 84 | | - file, err := os.Create(logfile) | 85 | | - if err != nil { | 86 | | - slog.Error("Failed to create log file", "error", err) | 87 | | - os.Exit(1) | 88 | | - } | 89 | | - defer file.Close() | 90 | | - logger := slog.New(slog.NewTextHandler(file, &slog.HandlerOptions{Level: slog.LevelDebug})) | 91 | | - slog.SetDefault(logger) | 92 | | - }() | 93 | | - | 94 | 88 | // Run the TUI | 95 | 89 | result, err := program.Run() | 96 | 90 | if err != nil { | 97 | 91 | slog.Error("TUI error", "error", err) | 98 | | - // return fmt.Errorf("TUI error: %v", err) | 99 | 92 | } | 100 | 93 |
| 101 | 94 | slog.Info("TUI exited", "result", result) | |
Collapse file Copy file name to clipboardExpand all lines: packages/tui/internal/app/app.go +48-22Lines changed: 48 additions & 22 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,16 +19,16 @@ import ( | 19 | 19 | var RootPath string | 20 | 20 |
| 21 | 21 | type App struct { | 22 | | - Info client.AppInfo | 23 | | - Version string | 24 | | - ConfigPath string | 25 | | - Config *config.Config | 26 | | - Client *client.ClientWithResponses | 27 | | - Provider *client.ProviderInfo | 28 | | - Model *client.ModelInfo | 29 | | - Session *client.SessionInfo | 30 | | - Messages []client.MessageInfo | 31 | | - Commands commands.Registry | | 22 | + Info client.AppInfo | | 23 | + Version string | | 24 | + StatePath string | | 25 | + Config *config.Config | | 26 | + Client *client.ClientWithResponses | | 27 | + Provider *client.ProviderInfo | | 28 | + Model *client.ModelInfo | | 29 | + Session *client.SessionInfo | | 30 | + Messages []client.MessageInfo | | 31 | + Commands commands.CommandRegistry | 32 | 32 | } | 33 | 33 |
| 34 | 34 | type SessionSelectedMsg = *client.SessionInfo | @@ -38,6 +38,10 @@ type ModelSelectedMsg struct { | 38 | 38 | } | 39 | 39 | type SessionClearedMsg struct{} | 40 | 40 | type CompactSessionMsg struct{} | | 41 | +type SendMsg struct { | | 42 | + Text string | | 43 | + Attachments []Attachment | | 44 | +} | 41 | 45 |
| 42 | 46 | func New( | 43 | 47 | ctx context.Context, | @@ -51,19 +55,33 @@ func New( | 51 | 55 | appConfig, err := config.LoadConfig(appConfigPath) | 52 | 56 | if err != nil { | 53 | 57 | appConfig = config.NewConfig() | 54 | | - config.SaveConfig(appConfigPath, appConfig) | 55 | 58 | } | 56 | | - theme.SetTheme(appConfig.Theme) | | 59 | + if len(appConfig.Keybinds) == 0 { | | 60 | + appConfig.Keybinds = make(map[string]string) | | 61 | + appConfig.Keybinds["leader"] = "ctrl+x" | | 62 | + } | | 63 | + | | 64 | + appStatePath := filepath.Join(appInfo.Path.State, "tui") | | 65 | + appState, err := config.LoadState(appStatePath) | | 66 | + if err != nil { | | 67 | + appState = config.NewState() | | 68 | + config.SaveState(appStatePath, appState) | | 69 | + } | | 70 | + | | 71 | + mergedConfig := config.MergeState(appState, appConfig) | | 72 | + theme.SetTheme(mergedConfig.Theme) | | 73 | + | | 74 | + slog.Debug("Loaded config", "config", mergedConfig) | 57 | 75 |
| 58 | 76 | app := &App{ | 59 | | - Info: appInfo, | 60 | | - Version: version, | 61 | | - ConfigPath: appConfigPath, | 62 | | - Config: appConfig, | 63 | | - Client: httpClient, | 64 | | - Session: &client.SessionInfo{}, | 65 | | - Messages: []client.MessageInfo{}, | 66 | | - Commands: commands.NewCommandRegistry(), | | 77 | + Info: appInfo, | | 78 | + Version: version, | | 79 | + StatePath: appStatePath, | | 80 | + Config: mergedConfig, | | 81 | + Client: httpClient, | | 82 | + Session: &client.SessionInfo{}, | | 83 | + Messages: []client.MessageInfo{}, | | 84 | + Commands: commands.LoadFromConfig(mergedConfig), | 67 | 85 | } | 68 | 86 |
| 69 | 87 | return app, nil | @@ -160,8 +178,12 @@ func (a *App) IsBusy() bool { | 160 | 178 | return lastMessage.Metadata.Time.Completed == nil | 161 | 179 | } | 162 | 180 |
| 163 | | -func (a *App) SaveConfig() { | 164 | | - config.SaveConfig(a.ConfigPath, a.Config) | | 181 | +func (a *App) SaveState() { | | 182 | + state := config.ConfigToState(a.Config) | | 183 | + err := config.SaveState(a.StatePath, state) | | 184 | + if err != nil { | | 185 | + slog.Error("Failed to save state", "error", err) | | 186 | + } | 165 | 187 | } | 166 | 188 |
| 167 | 189 | func (a *App) InitializeProject(ctx context.Context) tea.Cmd { | @@ -348,3 +370,7 @@ func (a *App) ListProviders(ctx context.Context) ([]client.ProviderInfo, error) | 348 | 370 | providers := *resp.JSON200 | 349 | 371 | return providers.Providers, nil | 350 | 372 | } | | 373 | + | | 374 | +// func (a *App) loadCustomKeybinds() { | | 375 | +// | | 376 | +// } |
|