Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

Commit

Permalink
Made a work-around for selection char.
Browse files Browse the repository at this point in the history
Previously when using MoveCursorToIndex, the cursor would not use the
correct character - now it does.
  • Loading branch information
avahe-kellenberger authored and Bios-Marcel committed Oct 6, 2019
1 parent 87b3f2c commit 6d765cb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
28 changes: 10 additions & 18 deletions ui/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,29 +112,21 @@ func (e *Editor) MoveCursorRight(left, right, selection []rune) {
e.setAndFixText(newText)
}

func (e *Editor) MoveCursorToIndex(left, right, selection []rune, index int) {
var newText string = string(left) + string(selection) + string(right)
func (e *Editor) MoveCursorToIndex(text string, index int) {
// Bound the index to the string length
if index < 0 {
index = 0
} else if index >= len(newText) {
index = len(newText) - 1
} else if index >= len(text) {
index = len(text) - 1
}

if index < len(left) {
newText = leftRegion + string(left[:index]) + selRegion + string(left[index]) + rightRegion + string(left[index+1:]) + string(right) + endRegion
} else {
indexSelection := index - len(left)
if indexSelection < len(selection) {
newText = leftRegion + string(left) + string(left[:indexSelection]) + selRegion + string(selection[indexSelection]) + rightRegion + string(selection[indexSelection+1:]) + string(right) + endRegion
} else {
indexRight := index - len(left) - len(selection)
if indexRight < len(right) {
newText = leftRegion + string(left) + string(selection) + string(right[:indexRight]) + selRegion + string(right[indexRight]) + rightRegion + string(right[indexRight+1:]) + endRegion
}
}
left := string(text[:index])
right := string(text[index:])
if len(right) == 1 {
right = " " + selectionChar
left += " "
}

e.setAndFixText(newText)
e.setAndFixText(leftRegion + left + selRegion + string(right[0]) + rightRegion + right[1:] + endRegion)
}

func (e *Editor) SelectWordLeft(left, right, selection []rune) {
Expand Down
4 changes: 2 additions & 2 deletions ui/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -845,14 +845,14 @@ func NewWindow(doRestart chan bool, app *tview.Application, session *discordgo.S
left := oldText[:beginIndex] + strings.TrimSpace(data) + " "
right := oldText[endIndex+1:]
window.messageInput.SetText(left + right)
window.messageInput.MoveCursorToIndex([]rune(left), []rune(right), []rune{}, len(left))
window.messageInput.MoveCursorToIndex(left+right, len(left))
} else {
role, ok := node.GetReference().(*discordgo.Role)
if ok {
left := "<@&" + strings.TrimSpace(role.ID) + "> "
right := oldText[endIndex+1:]
window.messageInput.SetText(left + right)
window.messageInput.MoveCursorToIndex([]rune(left), []rune(right), []rune{}, len(left))
window.messageInput.MoveCursorToIndex(left+right, len(left))
}
}
window.messageInput.mentionHideHandler()
Expand Down

0 comments on commit 6d765cb

Please sign in to comment.