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

88 lines
2.5 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 {
newlineKey, ok := gui.getKey(gui.c.UserConfig.Keybinding.Universal.AppendNewline).(gocui.Key)
if !ok {
newlineKey = gocui.KeyAltEnter
}
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()
case key == gocui.KeyArrowLeft:
2021-10-17 04:00:44 +02:00
textArea.MoveCursorLeft()
case key == gocui.KeyArrowRight:
2021-10-17 04:00:44 +02:00
textArea.MoveCursorRight()
case key == newlineKey:
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()
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()
2021-04-02 05:38:09 +02:00
// TODO: see if we need all three of these conditions: maybe the final one is sufficient
case ch != 0 && mod == 0 && 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, true)
// This function is called again on refresh as part of the general resize popup call,
// but we need to call it here so that when we go to render the text area it's not
// considered out of bounds to add a newline, meaning we can avoid unnecessary scrolling.
err := gui.resizePopupPanel(v, v.TextArea.GetContent())
if err != nil {
gui.c.Log.Error(err)
2021-10-17 04:00:44 +02:00
}
v.RenderTextArea()
gui.RenderCommitLength()
2021-02-18 10:39:33 +02:00
return matched
}
2021-02-18 10:39:33 +02:00
func (gui *Gui) defaultEditor(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 11:01:45 +02:00
if gui.findSuggestions != nil {
2021-10-17 04:00:44 +02:00
input := v.TextArea.GetContent()
gui.suggestionsAsyncHandler.Do(func() func() {
suggestions := gui.findSuggestions(input)
return func() { gui.setSuggestions(suggestions) }
})
}
2021-02-18 10:39:33 +02:00
return matched
}