1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-04 10:34:55 +02:00
lazygit/pkg/gui/editors.go

102 lines
3.0 KiB
Go
Raw Normal View History

package gui
import (
"unicode"
"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 {
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()
case key == gocui.KeyArrowDown:
2021-10-17 04:00:44 +02:00
textArea.MoveCursorDown()
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()
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()
case key == gocui.KeyArrowRight || key == gocui.KeyCtrlF:
2021-10-17 04:00:44 +02:00
textArea.MoveCursorRight()
case key == gocui.KeyEnter:
2021-10-17 04:00:44 +02:00
if allowMultiline {
textArea.TypeRune('\n')
} else {
return false
}
case key == gocui.KeySpace:
2021-10-17 04:00:44 +02:00
textArea.TypeRune(' ')
case key == gocui.KeyInsert:
2021-10-17 04:00:44 +02:00
textArea.ToggleOverwrite()
case key == gocui.KeyCtrlU:
2021-10-17 04:00:44 +02:00
textArea.DeleteToStartOfLine()
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()
case key == gocui.KeyCtrlW:
textArea.BackSpaceWord()
case key == gocui.KeyCtrlY:
textArea.Yank()
2021-04-02 05:38:09 +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
}
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 {
matched := gui.handleEditorKeypress(v.TextArea, key, ch, mod, false)
2021-10-17 04:00:44 +02:00
v.RenderTextArea()
gui.c.Contexts().CommitMessage.RenderCommitLength()
return matched
}
2021-02-18 10:39:33 +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
}
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()
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-02-18 10:39:33 +02:00
return matched
}
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
}