Skip to content

Commit 3fa5a8e

Browse files
kyu08stefanhaller
authored andcommitted
Add "CopyToClipboard" command to ConfirmationController
1 parent 0573529 commit 3fa5a8e

File tree

5 files changed

+75
-0
lines changed

5 files changed

+75
-0
lines changed

pkg/gui/controllers/confirmation_controller.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ func (self *ConfirmationController) GetKeybindings(opts types.KeybindingsOpts) [
4747
return nil
4848
},
4949
},
50+
{
51+
Key: opts.GetKey(opts.Config.Universal.CopyToClipboard),
52+
Handler: self.handleCopyToClipboard,
53+
Description: self.c.Tr.CopyToClipboardMenu,
54+
DisplayOnScreen: true,
55+
GetDisabledReason: self.copyToClipboardEnabled,
56+
},
5057
}
5158

5259
return bindings
@@ -93,3 +100,23 @@ func (self *ConfirmationController) switchToSuggestions() {
93100
self.c.Views().Suggestions.Subtitle = subtitle
94101
self.c.Context().Replace(self.c.Contexts().Suggestions)
95102
}
103+
104+
func (self *ConfirmationController) handleCopyToClipboard() error {
105+
confirmationView := self.c.Views().Confirmation
106+
text := confirmationView.Buffer()
107+
if err := self.c.OS().CopyToClipboard(text); err != nil {
108+
return err
109+
}
110+
111+
self.c.Toast(self.c.Tr.MessageCopiedToClipboard)
112+
return nil
113+
}
114+
115+
func (self *ConfirmationController) copyToClipboardEnabled() *types.DisabledReason {
116+
if self.c.Views().Confirmation.Editable {
117+
// The empty text is intentional. We don't want to get a toast when invoking this, we only
118+
// want to prevent it from showing up in the options bar.
119+
return &types.DisabledReason{Text: ""}
120+
}
121+
return nil
122+
}

pkg/i18n/english.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,7 @@ type TranslationSet struct {
723723
CommitHasNoTags string
724724
CommitHasNoMessageBody string
725725
PatchCopiedToClipboard string
726+
MessageCopiedToClipboard string
726727
CopiedToClipboard string
727728
ErrCannotEditDirectory string
728729
ErrCannotCopyContentOfDirectory string
@@ -1800,6 +1801,7 @@ func EnglishTranslationSet() *TranslationSet {
18001801
CommitHasNoTags: "Commit has no tags",
18011802
CommitHasNoMessageBody: "Commit has no message body",
18021803
PatchCopiedToClipboard: "Patch copied to clipboard",
1804+
MessageCopiedToClipboard: "Message copied to clipboard",
18031805
CopiedToClipboard: "copied to clipboard",
18041806
ErrCannotEditDirectory: "Cannot edit directories: you can only edit individual files",
18051807
ErrCannotCopyContentOfDirectory: "Cannot copy content of directories: you can only copy content of individual files",

pkg/integration/components/popup.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ func (self *Popup) Alert() *AlertDriver {
3636
return &AlertDriver{t: self.t}
3737
}
3838

39+
func (self *AlertDriver) Tap(f func()) *AlertDriver {
40+
self.getViewDriver().Tap(f)
41+
return self
42+
}
43+
3944
func (self *Popup) inAlert() {
4045
// basically the same thing as a confirmation popup with the current implementation
4146
self.t.assertWithRetries(func() (bool, string) {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package misc
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/config"
5+
. "github.com/jesseduffield/lazygit/pkg/integration/components"
6+
)
7+
8+
var CopyConfirmationMessageToClipboard = NewIntegrationTest(NewIntegrationTestArgs{
9+
Description: "Copy the text of a confirmation popup to the clipboard",
10+
ExtraCmdArgs: []string{},
11+
Skip: false,
12+
SetupConfig: func(config *config.AppConfig) {
13+
config.GetUserConfig().OS.CopyToClipboardCmd = "printf '%s' {{text}} > clipboard"
14+
},
15+
16+
SetupRepo: func(shell *Shell) {
17+
shell.EmptyCommit("commit")
18+
},
19+
20+
Run: func(t *TestDriver, keys config.KeybindingConfig) {
21+
t.Views().Commits().
22+
Focus().
23+
Lines(
24+
Contains("commit").IsSelected(),
25+
).
26+
Press(keys.Universal.Remove)
27+
28+
t.ExpectPopup().Alert().
29+
Title(Equals("Drop commit")).
30+
Content(Equals("Are you sure you want to drop the selected commit(s)?")).
31+
Tap(func() {
32+
t.GlobalPress(keys.Universal.CopyToClipboard)
33+
t.ExpectToast(Equals("Message copied to clipboard"))
34+
}).
35+
Confirm()
36+
37+
t.FileSystem().FileContent("clipboard",
38+
Equals("Are you sure you want to drop the selected commit(s)?"))
39+
},
40+
})

pkg/integration/tests/test_list.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ var tests = []*components.IntegrationTest{
299299
interactive_rebase.SwapWithConflict,
300300
interactive_rebase.ViewFilesOfTodoEntries,
301301
misc.ConfirmOnQuit,
302+
misc.CopyConfirmationMessageToClipboard,
302303
misc.CopyToClipboard,
303304
misc.DisabledKeybindings,
304305
misc.InitialOpen,

0 commit comments

Comments
 (0)