Skip to content

Commit

Permalink
🐛 Fix option key
Browse files Browse the repository at this point in the history
The option key on terminals such as macOS Terminal and iTerm2 sends
special characters and not Esc+. Add support for both types.

There is a limitation that alt+enter cannot be detected so an
alternative keybinding has been added.
  • Loading branch information
mikelorant committed Feb 6, 2023
1 parent d62cf62 commit 20901bf
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,24 @@ useful.

## ⚠️ Limitations [](#committed)

### Option Key

The option key needs to be set to send the `meta` or `esc+` keycode. Terminals such
as macOS Terminal or iTerm2 may not have this as default. If not set correctly it will
not be possible to apply a commit.

To make these changes following the instructions below.

| Terminal | Setting |
| :------------- | :----------------------------------------------------------- |
| macOS Terminal | `Profiles` `Keyboard`<br />![macOS Terminal Keyboard Setting](docs/keyboard-options-macos-terminal.png) |
| iTerm2 | `Preferences` `Profile ` `Keys`<br />![iTerm2 Keyboard Setting](docs/keyboard-options-iterm2.png) |

The alternative keyboard shortcut <kbd>⌥ Option</kbd> + <kbd>\\</kbd> can also be used to apply
a commit.

### Rendering Borders

Terminals render emojis differently and this makes alignment of borders
complicated and difficult. It is an ongoing process to improve the compatibility
with terminals. The following list are the terminals that have been tested.
Expand Down Expand Up @@ -239,6 +257,7 @@ The global shortcuts can be used within any view.
| Key Binding | Command |
| :--------------------------------------- | :----------------- |
| <kbd>⌥ Option</kbd> + <kbd>⏎ Enter</kbd> | Commit |
| <kbd>⌥ Option</kbd> + <kbd>\\</kbd> | Commit |
| <kbd>⌥ Option</kbd> + <kbd>S</kbd> | Toggle sign-off |
| <kbd>⌥ Option</kbd> + <kbd>T</kbd> | Toggle theme |
| <kbd>⌥ Option</kbd> + <kbd>/</kbd> | Help |
Expand Down
Binary file added docs/keyboard-options-iterm2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/keyboard-options-macos-terminal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 22 additions & 10 deletions internal/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ const (
bodyName = "Body"
)

const (
KeyAmend = "å"
KeyLoad = "¬"
KeySignoff = "ß"
KeyTheme = "†"
KeyAuthor = "¡"
KeyEmoji = "™"
KeySummary = "£"
KeyBody = "¢"
KeyHelp = "˙"
)

func New() Model {
return Model{
Date: time.Now(),
Expand Down Expand Up @@ -228,22 +240,22 @@ func (m Model) View() string {

func (m Model) onKeyPress(msg tea.KeyMsg) keyResponse {
switch msg.String() {
case "alt+1":
case "alt+1", KeyAuthor:
if m.focus == authorComponent {
return keyResponse{model: m, nilMsg: true}
}
m.focus = authorComponent
case "alt+2":
case "alt+2", KeyEmoji:
if m.focus == emojiComponent {
return keyResponse{model: m, nilMsg: true}
}
m.focus = emojiComponent
case "alt+3":
case "alt+3", KeySummary:
if m.focus == summaryComponent {
return keyResponse{model: m, nilMsg: true}
}
m.focus = summaryComponent
case "alt+4":
case "alt+4", KeyBody:
if m.focus == bodyComponent {
return keyResponse{model: m, nilMsg: true}
}
Expand All @@ -259,15 +271,15 @@ func (m Model) onKeyPress(msg tea.KeyMsg) keyResponse {
case summaryComponent:
m.focus = bodyComponent
}
case "alt+enter":
case "alt+enter", "alt+\\":
if !m.validate() {
break
}

m = m.commit(applyQuit)

return keyResponse{model: m, cmd: tea.Quit, end: true}
case "alt+a":
case "alt+a", KeyAmend:
m.amend = !m.amend

m.swapSave()
Expand All @@ -276,21 +288,21 @@ func (m Model) onKeyPress(msg tea.KeyMsg) keyResponse {
m.models.body.CursorStart()

return keyResponse{model: m, end: false, nilMsg: true}
case "alt+l":
case "alt+l", KeyLoad:
if m.setSave() {
m.models.header.CursorStartSummary()
m.models.body.CursorStart()
}

return keyResponse{model: m, end: false, nilMsg: true}
case "alt+s":
case "alt+s", KeySignoff:
m.signoff = !m.signoff

return keyResponse{model: m, end: false, nilMsg: true}
case "alt+t":
case "alt+t", KeyTheme:
m.state.Theme.NextTint()
return keyResponse{model: m, cmd: theme.UpdateTheme, end: true}
case "ctrl+h":
case "ctrl+h", KeyHelp:
if m.focus == helpComponent {
m.focus = m.previousFocus
break
Expand Down

0 comments on commit 20901bf

Please sign in to comment.