1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-10 04:07:18 +02:00

centralise code for copying to clipboard

This commit is contained in:
Jesse Duffield 2020-08-22 12:07:03 +10:00
parent 442f6cd854
commit 438abd6003
5 changed files with 36 additions and 20 deletions

View File

@ -460,15 +460,6 @@ func (gui *Gui) currentBranch() *commands.Branch {
return gui.State.Branches[0] return gui.State.Branches[0]
} }
func (gui *Gui) handleClipboardCopyBranch(g *gocui.Gui, v *gocui.View) error {
branch := gui.getSelectedBranch()
if branch == nil {
return nil
}
return gui.OSCommand.CopyToClipboard(branch.Name)
}
func (gui *Gui) handleNewBranchOffCurrentItem() error { func (gui *Gui) handleNewBranchOffCurrentItem() error {
context := gui.currentSideContext() context := gui.currentSideContext()

View File

@ -571,12 +571,3 @@ func (gui *Gui) handleGotoBottomForCommitsPanel(g *gocui.Gui, v *gocui.View) err
return nil return nil
} }
func (gui *Gui) handleClipboardCopyCommit(g *gocui.Gui, v *gocui.View) error {
commit := gui.getSelectedLocalCommit()
if commit == nil {
return nil
}
return gui.OSCommand.CopyToClipboard(commit.Sha)
}

View File

@ -693,3 +693,12 @@ func (gui *Gui) getCurrentSideView() *gocui.View {
return view return view
} }
func (gui *Gui) getSideContextSelectedItem() ListItem {
currentSideContext := gui.currentSideContext()
if currentSideContext == nil {
return nil
}
return currentSideContext.GetSelectedItem()
}

View File

@ -178,3 +178,14 @@ func (gui *Gui) fetch(canPromptForCredentials bool) (err error) {
return err return err
} }
func (gui *Gui) handleCopySelectedSideContextItemToClipboard() error {
// important to note that this assumes we've selected an item in a side context
item := gui.getSideContextSelectedItem()
if item == nil {
return nil
}
return gui.OSCommand.CopyToClipboard(item.ID())
}

View File

@ -552,7 +552,7 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
ViewName: "branches", ViewName: "branches",
Contexts: []string{LOCAL_BRANCHES_CONTEXT_KEY}, Contexts: []string{LOCAL_BRANCHES_CONTEXT_KEY},
Key: gui.getKey("universal.copyToClipboard"), Key: gui.getKey("universal.copyToClipboard"),
Handler: gui.handleClipboardCopyBranch, Handler: gui.wrappedHandler(gui.handleCopySelectedSideContextItemToClipboard),
Description: gui.Tr.SLocalize("copyBranchNameToClipboard"), Description: gui.Tr.SLocalize("copyBranchNameToClipboard"),
}, },
{ {
@ -765,7 +765,7 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
ViewName: "commits", ViewName: "commits",
Contexts: []string{BRANCH_COMMITS_CONTEXT_KEY}, Contexts: []string{BRANCH_COMMITS_CONTEXT_KEY},
Key: gui.getKey("universal.copyToClipboard"), Key: gui.getKey("universal.copyToClipboard"),
Handler: gui.handleClipboardCopyCommit, Handler: gui.wrappedHandler(gui.handleCopySelectedSideContextItemToClipboard),
Description: gui.Tr.SLocalize("copyCommitShaToClipboard"), Description: gui.Tr.SLocalize("copyCommitShaToClipboard"),
}, },
{ {
@ -860,6 +860,13 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
Handler: gui.wrappedHandler(gui.exitCherryPickingMode), Handler: gui.wrappedHandler(gui.exitCherryPickingMode),
Description: gui.Tr.SLocalize("resetCherryPick"), Description: gui.Tr.SLocalize("resetCherryPick"),
}, },
{
ViewName: "commits",
Contexts: []string{REFLOG_COMMITS_CONTEXT_KEY},
Key: gui.getKey("universal.copyToClipboard"),
Handler: gui.wrappedHandler(gui.handleCopySelectedSideContextItemToClipboard),
Description: gui.Tr.SLocalize("copyCommitShaToClipboard"),
},
{ {
ViewName: "branches", ViewName: "branches",
Contexts: []string{SUB_COMMITS_CONTEXT_KEY}, Contexts: []string{SUB_COMMITS_CONTEXT_KEY},
@ -909,6 +916,13 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
Handler: gui.wrappedHandler(gui.exitCherryPickingMode), Handler: gui.wrappedHandler(gui.exitCherryPickingMode),
Description: gui.Tr.SLocalize("resetCherryPick"), Description: gui.Tr.SLocalize("resetCherryPick"),
}, },
{
ViewName: "branches",
Contexts: []string{SUB_COMMITS_CONTEXT_KEY},
Key: gui.getKey("universal.copyToClipboard"),
Handler: gui.wrappedHandler(gui.handleCopySelectedSideContextItemToClipboard),
Description: gui.Tr.SLocalize("copyCommitShaToClipboard"),
},
{ {
ViewName: "stash", ViewName: "stash",
Key: gui.getKey("universal.goInto"), Key: gui.getKey("universal.goInto"),