diff --git a/pkg/commands/oscommands/os.go b/pkg/commands/oscommands/os.go index 5a35b9278..b146d9d8e 100644 --- a/pkg/commands/oscommands/os.go +++ b/pkg/commands/oscommands/os.go @@ -546,7 +546,9 @@ func RunLineOutputCmd(cmd *exec.Cmd, onLine func(line string) (bool, error)) err } func (c *OSCommand) CopyToClipboard(str string) error { - c.LogCommand(fmt.Sprintf("Copying '%s' to clipboard", utils.TruncateWithEllipsis(str, 40)), false) + escaped := strings.Replace(str, "\n", "\\n", -1) + truncated := utils.TruncateWithEllipsis(escaped, 40) + c.LogCommand(fmt.Sprintf("Copying '%s' to clipboard", truncated), false) return clipboard.WriteAll(str) } diff --git a/pkg/commands/patch/patch_parser.go b/pkg/commands/patch/patch_parser.go index 4fed00325..1fefe0748 100644 --- a/pkg/commands/patch/patch_parser.go +++ b/pkg/commands/patch/patch_parser.go @@ -194,20 +194,17 @@ func (p *PatchParser) Render(firstLineIndex int, lastLineIndex int, incLineIndic return result } -// RenderLines returns the coloured string of diff part from firstLineIndex to +// PlainRenderLines returns the non-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, - ) +func (p *PatchParser) PlainRenderLines(firstLineIndex, lastLineIndex int) string { + linesToCopy := p.PatchLines[firstLineIndex : lastLineIndex+1] + + renderedLines := make([]string, len(linesToCopy)) + for index, line := range linesToCopy { + renderedLines[index] = line.Content } - result := strings.Join(renderedLines, "\n") - if strings.TrimSpace(utils.Decolorise(result)) == "" { - return "" - } - return result + + return strings.Join(renderedLines, "\n") } // GetNextStageableLineIndex takes a line index and returns the line index of the next stageable line diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index 44fee4fce..844a1dc8a 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -1304,11 +1304,12 @@ 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_PATCH_BUILDING_CONTEXT_KEY), string(MAIN_STAGING_CONTEXT_KEY)}, + Key: gui.getKey(config.Universal.CopyToClipboard), + Modifier: gocui.ModNone, + Handler: gui.copySelectedToClipboard, + Description: gui.Tr.LcCopySelectedTexToClipboard, }, { ViewName: "main", diff --git a/pkg/gui/lbl/state.go b/pkg/gui/lbl/state.go index e8014fc1a..8ff7d7e88 100644 --- a/pkg/gui/lbl/state.go +++ b/pkg/gui/lbl/state.go @@ -180,9 +180,9 @@ func (s *State) RenderForLineIndices(includedLineIndices []int) string { return s.patchParser.Render(firstLineIdx, lastLineIdx, includedLineIndices) } -func (s *State) RenderSelected() string { +func (s *State) PlainRenderSelected() string { firstLineIdx, lastLineIdx := s.SelectedRange() - return s.patchParser.RenderLines(firstLineIdx, lastLineIdx) + return s.patchParser.PlainRenderLines(firstLineIdx, lastLineIdx) } func (s *State) SelectBottom() { diff --git a/pkg/gui/line_by_line_panel.go b/pkg/gui/line_by_line_panel.go index b366352b0..8c793375b 100644 --- a/pkg/gui/line_by_line_panel.go +++ b/pkg/gui/line_by_line_panel.go @@ -2,13 +2,11 @@ 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'. @@ -90,9 +88,7 @@ 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)) + selected := state.PlainRenderSelected() if err := gui.OSCommand.WithSpan( gui.Tr.Spans.CopySelectedTextToClipboard, diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index eb172ccba..1156cfee7 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -361,6 +361,7 @@ type TranslationSet struct { LcCopyFileNameToClipboard string LcCopyCommitFileNameToClipboard string LcCommitPrefixPatternError string + LcCopySelectedTexToClipboard string NoFilesStagedTitle string NoFilesStagedPrompt string BranchNotFoundTitle string @@ -883,6 +884,7 @@ func englishTranslationSet() TranslationSet { LcCopyBranchNameToClipboard: "copy branch name to clipboard", LcCopyFileNameToClipboard: "copy the file name to the clipboard", LcCopyCommitFileNameToClipboard: "copy the committed file name to the clipboard", + LcCopySelectedTexToClipboard: "copy the selected text to the clipboard", LcCommitPrefixPatternError: "Error in commitPrefix pattern", NoFilesStagedTitle: "No files staged", NoFilesStagedPrompt: "You have not staged any files. Commit all files?", @@ -1000,7 +1002,7 @@ func englishTranslationSet() TranslationSet { GitFlowFinish: "Git flow finish", GitFlowStart: "Git Flow start", CopyToClipboard: "Copy to clipboard", - CopySelectedTextToClipboard: "Copy Selected Text to clipboard", + CopySelectedTextToClipboard: "Copy selected text to clipboard", RemovePatchFromCommit: "Remove patch from commit", MovePatchToSelectedCommit: "Move patch to selected commit", MovePatchIntoIndex: "Move patch into index",