Fix issue: sometimes it cuts selected text when copying to clipboard (#186)

This commit is contained in:
nikitar020 2019-01-30 15:43:08 +00:00 committed by Liam Galvin
parent 23797d50f3
commit 9190afef8a
2 changed files with 16 additions and 8 deletions

View File

@ -6,6 +6,7 @@ import (
"io/ioutil" "io/ioutil"
"net/url" "net/url"
"os" "os"
"strings"
) )
type Buffer struct { type Buffer struct {
@ -170,8 +171,6 @@ func (buffer *Buffer) GetSelectedText() string {
return "" return ""
} }
text := ""
var x1, x2, y1, y2 int 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) { 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 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++ { for row := y1; row <= y2; row++ {
if row >= len(buffer.lines) { if row >= len(buffer.lines) {
@ -199,7 +201,7 @@ func (buffer *Buffer) GetSelectedText() string {
if row == y1 { if row == y1 {
minX = x1 minX = x1
} else if !line.wrapped { } else if !line.wrapped {
text += "\n" builder.WriteString("\n")
} }
if row == y2 { if row == y2 {
maxX = x2 maxX = x2
@ -209,13 +211,15 @@ func (buffer *Buffer) GetSelectedText() string {
if col >= len(line.cells) { if col >= len(line.cells) {
break break
} }
cell := line.cells[col] r := line.cells[col].Rune()
text += string(cell.Rune()) if r == 0x00 {
r = ' '
}
builder.WriteRune(r)
} }
} }
return text return builder.String()
} }
func (buffer *Buffer) StartSelection(col uint16, viewRow uint16) { func (buffer *Buffer) StartSelection(col uint16, viewRow uint16) {

View File

@ -18,7 +18,11 @@ var actionMap = map[config.UserAction]func(gui *GUI){
} }
func actionCopy(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) { func actionPaste(gui *GUI) {