2020-11-28 04:14:48 +02:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
2021-03-21 01:12:17 +02:00
|
|
|
"unicode"
|
|
|
|
|
2020-11-28 04:14:48 +02:00
|
|
|
"github.com/jesseduffield/gocui"
|
|
|
|
)
|
|
|
|
|
2021-10-17 04:00:44 +02:00
|
|
|
func (gui *Gui) handleEditorKeypress(textArea *gocui.TextArea, key gocui.Key, ch rune, mod gocui.Modifier, allowMultiline bool) bool {
|
2020-11-28 04:14:48 +02:00
|
|
|
switch {
|
|
|
|
case key == gocui.KeyBackspace || key == gocui.KeyBackspace2:
|
2021-10-17 04:00:44 +02:00
|
|
|
textArea.BackSpaceChar()
|
2021-02-18 10:39:33 +02:00
|
|
|
case key == gocui.KeyCtrlD || key == gocui.KeyDelete:
|
2021-10-17 04:00:44 +02:00
|
|
|
textArea.DeleteChar()
|
2020-11-28 04:14:48 +02:00
|
|
|
case key == gocui.KeyArrowDown:
|
2021-10-17 04:00:44 +02:00
|
|
|
textArea.MoveCursorDown()
|
2020-11-28 04:14:48 +02:00
|
|
|
case key == gocui.KeyArrowUp:
|
2021-10-17 04:00:44 +02:00
|
|
|
textArea.MoveCursorUp()
|
2023-08-14 21:33:11 +02:00
|
|
|
case (key == gocui.KeyArrowLeft || ch == 'b') && (mod&gocui.ModAlt) != 0:
|
2022-10-05 15:29:51 +02:00
|
|
|
textArea.MoveLeftWord()
|
2022-08-09 09:58:43 +02:00
|
|
|
case key == gocui.KeyArrowLeft || key == gocui.KeyCtrlB:
|
2021-10-17 04:00:44 +02:00
|
|
|
textArea.MoveCursorLeft()
|
2023-08-14 21:33:11 +02:00
|
|
|
case (key == gocui.KeyArrowRight || ch == 'f') && (mod&gocui.ModAlt) != 0:
|
2022-10-05 15:29:51 +02:00
|
|
|
textArea.MoveRightWord()
|
2022-08-09 09:58:43 +02:00
|
|
|
case key == gocui.KeyArrowRight || key == gocui.KeyCtrlF:
|
2021-10-17 04:00:44 +02:00
|
|
|
textArea.MoveCursorRight()
|
2023-01-21 13:38:14 +02:00
|
|
|
case key == gocui.KeyEnter:
|
2021-10-17 04:00:44 +02:00
|
|
|
if allowMultiline {
|
|
|
|
textArea.TypeRune('\n')
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
2020-11-28 04:14:48 +02:00
|
|
|
case key == gocui.KeySpace:
|
2021-10-17 04:00:44 +02:00
|
|
|
textArea.TypeRune(' ')
|
2020-11-28 04:14:48 +02:00
|
|
|
case key == gocui.KeyInsert:
|
2021-10-17 04:00:44 +02:00
|
|
|
textArea.ToggleOverwrite()
|
2020-11-28 04:14:48 +02:00
|
|
|
case key == gocui.KeyCtrlU:
|
2021-10-17 04:00:44 +02:00
|
|
|
textArea.DeleteToStartOfLine()
|
2022-09-23 13:04:39 +02:00
|
|
|
case key == gocui.KeyCtrlK:
|
|
|
|
textArea.DeleteToEndOfLine()
|
2021-10-17 11:11:16 +02:00
|
|
|
case key == gocui.KeyCtrlA || key == gocui.KeyHome:
|
2021-10-17 04:00:44 +02:00
|
|
|
textArea.GoToStartOfLine()
|
2021-10-17 11:11:16 +02:00
|
|
|
case key == gocui.KeyCtrlE || key == gocui.KeyEnd:
|
2021-10-17 04:00:44 +02:00
|
|
|
textArea.GoToEndOfLine()
|
2022-09-23 13:04:39 +02:00
|
|
|
case key == gocui.KeyCtrlW:
|
|
|
|
textArea.BackSpaceWord()
|
|
|
|
case key == gocui.KeyCtrlY:
|
|
|
|
textArea.Yank()
|
2021-04-02 05:38:09 +02:00
|
|
|
|
2023-07-23 14:19:59 +02:00
|
|
|
case unicode.IsPrint(ch):
|
2021-10-17 04:00:44 +02:00
|
|
|
textArea.TypeRune(ch)
|
2021-04-02 05:38:09 +02:00
|
|
|
default:
|
2021-10-17 04:00:44 +02:00
|
|
|
return false
|
2020-11-28 04:14:48 +02:00
|
|
|
}
|
|
|
|
|
2021-10-17 04:00:44 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// we've just copy+pasted the editor from gocui to here so that we can also re-
|
|
|
|
// render the commit message length on each keypress
|
|
|
|
func (gui *Gui) commitMessageEditor(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) bool {
|
2023-01-21 13:38:14 +02:00
|
|
|
matched := gui.handleEditorKeypress(v.TextArea, key, ch, mod, false)
|
2021-10-17 04:00:44 +02:00
|
|
|
v.RenderTextArea()
|
2023-01-21 13:38:14 +02:00
|
|
|
gui.c.Contexts().CommitMessage.RenderCommitLength()
|
|
|
|
return matched
|
|
|
|
}
|
2021-02-18 10:39:33 +02:00
|
|
|
|
2023-01-21 13:38:14 +02:00
|
|
|
func (gui *Gui) commitDescriptionEditor(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) bool {
|
|
|
|
matched := gui.handleEditorKeypress(v.TextArea, key, ch, mod, true)
|
|
|
|
v.RenderTextArea()
|
|
|
|
gui.c.Contexts().CommitMessage.RenderCommitLength()
|
2021-02-18 10:39:33 +02:00
|
|
|
return matched
|
2020-11-28 04:14:48 +02:00
|
|
|
}
|
|
|
|
|
2023-01-21 13:38:14 +02:00
|
|
|
func (gui *Gui) promptEditor(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) bool {
|
2021-10-17 04:00:44 +02:00
|
|
|
matched := gui.handleEditorKeypress(v.TextArea, key, ch, mod, false)
|
2021-04-02 05:38:09 +02:00
|
|
|
|
2021-10-17 04:00:44 +02:00
|
|
|
v.RenderTextArea()
|
2020-11-28 04:14:48 +02:00
|
|
|
|
2023-03-21 11:57:52 +02:00
|
|
|
suggestionsContext := gui.State.Contexts.Suggestions
|
|
|
|
if suggestionsContext.State.FindSuggestions != nil {
|
2021-10-17 04:00:44 +02:00
|
|
|
input := v.TextArea.GetContent()
|
2023-03-21 11:57:52 +02:00
|
|
|
suggestionsContext.State.AsyncHandler.Do(func() func() {
|
|
|
|
suggestions := suggestionsContext.State.FindSuggestions(input)
|
|
|
|
return func() { suggestionsContext.SetSuggestions(suggestions) }
|
2021-10-18 10:00:03 +02:00
|
|
|
})
|
2020-11-28 04:14:48 +02:00
|
|
|
}
|
2021-02-18 10:39:33 +02:00
|
|
|
|
|
|
|
return matched
|
2020-11-28 04:14:48 +02:00
|
|
|
}
|
2023-05-27 06:14:43 +02:00
|
|
|
|
|
|
|
func (gui *Gui) searchEditor(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) bool {
|
|
|
|
matched := gui.handleEditorKeypress(v.TextArea, key, ch, mod, false)
|
|
|
|
v.RenderTextArea()
|
|
|
|
|
|
|
|
searchString := v.TextArea.GetContent()
|
|
|
|
|
|
|
|
gui.helpers.Search.OnPromptContentChanged(searchString)
|
|
|
|
|
|
|
|
return matched
|
|
|
|
}
|