-
Notifications
You must be signed in to change notification settings - Fork 355
feat: user configurable tui keybindings #2415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
joshbarrington
wants to merge
4
commits into
docker:main
Choose a base branch
from
joshbarrington:tui-configurable-keybindings
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| package core | ||
|
|
||
| import ( | ||
| "log/slog" | ||
| "strings" | ||
| "sync" | ||
|
|
||
| "charm.land/bubbles/v2/key" | ||
|
|
||
| "github.com/docker/docker-agent/pkg/userconfig" | ||
| ) | ||
|
|
||
| // KeyMap contains global keybindings used across the TUI | ||
| type KeyMap struct { | ||
| Quit key.Binding | ||
| SwitchFocus key.Binding | ||
| Commands key.Binding | ||
| Help key.Binding | ||
| ToggleYolo key.Binding | ||
| ToggleHideToolResults key.Binding | ||
| CycleAgent key.Binding | ||
| ModelPicker key.Binding | ||
| ClearQueue key.Binding | ||
| Suspend key.Binding | ||
| ToggleSidebar key.Binding | ||
| EditExternal key.Binding | ||
| HistorySearch key.Binding | ||
| } | ||
|
|
||
| var ( | ||
| cachedKeys KeyMap | ||
| keysOnce sync.Once | ||
| ) | ||
|
|
||
| // DefaultKeyMap returns the default keybindings | ||
| func DefaultKeyMap() KeyMap { | ||
| return KeyMap{ | ||
| Quit: key.NewBinding(key.WithKeys("ctrl+c"), key.WithHelp("ctrl+c", "quit")), | ||
| SwitchFocus: key.NewBinding(key.WithKeys("tab"), key.WithHelp("tab", "switch focus")), | ||
| Commands: key.NewBinding(key.WithKeys("ctrl+k"), key.WithHelp("ctrl+k", "commands")), | ||
| Help: key.NewBinding(key.WithKeys("ctrl+h", "f1", "ctrl+?"), key.WithHelp("ctrl+h", "help")), | ||
| ToggleYolo: key.NewBinding(key.WithKeys("ctrl+y"), key.WithHelp("ctrl+y", "toggle yolo mode")), | ||
| ToggleHideToolResults: key.NewBinding(key.WithKeys("ctrl+o"), key.WithHelp("ctrl+o", "toggle hide tool results")), | ||
| CycleAgent: key.NewBinding(key.WithKeys("ctrl+s"), key.WithHelp("ctrl+s", "cycle agent")), | ||
| ModelPicker: key.NewBinding(key.WithKeys("ctrl+m"), key.WithHelp("ctrl+m", "model picker")), | ||
| ClearQueue: key.NewBinding(key.WithKeys("ctrl+x"), key.WithHelp("ctrl+x", "clear queue")), | ||
| Suspend: key.NewBinding(key.WithKeys("ctrl+z"), key.WithHelp("ctrl+z", "suspend")), | ||
| ToggleSidebar: key.NewBinding(key.WithKeys("ctrl+b"), key.WithHelp("ctrl+b", "toggle sidebar")), | ||
| EditExternal: key.NewBinding(key.WithKeys("ctrl+g"), key.WithHelp("ctrl+g", "edit in external editor")), | ||
| HistorySearch: key.NewBinding(key.WithKeys("ctrl+r"), key.WithHelp("ctrl+r", "history search")), | ||
| } | ||
| } | ||
|
|
||
| type keyField struct { | ||
| binding *key.Binding | ||
| help string | ||
| } | ||
|
|
||
| func validateKeys(keys []string, action string, boundKeys map[string]string) []string { | ||
| var validKeys []string | ||
| for _, k := range keys { | ||
| kStr := strings.TrimSpace(k) | ||
| if kStr == "" || strings.Contains(kStr, " ") { | ||
| slog.Warn("Invalid key string ignored", "action", action, "key", k) | ||
| continue | ||
| } | ||
|
|
||
| if existingAction, exists := boundKeys[kStr]; exists { | ||
| slog.Warn("Keybinding conflict detected", "key", kStr, "action", action, "conflicts_with", existingAction) | ||
| } else { | ||
| boundKeys[kStr] = action | ||
| } | ||
|
|
||
| validKeys = append(validKeys, kStr) | ||
| } | ||
| return validKeys | ||
| } | ||
|
|
||
| // applyUserKeybindings loops through user-defined keybindings and overrides the defaults. | ||
| // Basic string validation and key conflict detection is applied, any issues are logged. | ||
| func applyUserKeybindings(bindings []userconfig.Keybinding, actionMap map[string]keyField) { | ||
| boundKeys := make(map[string]string) | ||
|
|
||
| for _, b := range bindings { | ||
| if len(b.Keys) == 0 { | ||
| slog.Warn("Keybinding ignored: no keys specified", "action", b.Action) | ||
| continue | ||
| } | ||
|
|
||
| if f, ok := actionMap[b.Action]; ok { | ||
| validKeys := validateKeys(b.Keys, b.Action, boundKeys) | ||
|
|
||
| if len(validKeys) > 0 { | ||
| *f.binding = key.NewBinding(key.WithKeys(validKeys...), key.WithHelp(validKeys[0], f.help)) | ||
| } | ||
| } else { | ||
| slog.Warn("Unrecognized keybinding action", "action", b.Action) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // buildKeys merges user config overrides with the defaults to produce a KeyMap. | ||
| // This is separated from GetKeys() to allow testing with mock settings. | ||
| func buildKeys(settings *userconfig.Settings) KeyMap { | ||
| keys := DefaultKeyMap() | ||
|
|
||
| if settings != nil && settings.Keybindings != nil { | ||
| actionMap := map[string]keyField{ | ||
| "quit": {&keys.Quit, "quit"}, | ||
| "switch_focus": {&keys.SwitchFocus, "switch focus"}, | ||
| "commands": {&keys.Commands, "commands"}, | ||
| "help": {&keys.Help, "help"}, | ||
| "toggle_yolo": {&keys.ToggleYolo, "toggle yolo mode"}, | ||
| "toggle_hide_tool_results": {&keys.ToggleHideToolResults, "toggle hide tool results"}, | ||
| "cycle_agent": {&keys.CycleAgent, "cycle agent"}, | ||
| "model_picker": {&keys.ModelPicker, "model picker"}, | ||
| "clear_queue": {&keys.ClearQueue, "clear queue"}, | ||
| "suspend": {&keys.Suspend, "suspend"}, | ||
| "toggle_sidebar": {&keys.ToggleSidebar, "toggle sidebar"}, | ||
| "edit_external": {&keys.EditExternal, "edit in external editor"}, | ||
| "history_search": {&keys.HistorySearch, "history search"}, | ||
| } | ||
|
|
||
| applyUserKeybindings(*settings.Keybindings, actionMap) | ||
| } | ||
|
|
||
| return keys | ||
| } | ||
|
|
||
| // GetKeys returns the current keybindings, merging user config overrides with defaults. | ||
| // The result is cached after the first call. | ||
| func GetKeys() KeyMap { | ||
|
joshbarrington marked this conversation as resolved.
|
||
| keysOnce.Do(func() { | ||
| cachedKeys = buildKeys(userconfig.Get()) | ||
| }) | ||
|
|
||
| return cachedKeys | ||
| } | ||
|
|
||
| // ResetKeys clears the cached keybindings, allowing them to be reloaded. | ||
| // This is primarily useful for testing or future hot-reload support. | ||
| func ResetKeys() { | ||
| keysOnce = sync.Once{} | ||
| } | ||
|
joshbarrington marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| package core | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "charm.land/bubbles/v2/key" | ||
| "github.com/goccy/go-yaml" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/docker/docker-agent/pkg/userconfig" | ||
| ) | ||
|
|
||
| func TestBuildKeys_Defaults(t *testing.T) { | ||
| keys := buildKeys(nil) | ||
|
|
||
| // Verify defaults | ||
| assert.Equal(t, []string{"ctrl+c"}, keys.Quit.Keys()) | ||
| assert.Equal(t, []string{"tab"}, keys.SwitchFocus.Keys()) | ||
| assert.Equal(t, []string{"ctrl+k"}, keys.Commands.Keys()) | ||
| assert.Equal(t, []string{"ctrl+h", "f1", "ctrl+?"}, keys.Help.Keys()) | ||
| assert.Equal(t, []string{"ctrl+y"}, keys.ToggleYolo.Keys()) | ||
| assert.Equal(t, []string{"ctrl+o"}, keys.ToggleHideToolResults.Keys()) | ||
| assert.Equal(t, []string{"ctrl+s"}, keys.CycleAgent.Keys()) | ||
| assert.Equal(t, []string{"ctrl+m"}, keys.ModelPicker.Keys()) | ||
| assert.Equal(t, []string{"ctrl+x"}, keys.ClearQueue.Keys()) | ||
| assert.Equal(t, []string{"ctrl+z"}, keys.Suspend.Keys()) | ||
| assert.Equal(t, []string{"ctrl+b"}, keys.ToggleSidebar.Keys()) | ||
| assert.Equal(t, []string{"ctrl+g"}, keys.EditExternal.Keys()) | ||
| assert.Equal(t, []string{"ctrl+r"}, keys.HistorySearch.Keys()) | ||
| } | ||
|
|
||
| func TestBuildKeys_Overrides(t *testing.T) { | ||
| settings := &userconfig.Settings{ | ||
| Keybindings: &[]userconfig.Keybinding{ | ||
| {Action: "quit", Keys: []string{"ctrl+q"}}, | ||
| {Action: "switch_focus", Keys: []string{"ctrl+t"}}, | ||
| {Action: "commands", Keys: []string{"f2", "ctrl+k"}}, | ||
| {Action: "unknown_action", Keys: []string{"ctrl+u"}}, // Should be ignored | ||
| }, | ||
| } | ||
|
|
||
| keys := buildKeys(settings) | ||
|
|
||
| // Verify overrides | ||
| assert.Equal(t, []string{"ctrl+q"}, keys.Quit.Keys()) | ||
| assert.Equal(t, []string{"ctrl+t"}, keys.SwitchFocus.Keys()) | ||
|
|
||
| // Verify arrays are maintained | ||
| assert.Equal(t, []string{"f2", "ctrl+k"}, keys.Commands.Keys()) | ||
|
|
||
| // Verify defaults are preserved where not overridden | ||
| assert.Equal(t, []string{"ctrl+h", "f1", "ctrl+?"}, keys.Help.Keys()) | ||
| assert.Equal(t, []string{"ctrl+y"}, keys.ToggleYolo.Keys()) | ||
| assert.Equal(t, []string{"ctrl+o"}, keys.ToggleHideToolResults.Keys()) | ||
| assert.Equal(t, []string{"ctrl+s"}, keys.CycleAgent.Keys()) | ||
| assert.Equal(t, []string{"ctrl+m"}, keys.ModelPicker.Keys()) | ||
| assert.Equal(t, []string{"ctrl+x"}, keys.ClearQueue.Keys()) | ||
| assert.Equal(t, []string{"ctrl+z"}, keys.Suspend.Keys()) | ||
| assert.Equal(t, []string{"ctrl+b"}, keys.ToggleSidebar.Keys()) | ||
| assert.Equal(t, []string{"ctrl+g"}, keys.EditExternal.Keys()) | ||
| assert.Equal(t, []string{"ctrl+r"}, keys.HistorySearch.Keys()) | ||
| } | ||
|
|
||
| func TestBuildKeys_EmptySettings(t *testing.T) { | ||
| settings := &userconfig.Settings{} | ||
| keys := buildKeys(settings) | ||
|
|
||
| // Verify defaults | ||
| assert.Equal(t, []string{"ctrl+c"}, keys.Quit.Keys()) | ||
| assert.Equal(t, []string{"tab"}, keys.SwitchFocus.Keys()) | ||
| } | ||
|
|
||
| func TestBuildKeys_EmptyKey(t *testing.T) { | ||
| settings := &userconfig.Settings{ | ||
| Keybindings: &[]userconfig.Keybinding{ | ||
| {Action: "quit", Keys: []string{}}, // Should be ignored | ||
| }, | ||
| } | ||
| keys := buildKeys(settings) | ||
|
|
||
| // Verify defaults remain | ||
| assert.Equal(t, []string{"ctrl+c"}, keys.Quit.Keys()) | ||
| } | ||
|
|
||
| func TestBuildKeys_InvalidKeysAndConflicts(t *testing.T) { | ||
| settings := &userconfig.Settings{ | ||
| Keybindings: &[]userconfig.Keybinding{ | ||
| {Action: "quit", Keys: []string{"ctrl+q", " ", ""}}, // spaces and empty should be ignored | ||
| {Action: "suspend", Keys: []string{"ctrl+q"}}, // conflict with quit | ||
| }, | ||
| } | ||
|
|
||
| keys := buildKeys(settings) | ||
|
|
||
| // Valid keys should still be applied | ||
| assert.Equal(t, []string{"ctrl+q"}, keys.Quit.Keys()) | ||
| assert.Equal(t, []string{"ctrl+q"}, keys.Suspend.Keys()) | ||
| } | ||
|
|
||
| func TestBuildKeys_FromYAML(t *testing.T) { | ||
| yamlConfig := ` | ||
| settings: | ||
| keybindings: | ||
| - action: "quit" | ||
| keys: ["ctrl+q"] | ||
| - action: "commands" | ||
| keys: ["f2", "ctrl+k"] | ||
| - action: "history_search" | ||
| keys: ["ctrl+f"] | ||
| ` | ||
|
|
||
| var config userconfig.Config | ||
| err := yaml.Unmarshal([]byte(yamlConfig), &config) | ||
| require.NoError(t, err) | ||
|
|
||
| keys := buildKeys(config.Settings) | ||
|
|
||
| // Verify the keys loaded correctly from the YAML unmarshal | ||
| assert.Equal(t, []string{"ctrl+q"}, keys.Quit.Keys()) | ||
| assert.Equal(t, []string{"f2", "ctrl+k"}, keys.Commands.Keys()) | ||
| assert.Equal(t, []string{"ctrl+f"}, keys.HistorySearch.Keys()) | ||
|
|
||
| // Verify defaults are preserved for missing YAML fields | ||
| assert.Equal(t, []string{"tab"}, keys.SwitchFocus.Keys()) | ||
| assert.Equal(t, []string{"ctrl+h", "f1", "ctrl+?"}, keys.Help.Keys()) | ||
| } | ||
|
|
||
| func TestResetKeys(t *testing.T) { | ||
| // Call GetKeys to initialize sync.Once | ||
| _ = GetKeys() | ||
|
|
||
| // Keep a copy of original to restore later | ||
| originalCached := cachedKeys | ||
|
|
||
| // Modify cachedKeys to a bogus value | ||
| cachedKeys.Quit = key.NewBinding(key.WithKeys("bogus")) | ||
|
|
||
| // Calling GetKeys again should still return the bogus value because sync.Once isn't reset | ||
| assert.Equal(t, []string{"bogus"}, GetKeys().Quit.Keys()) | ||
|
|
||
| // Reset keys | ||
| ResetKeys() | ||
|
|
||
| // Calling GetKeys now should re-initialize from default/config | ||
| assert.NotEqual(t, []string{"bogus"}, GetKeys().Quit.Keys()) | ||
|
|
||
| // Clean up | ||
| cachedKeys = originalCached | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.