-
Notifications
You must be signed in to change notification settings - Fork 178
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
support modifying default keybindings prototype #356
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package keys | |
import ( | ||
"github.com/charmbracelet/bubbles/help" | ||
"github.com/charmbracelet/bubbles/key" | ||
log "github.com/charmbracelet/log" | ||
|
||
"github.com/dlvhdr/gh-dash/config" | ||
) | ||
|
@@ -84,7 +85,7 @@ func (k KeyMap) QuitAndHelpKeys() []key.Binding { | |
return []key.Binding{k.Help, k.Quit} | ||
} | ||
|
||
var Keys = KeyMap{ | ||
var Keys = &KeyMap{ | ||
Up: key.NewBinding( | ||
key.WithKeys("up", "k"), | ||
key.WithHelp("↑/k", "move up"), | ||
|
@@ -158,3 +159,52 @@ var Keys = KeyMap{ | |
key.WithHelp("q", "quit"), | ||
), | ||
} | ||
|
||
// Rebind will update our saved keybindings from configuration values. | ||
func Rebind(universal, issueKeys, prKeys []config.Keybinding) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do you mean to handle There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It matches the pr keys stuff so I just didn't handle it yet with the questions I had, but it will be part of this PR. |
||
err := rebindUniversal(universal) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = rebindPRKeys(prKeys) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func rebindUniversal(universal []config.Keybinding) error { | ||
for _, kb := range universal { | ||
log.Debug("Rebinding key", "builtin", kb.Builtin, "key", kb.Key) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will be adding to prs and issue keys as well. |
||
switch kb.Builtin { | ||
case "up": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can use the keys.Keys global var here instead of the switch case. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The switch is used to map the yaml string to the keybinding in the global var. I am not sure how to go bind the two otherwise. Other then maybe reflection. |
||
Keys.Up = kb.NewBinding(&Keys.Up) | ||
case "down": | ||
Keys.Down = kb.NewBinding(&Keys.Down) | ||
case "firstline": // should this be first-line or firstLine? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using camel case for keys in next commit. |
||
case "lastline": | ||
case "togglepreview": | ||
case "opengithub": | ||
case "refresh": | ||
case "refreshall": | ||
case "pagedown": | ||
case "pageup": | ||
case "nextsection": | ||
case "prevsection": | ||
case "switchview": | ||
Keys.SwitchView = kb.NewBinding(&Keys.SwitchView) | ||
case "search": | ||
case "copyurl": | ||
case "copynumber": | ||
case "help": | ||
case "quit": | ||
default: | ||
// TODO: return an error here | ||
return nil | ||
} | ||
} | ||
|
||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package keys | |
|
||
import ( | ||
"github.com/charmbracelet/bubbles/key" | ||
"github.com/dlvhdr/gh-dash/config" | ||
) | ||
|
||
type PRKeyMap struct { | ||
|
@@ -74,3 +75,45 @@ func PRFullHelp() []key.Binding { | |
PRKeys.WatchChecks, | ||
} | ||
} | ||
|
||
func rebindPRKeys(keys []config.Keybinding) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see there's the |
||
for _, prKey := range keys { | ||
var k key.Binding | ||
|
||
switch prKey.Builtin { | ||
case "assign": | ||
k = PRKeys.Assign | ||
case "unassign": | ||
k = PRKeys.Unassign | ||
case "comment": | ||
k = PRKeys.Comment | ||
case "diff": | ||
k = PRKeys.Diff | ||
case "checkout": | ||
k = PRKeys.Checkout | ||
case "close": | ||
k = PRKeys.Close | ||
case "ready": | ||
k = PRKeys.Ready | ||
case "reopen": | ||
k = PRKeys.Reopen | ||
case "merge": | ||
k = PRKeys.Merge | ||
case "watchchecks": | ||
k = PRKeys.WatchChecks | ||
default: | ||
// TODO: return an error here | ||
return nil | ||
} | ||
|
||
// Not really sure if this is the best idea but I am not | ||
// sure how else we are meant to define alt keys. | ||
if len(prKey.Keys) > 0 { | ||
k.SetKeys(prKey.Keys...) | ||
} else { | ||
k.SetKeys(prKey.Key) | ||
} | ||
} | ||
|
||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,7 @@ import ( | |
) | ||
|
||
type Model struct { | ||
keys keys.KeyMap | ||
keys *keys.KeyMap | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am actually not sure this is used, it should be a duplicate of the pre-created KeyMap from the keys.go file. |
||
sidebar sidebar.Model | ||
prSidebar prsidebar.Model | ||
issueSidebar issuesidebar.Model | ||
|
@@ -82,10 +82,7 @@ func NewModel(configPath string) Model { | |
} | ||
|
||
func (m *Model) initScreen() tea.Msg { | ||
var err error | ||
|
||
config, err := config.ParseConfig(m.ctx.ConfigPath) | ||
if err != nil { | ||
showError := func(err error) { | ||
styles := log.DefaultStyles() | ||
styles.Key = lipgloss.NewStyle(). | ||
Foreground(lipgloss.Color("1")). | ||
|
@@ -107,10 +104,24 @@ func (m *Model) initScreen() tea.Msg { | |
"err", | ||
err, | ||
) | ||
} | ||
|
||
cfg, err := config.ParseConfig(m.ctx.ConfigPath) | ||
if err != nil { | ||
showError(err) | ||
return initMsg{Config: cfg} | ||
} | ||
|
||
err = keys.Rebind( | ||
cfg.Keybindings.Universal, | ||
cfg.Keybindings.Issues, | ||
cfg.Keybindings.Prs, | ||
) | ||
if err != nil { | ||
showError(err) | ||
} | ||
|
||
return initMsg{Config: config} | ||
return initMsg{Config: cfg} | ||
} | ||
|
||
func (m Model) Init() tea.Cmd { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you expect people to want alts? Is this a personal need of yours? I don't expect many users will actually define multiple keys for the same action.
If there's not a big use case, maybe it's better to omit to reduce the config bloat
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea I wasn't entirely sure, I can't see myself particularly using it. So that is fair.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed in latest commit