mirror of
https://github.com/EnumeratedDev/Typer.git
synced 2025-07-01 23:58:22 +00:00
Compare commits
2 Commits
f30eb3c46f
...
e25916228c
Author | SHA1 | Date | |
---|---|---|---|
e25916228c | |||
de19696b35 |
@ -98,6 +98,16 @@ func (buffer *Buffer) Load() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Replace tilde with home directory
|
||||
if strings.HasPrefix(buffer.filename, "~/") {
|
||||
homedir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
buffer.filename = filepath.Join(homedir, buffer.filename[2:])
|
||||
}
|
||||
|
||||
content, err := os.ReadFile(buffer.filename)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -113,6 +123,16 @@ func (buffer *Buffer) Save() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Replace tilde with home directory
|
||||
if strings.HasPrefix(buffer.filename, "~/") {
|
||||
homedir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
buffer.filename = filepath.Join(homedir, buffer.filename[2:])
|
||||
}
|
||||
|
||||
// Append new line character at end of buffer contents if not present
|
||||
if buffer.Contents[len(buffer.Contents)-1] != '\n' {
|
||||
buffer.Contents += "\n"
|
||||
@ -164,6 +184,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, "~/") {
|
||||
|
@ -21,14 +21,16 @@ func initCommands() {
|
||||
copyCmd := Command{
|
||||
cmd: "copy",
|
||||
run: func(window *Window, args ...string) {
|
||||
if window.CurrentBuffer.Selection == nil {
|
||||
// Copy line
|
||||
_, line := window.GetCursorPos2D()
|
||||
window.Clipboard = strings.SplitAfter(window.CurrentBuffer.Contents, "\n")[line]
|
||||
// Copy text from buffer
|
||||
copiedText, copyingMethod := window.CurrentBuffer.CopyText()
|
||||
|
||||
// Put copied text to clipboard
|
||||
window.Clipboard = copiedText
|
||||
|
||||
// Send appropriate message depending on copying method
|
||||
if copyingMethod == 0 {
|
||||
PrintMessage(window, "Copied line to clipboard.")
|
||||
} else {
|
||||
// Copy selection
|
||||
window.Clipboard = window.CurrentBuffer.GetSelectedText()
|
||||
PrintMessage(window, "Copied selection to clipboard.")
|
||||
}
|
||||
},
|
||||
@ -37,16 +39,10 @@ func initCommands() {
|
||||
pasteCmd := Command{
|
||||
cmd: "paste",
|
||||
run: func(window *Window, args ...string) {
|
||||
str := window.CurrentBuffer.Contents
|
||||
index := window.CurrentBuffer.CursorPos
|
||||
|
||||
if index == len(str) {
|
||||
str += window.Clipboard
|
||||
} else {
|
||||
str = str[:index] + window.Clipboard + str[index:]
|
||||
if window.Clipboard != "" {
|
||||
window.CurrentBuffer.PasteText(window, window.Clipboard)
|
||||
PrintMessage(window, "Pasted text to buffer.")
|
||||
}
|
||||
window.CurrentBuffer.Contents = str
|
||||
window.SetCursorPos(window.CurrentBuffer.CursorPos + len(window.Clipboard))
|
||||
},
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user