Add selections

This commit is contained in:
2025-06-08 20:01:27 +03:00
parent 1620b6c2b9
commit 8f85e0991a
3 changed files with 195 additions and 4 deletions
+25
View File
@@ -12,11 +12,17 @@ type Buffer struct {
Name string
Contents string
CursorPos int
Selection *Selection
canSave bool
filename string
}
type Selection struct {
selectionStart int
selectionEnd int
}
var Buffers = make(map[int]*Buffer)
var LastBufferId int
@@ -54,6 +60,25 @@ func (buffer *Buffer) Save() error {
return nil
}
func (buffer *Buffer) GetSelectedText() string {
if buffer.Selection == nil {
return ""
}
if len(buffer.Contents) == 0 {
return ""
}
start := buffer.Selection.selectionStart
end := buffer.Selection.selectionEnd
if start <= end {
return buffer.Contents[buffer.Selection.selectionStart : buffer.Selection.selectionEnd+1]
} else {
return buffer.Contents[buffer.Selection.selectionEnd : buffer.Selection.selectionStart+1]
}
}
func GetOpenFileBuffer(filename string) *Buffer {
// Replace tilde with home directory
if filename != "~" && strings.HasPrefix(filename, "~/") {