1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-08-06 11:02:41 +03:00

copy selected text to clipboard

This commit is contained in:
Hrishikesh Hiraskar
2021-10-02 13:20:26 +05:30
committed by Jesse Duffield
parent 3621084096
commit b6454755ca
5 changed files with 48 additions and 0 deletions

View File

@@ -194,6 +194,22 @@ func (p *PatchParser) Render(firstLineIndex int, lastLineIndex int, incLineIndic
return result 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 // 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 // note this will actually include the current index if it is stageable
func (p *PatchParser) GetNextStageableLineIndex(currentIndex int) int { func (p *PatchParser) GetNextStageableLineIndex(currentIndex int) int {

View File

@@ -1303,6 +1303,13 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
Modifier: gocui.ModNone, Modifier: gocui.ModNone,
Handler: gui.handleSelectNextHunk, 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", ViewName: "main",
Contexts: []string{string(MAIN_STAGING_CONTEXT_KEY)}, Contexts: []string{string(MAIN_STAGING_CONTEXT_KEY)},

View File

@@ -180,6 +180,11 @@ func (s *State) RenderForLineIndices(includedLineIndices []int) string {
return s.patchParser.Render(firstLineIdx, lastLineIdx, includedLineIndices) 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() { func (s *State) SelectBottom() {
s.SetLineSelectMode() s.SetLineSelectMode()
s.SelectLine(len(s.patchParser.PatchLines) - 1) s.SelectLine(len(s.patchParser.PatchLines) - 1)

View File

@@ -2,11 +2,13 @@ package gui
import ( import (
"fmt" "fmt"
"strings"
"github.com/go-errors/errors" "github.com/go-errors/errors"
"github.com/jesseduffield/gocui" "github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands/patch" "github.com/jesseduffield/lazygit/pkg/commands/patch"
"github.com/jesseduffield/lazygit/pkg/gui/lbl" "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'. // 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 { func (gui *Gui) refreshAndFocusLblPanel(state *LblPanelState) error {
if err := gui.refreshMainViewForLineByLine(state); err != nil { if err := gui.refreshMainViewForLineByLine(state); err != nil {
return err return err

View File

@@ -479,6 +479,7 @@ type Spans struct {
GitFlowFinish string GitFlowFinish string
GitFlowStart string GitFlowStart string
CopyToClipboard string CopyToClipboard string
CopySelectedTextToClipboard string
RemovePatchFromCommit string RemovePatchFromCommit string
MovePatchToSelectedCommit string MovePatchToSelectedCommit string
MovePatchIntoIndex string MovePatchIntoIndex string
@@ -999,6 +1000,7 @@ func englishTranslationSet() TranslationSet {
GitFlowFinish: "Git flow finish", GitFlowFinish: "Git flow finish",
GitFlowStart: "Git Flow start", GitFlowStart: "Git Flow start",
CopyToClipboard: "Copy to clipboard", CopyToClipboard: "Copy to clipboard",
CopySelectedTextToClipboard: "Copy Selected Text to clipboard",
RemovePatchFromCommit: "Remove patch from commit", RemovePatchFromCommit: "Remove patch from commit",
MovePatchToSelectedCommit: "Move patch to selected commit", MovePatchToSelectedCommit: "Move patch to selected commit",
MovePatchIntoIndex: "Move patch into index", MovePatchIntoIndex: "Move patch into index",