mirror of
https://github.com/EnumeratedDev/typer.git
synced 2026-09-16 05:36:10 +00:00
Improve copy and pasting
This commit is contained in:
@@ -164,6 +164,70 @@ func (buffer *Buffer) GetSelectedText() string {
|
||||
}
|
||||
}
|
||||
|
||||
func (buffer *Buffer) CopyText() (string, int) {
|
||||
if buffer.Selection == nil {
|
||||
// Copy line
|
||||
copiedText := ""
|
||||
|
||||
// Add current letter to copied text
|
||||
if buffer.CursorPos < len(buffer.Contents) {
|
||||
copiedText = string(buffer.Contents[buffer.CursorPos])
|
||||
}
|
||||
|
||||
// Find end of line
|
||||
for i := buffer.CursorPos + 1; i < len(buffer.Contents); i++ {
|
||||
currentLetter := buffer.Contents[i]
|
||||
|
||||
copiedText += string(currentLetter)
|
||||
if currentLetter == '\n' {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// Find start of line
|
||||
for i := buffer.CursorPos - 1; i >= 0; i-- {
|
||||
currentLetter := buffer.Contents[i]
|
||||
if currentLetter != '\n' {
|
||||
copiedText = string(currentLetter) + copiedText
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return copiedText, 0
|
||||
} else {
|
||||
// Copy selection
|
||||
return buffer.GetSelectedText(), 1
|
||||
}
|
||||
}
|
||||
|
||||
func (buffer *Buffer) PasteText(window *Window, text string) {
|
||||
str := buffer.Contents
|
||||
|
||||
// Remove selected text
|
||||
if buffer.Selection != nil {
|
||||
edge1, edge2 := buffer.GetSelectionEdges()
|
||||
if edge2 == len(buffer.Contents) {
|
||||
edge2 = len(buffer.Contents) - 1
|
||||
}
|
||||
|
||||
str = str[:edge1] + str[edge2+1:]
|
||||
buffer.Contents = str
|
||||
window.SetCursorPos(edge1)
|
||||
buffer.Selection = nil
|
||||
}
|
||||
|
||||
index := buffer.CursorPos
|
||||
|
||||
if index == len(str) {
|
||||
str += text
|
||||
} else {
|
||||
str = str[:index] + text + str[index:]
|
||||
}
|
||||
buffer.Contents = str
|
||||
window.SetCursorPos(buffer.CursorPos + len(text))
|
||||
}
|
||||
|
||||
func GetOpenFileBuffer(filename string) *Buffer {
|
||||
// Replace tilde with home directory
|
||||
if filename != "~" && strings.HasPrefix(filename, "~/") {
|
||||
|
||||
Reference in New Issue
Block a user