summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHrishikesh Hiraskar <hrishi@enterpret.com>2021-10-02 13:20:26 +0530
committerJesse Duffield <jessedduffield@gmail.com>2021-10-16 21:18:43 +1100
commitb6454755caf6b90c935f986558f4f9ad085b1837 (patch)
tree65704a5fc6436390cf8dbe54428936edb0d9c6ff
parent36210840962bb448208b6cc628319f994c392075 (diff)
copy selected text to clipboard
-rw-r--r--pkg/commands/patch/patch_parser.go16
-rw-r--r--pkg/gui/keybindings.go7
-rw-r--r--pkg/gui/lbl/state.go5
-rw-r--r--pkg/gui/line_by_line_panel.go18
-rw-r--r--pkg/i18n/english.go2
5 files changed, 48 insertions, 0 deletions
diff --git a/pkg/commands/patch/patch_parser.go b/pkg/commands/patch/patch_parser.go
index 4fb8eb68b..4fed00325 100644
--- a/pkg/commands/patch/patch_parser.go
+++ b/pkg/commands/patch/patch_parser.go
@@ -194,6 +194,22 @@ func (p *PatchParser) Render(firstLineIndex int, lastLineIndex int, incLineIndic
return result
}
+// RenderLines returns the coloured string of diff part from firstLineIndex to
+// lastLineIndex
+func (p *PatchParser) RenderLines(firstLineIndex, lastLineIndex int) string {
+ renderedLines := make([]string, lastLineIndex-firstLineIndex+1)
+ for index := firstLineIndex; index <= lastLineIndex; index++ {
+ renderedLines[index-firstLineIndex] = p.PatchLines[index].render(
+ false, false,
+ )
+ }
+ result := strings.Join(renderedLines, "\n")
+ if strings.TrimSpace(utils.Decolorise(result)) == "" {
+ return ""
+ }
+ return result
+}
+
// GetNextStageableLineIndex takes a line index and returns the line index of the next stageable line
// note this will actually include the current index if it is stageable
func (p *PatchParser) GetNextStageableLineIndex(currentIndex int) int {
diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go
index 0280d6fd1..44fee4fce 100644
--- a/pkg/gui/keybindings.go
+++ b/pkg/gui/keybindings.go
@@ -1304,6 +1304,13 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
Handler: gui.handleSelectNextHunk,
},
{
+ ViewName: "main",
+ Contexts: []string{string(MAIN_PATCH_BUILDING_CONTEXT_KEY), string(MAIN_STAGING_CONTEXT_KEY)},
+ Key: gui.getKey(config.Universal.CopyToClipboard),
+ Modifier: gocui.ModNone,
+ Handler: gui.copySelectedToClipboard,
+ },
+ {
ViewName: "main",
Contexts: []string{string(MAIN_STAGING_CONTEXT_KEY)},
Key: gui.getKey(config.Universal.Edit),
diff --git a/pkg/gui/lbl/state.go b/pkg/gui/lbl/state.go
index 8ae828923..e8014fc1a 100644
--- a/pkg/gui/lbl/state.go
+++ b/pkg/gui/lbl/state.go
@@ -180,6 +180,11 @@ func (s *State) RenderForLineIndices(includedLineIndices []int) string {
return s.patchParser.Render(firstLineIdx, lastLineIdx, includedLineIndices)
}
+func (s *State) RenderSelected() string {
+ firstLineIdx, lastLineIdx := s.SelectedRange()
+ return s.patchParser.RenderLines(firstLineIdx, lastLineIdx)
+}
+
func (s *State) SelectBottom() {
s.SetLineSelectMode()
s.SelectLine(len(s.patchParser.PatchLines) - 1)
diff --git a/pkg/gui/line_by_line_panel.go b/pkg/gui/line_by_line_panel.go
index 8121b6ed3..b366352b0 100644
--- a/pkg/gui/line_by_line_panel.go
+++ b/pkg/gui/line_by_line_panel.go
@@ -2,11 +2,13 @@ package gui
import (
"fmt"
+ "strings"
"github.com/go-errors/errors"
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands/patch"
"github.com/jesseduffield/lazygit/pkg/gui/lbl"
+ "github.com/jesseduffield/lazygit/pkg/utils"
)
// Currently there are two 'pseudo-panels' that make use of this 'pseudo-panel'.
@@ -86,6 +88,22 @@ func (gui *Gui) handleSelectNextHunk() error {
})
}
+func (gui *Gui) copySelectedToClipboard() error {
+ return gui.withLBLActiveCheck(func(state *LblPanelState) error {
+
+ colorSelected := state.RenderSelected()
+ selected := strings.TrimSpace(utils.Decolorise(colorSelected))
+
+ if err := gui.OSCommand.WithSpan(
+ gui.Tr.Spans.CopySelectedTextToClipboard,
+ ).CopyToClipboard(selected); err != nil {
+ return gui.surfaceError(err)
+ }
+
+ return nil
+ })
+}
+
func (gui *Gui) refreshAndFocusLblPanel(state *LblPanelState) error {
if err := gui.refreshMainViewForLineByLine(state); err != nil {
return err
diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go
index 8e1ac9647..eb172ccba 100644
--- a/pkg/i18n/english.go
+++ b/pkg/i18n/english.go
@@ -479,6 +479,7 @@ type Spans struct {
GitFlowFinish string
GitFlowStart string
CopyToClipboard string
+ CopySelectedTextToClipboard string
RemovePatchFromCommit string
MovePatchToSelectedCommit string
MovePatchIntoIndex string
@@ -999,6 +1000,7 @@ func englishTranslationSet() TranslationSet {
GitFlowFinish: "Git flow finish",
GitFlowStart: "Git Flow start",
CopyToClipboard: "Copy to clipboard",
+ CopySelectedTextToClipboard: "Copy Selected Text to clipboard",
RemovePatchFromCommit: "Remove patch from commit",
MovePatchToSelectedCommit: "Move patch to selected commit",
MovePatchIntoIndex: "Move patch into index",