mirror of
https://github.com/EnumeratedDev/typer.git
synced 2026-09-16 05:36:10 +00:00
Add selections
This commit is contained in:
@@ -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, "~/") {
|
||||
|
||||
Reference in New Issue
Block a user