From 9190afef8a2f02856d006a00cc6171a9881de9d7 Mon Sep 17 00:00:00 2001 From: nikitar020 <42252263+nikitar020@users.noreply.github.com> Date: Wed, 30 Jan 2019 15:43:08 +0000 Subject: [PATCH] Fix issue: sometimes it cuts selected text when copying to clipboard (#186) --- buffer/buffer.go | 18 +++++++++++------- gui/actions.go | 6 +++++- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/buffer/buffer.go b/buffer/buffer.go index 4130386..af6c65c 100644 --- a/buffer/buffer.go +++ b/buffer/buffer.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "net/url" "os" + "strings" ) type Buffer struct { @@ -170,8 +171,6 @@ func (buffer *Buffer) GetSelectedText() string { return "" } - text := "" - var x1, x2, y1, y2 int if buffer.selectionStart.Line > buffer.selectionEnd.Line || (buffer.selectionStart.Line == buffer.selectionEnd.Line && buffer.selectionStart.Col > buffer.selectionEnd.Col) { @@ -186,6 +185,9 @@ func (buffer *Buffer) GetSelectedText() string { x2 = buffer.selectionEnd.Col } + var builder strings.Builder + builder.Grow( int(buffer.terminalState.viewWidth) * (y2 - y1 + 1)) // reserve space to minimize allocations + for row := y1; row <= y2; row++ { if row >= len(buffer.lines) { @@ -199,7 +201,7 @@ func (buffer *Buffer) GetSelectedText() string { if row == y1 { minX = x1 } else if !line.wrapped { - text += "\n" + builder.WriteString("\n") } if row == y2 { maxX = x2 @@ -209,13 +211,15 @@ func (buffer *Buffer) GetSelectedText() string { if col >= len(line.cells) { break } - cell := line.cells[col] - text += string(cell.Rune()) + r := line.cells[col].Rune() + if r == 0x00 { + r = ' ' + } + builder.WriteRune(r) } - } - return text + return builder.String() } func (buffer *Buffer) StartSelection(col uint16, viewRow uint16) { diff --git a/gui/actions.go b/gui/actions.go index d250f9a..64c030f 100644 --- a/gui/actions.go +++ b/gui/actions.go @@ -18,7 +18,11 @@ var actionMap = map[config.UserAction]func(gui *GUI){ } func actionCopy(gui *GUI) { - gui.window.SetClipboardString(gui.terminal.ActiveBuffer().GetSelectedText()) + selectedText := gui.terminal.ActiveBuffer().GetSelectedText() + + if selectedText != "" { + gui.window.SetClipboardString(selectedText) + } } func actionPaste(gui *GUI) {