Skip to content
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

Keybinding system for global keys #135

Merged
merged 17 commits into from
Dec 24, 2020

Conversation

phaedrus-jaf
Copy link
Contributor

I've been really enjoying using amfora, but it has different keybindings from qutebrowser and my terminal translates ctrl-t to ctrl-a,c to get screen to create a new tab/view. Also, my compositor eats all F1 keypresses. Re-doing the hardcoding has been nice for a couple weeks, but that doesn't let me keep up to date with y'all.

I think this code is pretty straightforward, let me know what you think or if you wanted a totally different design. (I know I didn't ask, I just wrote something that worked for me :)

@makew0rld
Copy link
Owner

makew0rld commented Dec 5, 2020

Hello, happy to hear you've been enjoying Amfora! Thanks for making this PR, keybindings is a good feature, and it's even better when it's not just on your fork :)

I think your code is good, but I find the format you chose kind of ugly/unclear to write. Using Rune: is verbose, it's weird to have Alt: but Ctrl+, etc. I was thinking you could take a look at cbind's key.go file, for an idea of how decoding would work. I find the way cbind encodes keybindings much cleaner and more intuitive. Feel free to copy that code entirely, it's under the MIT license. Note I'm not looking for the entire functionality of cbind to be copied, just the ideas in that one file.

Some other things:

  • Use TOML arrays instead separating keybindings with commas. This allows for easily binding commands to the comma key, as well as just simpler parsing
  • Please keep shiftnumbers in there. It's ugly, but I don't want to break existing configs.
  • Make sure you're not exporting functions/variables/structs that aren't used elsewhere

@phaedrus-jaf
Copy link
Contributor Author

Good point about the TOML parser... I feel dumb for not seeing that.

I decided to go with Alt- and keep Ctrl-, because it required only minor tweaks to what I had written (and it's simpler than the cbind code). I got rid of the 'Rune:' business. I also decided to handle Space as a special case. If we want to be able to have whitespace in there, a couple strings.ReplaceAll calls would allow that.

And finally, I re-added shift_numbers, because not breaking non-US configs is a good idea. The code I wrote does enforce 10 characters, where the old code did not. I might go back and make it more liberal in a bit here, I want to clear the merge conflict first.

@phaedrus-jaf
Copy link
Contributor Author

Ok, I think this should be ready to merge.

@phaedrus-jaf
Copy link
Contributor Author

Oh! I just realized something that should probably go with this: the help screen is static. I'll take a shot at updating it to display the configured keys later tonight. (I'm also happy to update the wiki to add a keybindings page under the configuration page)

@makew0rld
Copy link
Owner

Nice catch, thanks. I'll take a look at everything when I can, after you update the help page.

@makew0rld
Copy link
Owner

Looks like maybe that part is done? Let me know and I'll take a look soon.

@phaedrus-jaf
Copy link
Contributor Author

Yes, I got the help page working, and I think it's ready.

Copy link
Owner

@makew0rld makew0rld left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for working on this! Overall it looks good, I've made some specific comments. Also:

  • In general, put a space after the hashtag in the config file. So #bind_whatever becomes # bind_whatever.
  • Add comments to document functions

Also, are you interested in writing a wiki page on this? It would be under the configuration section, like the Proxying page.

config/config.go Outdated Show resolved Hide resolved
default-config.toml Outdated Show resolved Hide resolved
default-config.toml Outdated Show resolved Hide resolved
default-config.toml Outdated Show resolved Hide resolved
Comment on lines 11 to 32
CmdInvalid = 0
CmdLink1 = 1
CmdLink2 = 2
CmdLink3 = 3
CmdLink4 = 4
CmdLink5 = 5
CmdLink6 = 6
CmdLink7 = 7
CmdLink8 = 8
CmdLink9 = 9
CmdLink0 = 10
CmdTab1 = 11
CmdTab2 = 12
CmdTab3 = 13
CmdTab4 = 14
CmdTab5 = 15
CmdTab6 = 16
CmdTab7 = 17
CmdTab8 = 18
CmdTab9 = 19
CmdTab0 = 20
CmdBottom = iota
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason why you did it like this? You can just specify = iota at the start.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was meant as a semantic warning: there's some code that depends on these values being in this relation to each other. Essentially: CmdTab0 - CmdTab1 == 9, and CmdTab[N+1] == 1 + CmdTabN, thus rearranging these would break stuff. Basically, get future programmers to ask, wait, why does this break convention?

I'll add a comment to this effect.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, that makes sense. And yeah a comment is helpful, thanks.

config/keybindings.go Outdated Show resolved Hide resolved
config/keybindings.go Show resolved Hide resolved
k := e.Key()
if k == tcell.KeyRune {
cmd, ok = bindings[keyBinding{k, e.Modifiers(), e.Rune()}]
} else { // Sometimes tcell sets e.Rune() on non-KeyRune events.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where have you seen this? Just curious.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Primarily on the Ctrl-Letter-Key events. The event will have e.Key() == KeyCtrlA, e.Modifers() == 2 (for Control) and e.Rune() == 'A'. I originally added a correction for this when generating the bindings table, but it felt awkward, and would break if tcell changed behavior.

display/display.go Outdated Show resolved Hide resolved
display/help.go Outdated Show resolved Hide resolved
Committing suggested changes with diffs

Co-authored-by: makeworld <25111343+makeworld-the-better-one@users.noreply.github.com>
@phaedrus-jaf
Copy link
Contributor Author

Just commited the suggestions that could be committed in the GitHub UI. I'll go work through the rest of the conversations in a bit here. (And fix the linter error)

@phaedrus-jaf
Copy link
Contributor Author

And I think I mentioned it, but I'm definitely happy to update the wiki and add a page for keybindings under the configuration page.

@phaedrus-jaf
Copy link
Contributor Author

I think I addressed all the comments and the linter is happy. It probably makes sense to hold off on trying to update the wiki until after the merge.

Merge branch 'master' into key-remapping
@makew0rld
Copy link
Owner

Thanks for adding this! I'm having a little trouble grokking it but I think I'm just a bit tired right now. Overall it looks good.

@makew0rld makew0rld merged commit f10337e into makew0rld:master Dec 24, 2020
makew0rld added a commit that referenced this pull request Dec 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants