1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/gui/keybindings.go

528 lines
14 KiB
Go
Raw Normal View History

package gui
2018-08-08 11:18:41 +02:00
import (
"github.com/jesseduffield/gocui"
)
2018-08-08 11:18:41 +02:00
// Binding - a keybinding mapping a key and modifier to a handler. The keypress
// is only handled if the given view has focus, or handled globally if the view
// is ""
type Binding struct {
2018-08-29 11:56:28 +02:00
ViewName string
Handler func(*gocui.Gui, *gocui.View) error
Key interface{} // FIXME: find out how to get `gocui.Key | rune`
Modifier gocui.Modifier
2018-08-28 20:13:01 +02:00
Description string
2018-08-08 11:18:41 +02:00
}
// GetDisplayStrings returns the display string of a file
func (b *Binding) GetDisplayStrings() []string {
return []string{b.GetKey(), b.Description}
}
2018-11-30 02:47:14 +02:00
// GetKey is a function.
func (b *Binding) GetKey() string {
key := 0
switch b.Key.(type) {
case rune:
key = int(b.Key.(rune))
case gocui.Key:
key = int(b.Key.(gocui.Key))
}
// special keys
switch key {
case 27:
return "esc"
case 13:
return "enter"
case 32:
return "space"
}
return string(key)
}
2018-11-30 02:47:14 +02:00
// GetKeybindings is a function.
func (gui *Gui) GetKeybindings() []*Binding {
bindings := []*Binding{
2018-09-01 12:10:03 +02:00
{
ViewName: "",
Key: 'q',
Modifier: gocui.ModNone,
Handler: gui.quit,
}, {
ViewName: "",
Key: gocui.KeyCtrlC,
Modifier: gocui.ModNone,
Handler: gui.quit,
}, {
ViewName: "",
Key: gocui.KeyEsc,
Modifier: gocui.ModNone,
Handler: gui.quit,
}, {
ViewName: "",
Key: gocui.KeyPgup,
Modifier: gocui.ModNone,
Handler: gui.scrollUpMain,
}, {
ViewName: "",
Key: gocui.KeyPgdn,
Modifier: gocui.ModNone,
Handler: gui.scrollDownMain,
}, {
ViewName: "",
Key: gocui.KeyCtrlU,
Modifier: gocui.ModNone,
Handler: gui.scrollUpMain,
}, {
ViewName: "",
Key: gocui.KeyCtrlD,
Modifier: gocui.ModNone,
Handler: gui.scrollDownMain,
}, {
2018-09-05 13:23:06 +02:00
ViewName: "",
Key: 'P',
Modifier: gocui.ModNone,
Handler: gui.pushFiles,
2018-09-05 13:01:21 +02:00
Description: gui.Tr.SLocalize("push"),
2018-09-01 12:10:03 +02:00
}, {
2018-09-05 13:23:06 +02:00
ViewName: "",
Key: 'p',
Modifier: gocui.ModNone,
Handler: gui.pullFiles,
2018-09-05 13:01:21 +02:00
Description: gui.Tr.SLocalize("pull"),
2018-09-01 12:10:03 +02:00
}, {
2018-09-05 13:23:06 +02:00
ViewName: "",
Key: 'R',
Modifier: gocui.ModNone,
Handler: gui.handleRefresh,
2018-09-05 13:01:21 +02:00
Description: gui.Tr.SLocalize("refresh"),
2018-09-01 12:10:03 +02:00
}, {
ViewName: "",
2018-09-05 15:55:24 +02:00
Key: 'x',
2018-09-01 12:10:03 +02:00
Modifier: gocui.ModNone,
2018-09-18 13:07:25 +02:00
Handler: gui.handleCreateOptionsMenu,
2018-09-01 12:10:03 +02:00
}, {
ViewName: "status",
Key: 'e',
Modifier: gocui.ModNone,
Handler: gui.handleEditConfig,
Description: gui.Tr.SLocalize("EditConfig"),
}, {
ViewName: "status",
Key: 'o',
Modifier: gocui.ModNone,
Handler: gui.handleOpenConfig,
Description: gui.Tr.SLocalize("OpenConfig"),
}, {
ViewName: "status",
Key: 'u',
Modifier: gocui.ModNone,
Handler: gui.handleCheckForUpdate,
Description: gui.Tr.SLocalize("checkForUpdate"),
2018-09-01 12:10:03 +02:00
}, {
ViewName: "status",
Key: 's',
Modifier: gocui.ModNone,
2018-09-19 11:15:29 +02:00
Handler: gui.handleCreateRecentReposMenu,
Description: gui.Tr.SLocalize("SwitchRepo"),
},
{
2018-09-01 12:10:03 +02:00
ViewName: "files",
Key: 'c',
Modifier: gocui.ModNone,
Handler: gui.handleCommitPress,
Description: gui.Tr.SLocalize("CommitChanges"),
2018-09-12 15:20:35 +02:00
}, {
ViewName: "files",
Key: 'A',
2018-09-12 15:20:35 +02:00
Modifier: gocui.ModNone,
Handler: gui.handleAmendCommitPress,
Description: gui.Tr.SLocalize("AmendLastCommit"),
2018-09-01 12:10:03 +02:00
}, {
ViewName: "files",
Key: 'C',
Modifier: gocui.ModNone,
Handler: gui.handleCommitEditorPress,
Description: gui.Tr.SLocalize("CommitChangesWithEditor"),
}, {
2018-09-03 18:44:56 +02:00
ViewName: "files",
Key: gocui.KeySpace,
Modifier: gocui.ModNone,
Handler: gui.handleFilePress,
Description: gui.Tr.SLocalize("toggleStaged"),
2018-09-01 12:10:03 +02:00
}, {
2018-09-01 14:03:43 +02:00
ViewName: "files",
Key: 'd',
Modifier: gocui.ModNone,
Handler: gui.handleFileRemove,
Description: gui.Tr.SLocalize("removeFile"),
2018-09-01 12:10:03 +02:00
}, {
2018-12-08 07:54:54 +02:00
ViewName: "files", // TODO: might make this for more views as well
2018-09-01 14:20:45 +02:00
Key: 'm',
Modifier: gocui.ModNone,
2018-12-08 07:54:54 +02:00
Handler: gui.handleCreateRebaseOptionsMenu,
Description: gui.Tr.SLocalize("ViewMergeRebaseOptions"),
2018-09-01 12:10:03 +02:00
}, {
2018-09-01 14:20:45 +02:00
ViewName: "files",
Key: 'e',
Modifier: gocui.ModNone,
Handler: gui.handleFileEdit,
2018-09-01 14:03:43 +02:00
Description: gui.Tr.SLocalize("editFile"),
2018-09-01 12:10:03 +02:00
}, {
2018-09-01 14:20:45 +02:00
ViewName: "files",
Key: 'o',
Modifier: gocui.ModNone,
Handler: gui.handleFileOpen,
2018-09-01 14:03:43 +02:00
Description: gui.Tr.SLocalize("openFile"),
2018-09-01 12:10:03 +02:00
}, {
2018-09-01 14:20:45 +02:00
ViewName: "files",
Key: 'i',
Modifier: gocui.ModNone,
Handler: gui.handleIgnoreFile,
2018-09-01 14:03:43 +02:00
Description: gui.Tr.SLocalize("ignoreFile"),
2018-09-01 12:10:03 +02:00
}, {
2018-09-01 14:20:45 +02:00
ViewName: "files",
Key: 'r',
Modifier: gocui.ModNone,
Handler: gui.handleRefreshFiles,
2018-09-01 14:03:43 +02:00
Description: gui.Tr.SLocalize("refreshFiles"),
2018-09-01 12:10:03 +02:00
}, {
2018-09-01 14:20:45 +02:00
ViewName: "files",
Key: 'S',
Modifier: gocui.ModNone,
Handler: gui.handleStashSave,
2018-09-01 14:03:43 +02:00
Description: gui.Tr.SLocalize("stashFiles"),
2018-09-01 12:10:03 +02:00
}, {
2018-09-01 14:20:45 +02:00
ViewName: "files",
Key: 'a',
Modifier: gocui.ModNone,
Handler: gui.handleStageAll,
2018-09-01 14:03:43 +02:00
Description: gui.Tr.SLocalize("toggleStagedAll"),
2018-09-01 12:10:03 +02:00
}, {
2018-09-01 14:20:45 +02:00
ViewName: "files",
Key: 't',
Modifier: gocui.ModNone,
Handler: gui.handleAddPatch,
2018-09-01 14:03:43 +02:00
Description: gui.Tr.SLocalize("addPatch"),
2018-09-01 12:10:03 +02:00
}, {
2018-09-01 14:20:45 +02:00
ViewName: "files",
Key: 'D',
Modifier: gocui.ModNone,
Handler: gui.handleResetAndClean,
2018-09-01 14:03:43 +02:00
Description: gui.Tr.SLocalize("resetHard"),
}, {
ViewName: "files",
Key: gocui.KeyEnter,
Modifier: gocui.ModNone,
Handler: gui.handleEnterFile,
Description: gui.Tr.SLocalize("StageLines"),
}, {
ViewName: "files",
Key: 'f',
Modifier: gocui.ModNone,
Handler: gui.handleGitFetch,
Description: gui.Tr.SLocalize("fetch"),
2018-12-07 09:52:31 +02:00
}, {
2018-12-08 07:54:54 +02:00
ViewName: "merging",
2018-09-01 12:10:03 +02:00
Key: gocui.KeyEsc,
Modifier: gocui.ModNone,
Handler: gui.handleEscapeMerge,
}, {
2018-12-08 07:54:54 +02:00
ViewName: "merging",
Key: gocui.KeySpace,
Modifier: gocui.ModNone,
Handler: gui.handlePickHunk,
Description: gui.Tr.SLocalize("PickHunk"),
2018-09-01 12:10:03 +02:00
}, {
2018-12-08 07:54:54 +02:00
ViewName: "merging",
Key: 'b',
Modifier: gocui.ModNone,
Handler: gui.handlePickBothHunks,
Description: gui.Tr.SLocalize("PickBothHunks"),
2018-09-01 12:10:03 +02:00
}, {
2018-12-08 07:54:54 +02:00
ViewName: "merging",
2018-09-01 12:10:03 +02:00
Key: gocui.KeyArrowLeft,
Modifier: gocui.ModNone,
Handler: gui.handleSelectPrevConflict,
}, {
2018-12-08 07:54:54 +02:00
ViewName: "merging",
2018-09-01 12:10:03 +02:00
Key: gocui.KeyArrowRight,
Modifier: gocui.ModNone,
Handler: gui.handleSelectNextConflict,
}, {
2018-12-08 07:54:54 +02:00
ViewName: "merging",
2018-09-01 12:10:03 +02:00
Key: gocui.KeyArrowUp,
Modifier: gocui.ModNone,
Handler: gui.handleSelectTop,
}, {
2018-12-08 07:54:54 +02:00
ViewName: "merging",
2018-09-01 12:10:03 +02:00
Key: gocui.KeyArrowDown,
Modifier: gocui.ModNone,
Handler: gui.handleSelectBottom,
}, {
2018-12-08 07:54:54 +02:00
ViewName: "merging",
2018-09-01 12:10:03 +02:00
Key: 'h',
Modifier: gocui.ModNone,
Handler: gui.handleSelectPrevConflict,
}, {
2018-12-08 07:54:54 +02:00
ViewName: "merging",
2018-09-01 12:10:03 +02:00
Key: 'l',
Modifier: gocui.ModNone,
Handler: gui.handleSelectNextConflict,
}, {
2018-12-08 07:54:54 +02:00
ViewName: "merging",
2018-09-01 12:10:03 +02:00
Key: 'k',
Modifier: gocui.ModNone,
Handler: gui.handleSelectTop,
}, {
2018-12-08 07:54:54 +02:00
ViewName: "merging",
2018-09-01 12:10:03 +02:00
Key: 'j',
Modifier: gocui.ModNone,
Handler: gui.handleSelectBottom,
}, {
2018-12-08 07:54:54 +02:00
ViewName: "merging",
Key: 'z',
Modifier: gocui.ModNone,
Handler: gui.handlePopFileSnapshot,
Description: gui.Tr.SLocalize("Undo"),
2018-09-01 12:10:03 +02:00
}, {
2018-09-03 18:44:56 +02:00
ViewName: "branches",
Key: gocui.KeySpace,
Modifier: gocui.ModNone,
Handler: gui.handleBranchPress,
Description: gui.Tr.SLocalize("checkout"),
}, {
ViewName: "branches",
Key: 'o',
Modifier: gocui.ModNone,
Handler: gui.handleCreatePullRequestPress,
Description: gui.Tr.SLocalize("createPullRequest"),
2018-09-01 12:10:03 +02:00
}, {
2018-09-01 14:20:45 +02:00
ViewName: "branches",
Key: 'c',
Modifier: gocui.ModNone,
Handler: gui.handleCheckoutByName,
2018-09-01 14:03:43 +02:00
Description: gui.Tr.SLocalize("checkoutByName"),
2018-09-01 12:10:03 +02:00
}, {
2018-09-01 14:20:45 +02:00
ViewName: "branches",
Key: 'F',
Modifier: gocui.ModNone,
Handler: gui.handleForceCheckout,
2018-09-01 14:03:43 +02:00
Description: gui.Tr.SLocalize("forceCheckout"),
2018-09-01 12:10:03 +02:00
}, {
2018-09-01 14:20:45 +02:00
ViewName: "branches",
Key: 'n',
Modifier: gocui.ModNone,
Handler: gui.handleNewBranch,
2018-09-01 14:03:43 +02:00
Description: gui.Tr.SLocalize("newBranch"),
2018-09-01 12:10:03 +02:00
}, {
2018-09-01 14:20:45 +02:00
ViewName: "branches",
Key: 'd',
Modifier: gocui.ModNone,
Handler: gui.handleDeleteBranch,
2018-09-01 14:03:43 +02:00
Description: gui.Tr.SLocalize("deleteBranch"),
2018-11-29 18:57:51 +02:00
}, {
ViewName: "branches",
Key: 'r',
Modifier: gocui.ModNone,
Handler: gui.handleRebase,
Description: gui.Tr.SLocalize("rebaseBranch"),
2018-09-01 12:10:03 +02:00
}, {
2018-09-01 14:20:45 +02:00
ViewName: "branches",
Key: 'm',
Modifier: gocui.ModNone,
Handler: gui.handleMerge,
2018-09-01 14:03:43 +02:00
Description: gui.Tr.SLocalize("mergeIntoCurrentBranch"),
2018-12-07 09:52:31 +02:00
}, {
ViewName: "branches",
Key: 'f',
Modifier: gocui.ModNone,
Handler: gui.handleFastForward,
Description: gui.Tr.SLocalize("FastForward"),
}, {
2018-09-01 14:20:45 +02:00
ViewName: "commits",
Key: 's',
Modifier: gocui.ModNone,
Handler: gui.handleCommitSquashDown,
2018-09-01 14:03:43 +02:00
Description: gui.Tr.SLocalize("squashDown"),
2018-09-01 12:10:03 +02:00
}, {
2018-09-01 14:20:45 +02:00
ViewName: "commits",
Key: 'r',
Modifier: gocui.ModNone,
Handler: gui.handleRenameCommit,
Description: gui.Tr.SLocalize("renameCommit"),
2018-09-03 18:01:07 +02:00
}, {
ViewName: "commits",
Key: 'R',
Modifier: gocui.ModNone,
Handler: gui.handleRenameCommitEditor,
Description: gui.Tr.SLocalize("renameCommitEditor"),
2018-09-01 12:10:03 +02:00
}, {
2018-09-01 14:20:45 +02:00
ViewName: "commits",
Key: 'g',
Modifier: gocui.ModNone,
Handler: gui.handleResetToCommit,
2018-09-01 14:03:43 +02:00
Description: gui.Tr.SLocalize("resetToThisCommit"),
2018-09-01 12:10:03 +02:00
}, {
2018-09-01 14:20:45 +02:00
ViewName: "commits",
Key: 'f',
Modifier: gocui.ModNone,
Handler: gui.handleCommitFixup,
2018-09-01 14:03:43 +02:00
Description: gui.Tr.SLocalize("fixupCommit"),
2018-12-07 09:52:31 +02:00
}, {
2018-09-01 14:20:45 +02:00
ViewName: "stash",
Key: gocui.KeySpace,
Modifier: gocui.ModNone,
Handler: gui.handleStashApply,
2018-09-01 14:03:43 +02:00
Description: gui.Tr.SLocalize("apply"),
2018-09-01 12:10:03 +02:00
}, {
2018-09-01 14:20:45 +02:00
ViewName: "stash",
Key: 'g',
Modifier: gocui.ModNone,
Handler: gui.handleStashPop,
2018-09-01 14:03:43 +02:00
Description: gui.Tr.SLocalize("pop"),
2018-09-01 12:10:03 +02:00
}, {
2018-09-01 14:20:45 +02:00
ViewName: "stash",
Key: 'd',
Modifier: gocui.ModNone,
Handler: gui.handleStashDrop,
2018-09-01 14:03:43 +02:00
Description: gui.Tr.SLocalize("drop"),
2018-12-07 09:52:31 +02:00
}, {
2018-09-01 12:10:03 +02:00
ViewName: "commitMessage",
Key: gocui.KeyEnter,
Modifier: gocui.ModNone,
Handler: gui.handleCommitConfirm,
}, {
ViewName: "commitMessage",
Key: gocui.KeyEsc,
Modifier: gocui.ModNone,
Handler: gui.handleCommitClose,
2018-10-20 17:37:55 +02:00
}, {
ViewName: "credentials",
2018-10-20 17:37:55 +02:00
Key: gocui.KeyEnter,
Modifier: gocui.ModNone,
Handler: gui.handleSubmitCredential,
2018-10-20 17:37:55 +02:00
}, {
ViewName: "credentials",
2018-10-20 17:37:55 +02:00
Key: gocui.KeyEsc,
Modifier: gocui.ModNone,
Handler: gui.handleCloseCredentialsView,
2018-09-01 12:10:03 +02:00
}, {
2018-09-05 11:12:11 +02:00
ViewName: "menu",
2018-09-01 12:10:03 +02:00
Key: gocui.KeyEsc,
Modifier: gocui.ModNone,
2018-09-05 11:12:11 +02:00
Handler: gui.handleMenuClose,
2018-09-01 12:10:03 +02:00
}, {
2018-09-05 11:12:11 +02:00
ViewName: "menu",
2018-09-01 12:10:03 +02:00
Key: 'q',
Modifier: gocui.ModNone,
2018-09-05 11:12:11 +02:00
Handler: gui.handleMenuClose,
2018-12-07 09:52:31 +02:00
}, {
2018-12-05 10:33:46 +02:00
ViewName: "staging",
Key: gocui.KeyEsc,
Modifier: gocui.ModNone,
Handler: gui.handleStagingEscape,
Description: gui.Tr.SLocalize("EscapeStaging"),
}, {
ViewName: "staging",
Key: gocui.KeyArrowUp,
Modifier: gocui.ModNone,
2018-12-05 10:33:46 +02:00
Handler: gui.handleStagingPrevLine,
}, {
ViewName: "staging",
Key: gocui.KeyArrowDown,
Modifier: gocui.ModNone,
2018-12-05 10:33:46 +02:00
Handler: gui.handleStagingNextLine,
}, {
ViewName: "staging",
2018-12-05 10:33:46 +02:00
Key: 'k',
Modifier: gocui.ModNone,
Handler: gui.handleStagingPrevLine,
}, {
ViewName: "staging",
Key: 'j',
Modifier: gocui.ModNone,
Handler: gui.handleStagingNextLine,
}, {
ViewName: "staging",
Key: gocui.KeyArrowLeft,
Modifier: gocui.ModNone,
Handler: gui.handleStagingPrevHunk,
}, {
ViewName: "staging",
Key: gocui.KeyArrowRight,
Modifier: gocui.ModNone,
Handler: gui.handleStagingNextHunk,
}, {
ViewName: "staging",
Key: 'h',
Modifier: gocui.ModNone,
Handler: gui.handleStagingPrevHunk,
}, {
ViewName: "staging",
Key: 'l',
Modifier: gocui.ModNone,
2018-12-05 10:33:46 +02:00
Handler: gui.handleStagingNextHunk,
}, {
ViewName: "staging",
Key: gocui.KeySpace,
Modifier: gocui.ModNone,
Handler: gui.handleStageLine,
Description: gui.Tr.SLocalize("StageLine"),
}, {
ViewName: "staging",
Key: 'a',
Modifier: gocui.ModNone,
Handler: gui.handleStageHunk,
Description: gui.Tr.SLocalize("StageHunk"),
2018-09-01 12:10:03 +02:00
},
2018-08-08 11:18:41 +02:00
}
for _, viewName := range []string{"status", "branches", "files", "commits", "stash", "menu"} {
bindings = append(bindings, []*Binding{
2018-08-13 13:16:21 +02:00
{ViewName: viewName, Key: gocui.KeyTab, Modifier: gocui.ModNone, Handler: gui.nextView},
{ViewName: viewName, Key: gocui.KeyArrowLeft, Modifier: gocui.ModNone, Handler: gui.previousView},
{ViewName: viewName, Key: gocui.KeyArrowRight, Modifier: gocui.ModNone, Handler: gui.nextView},
{ViewName: viewName, Key: 'h', Modifier: gocui.ModNone, Handler: gui.previousView},
{ViewName: viewName, Key: 'l', Modifier: gocui.ModNone, Handler: gui.nextView},
}...)
}
2018-12-07 09:52:31 +02:00
listPanelMap := map[string]struct {
prevLine func(*gocui.Gui, *gocui.View) error
nextLine func(*gocui.Gui, *gocui.View) error
}{
"menu": {prevLine: gui.handleMenuPrevLine, nextLine: gui.handleMenuNextLine},
"files": {prevLine: gui.handleFilesPrevLine, nextLine: gui.handleFilesNextLine},
"branches": {prevLine: gui.handleBranchesPrevLine, nextLine: gui.handleBranchesNextLine},
"commits": {prevLine: gui.handleCommitsPrevLine, nextLine: gui.handleCommitsNextLine},
"stash": {prevLine: gui.handleStashPrevLine, nextLine: gui.handleStashNextLine},
}
for viewName, functions := range listPanelMap {
bindings = append(bindings, []*Binding{
{ViewName: viewName, Key: 'k', Modifier: gocui.ModNone, Handler: functions.prevLine},
{ViewName: viewName, Key: gocui.KeyArrowUp, Modifier: gocui.ModNone, Handler: functions.prevLine},
{ViewName: viewName, Key: 'j', Modifier: gocui.ModNone, Handler: functions.nextLine},
{ViewName: viewName, Key: gocui.KeyArrowDown, Modifier: gocui.ModNone, Handler: functions.nextLine},
}...)
}
2018-08-28 20:07:13 +02:00
return bindings
}
func (gui *Gui) keybindings(g *gocui.Gui) error {
bindings := gui.GetKeybindings()
2018-08-28 20:07:13 +02:00
2018-08-08 11:18:41 +02:00
for _, binding := range bindings {
if err := g.SetKeybinding(binding.ViewName, binding.Key, binding.Modifier, binding.Handler); err != nil {
return err
}
}
return nil
}