1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-03-21 21:47:32 +02:00

78 lines
2.2 KiB
Go
Raw Normal View History

2018-08-06 00:38:38 +10:00
// Copyright 2014 The gocui Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gocui
2018-08-28 19:58:18 +10:00
import (
"unicode"
2018-08-28 19:58:18 +10:00
)
2018-08-06 00:38:38 +10:00
// Editor interface must be satisfied by gocui editors.
type Editor interface {
2021-02-18 19:39:33 +11:00
Edit(v *View, key Key, ch rune, mod Modifier) bool
2018-08-06 00:38:38 +10:00
}
// The EditorFunc type is an adapter to allow the use of ordinary functions as
// Editors. If f is a function with the appropriate signature, EditorFunc(f)
// is an Editor object that calls f.
2021-02-18 19:39:33 +11:00
type EditorFunc func(v *View, key Key, ch rune, mod Modifier) bool
2018-08-06 00:38:38 +10:00
// Edit calls f(v, key, ch, mod)
2021-02-18 19:39:33 +11:00
func (f EditorFunc) Edit(v *View, key Key, ch rune, mod Modifier) bool {
return f(v, key, ch, mod)
2018-08-06 00:38:38 +10:00
}
// DefaultEditor is the default editor.
2022-08-14 14:33:44 +10:00
var DefaultEditor Editor = EditorFunc(SimpleEditor)
2018-08-06 00:38:38 +10:00
2022-08-14 14:33:44 +10:00
// SimpleEditor is used as the default gocui editor.
func SimpleEditor(v *View, key Key, ch rune, mod Modifier) bool {
2018-08-06 00:38:38 +10:00
switch {
case key == KeyBackspace || key == KeyBackspace2:
2021-10-17 13:00:44 +11:00
v.TextArea.BackSpaceChar()
2021-02-18 19:39:33 +11:00
case key == KeyCtrlD || key == KeyDelete:
2021-10-17 13:00:44 +11:00
v.TextArea.DeleteChar()
2018-08-06 00:38:38 +10:00
case key == KeyArrowDown:
2021-10-17 13:00:44 +11:00
v.TextArea.MoveCursorDown()
2018-08-06 00:38:38 +10:00
case key == KeyArrowUp:
2021-10-17 13:00:44 +11:00
v.TextArea.MoveCursorUp()
2022-10-02 12:47:57 -07:00
case key == KeyArrowLeft && (mod&ModAlt) != 0:
v.TextArea.MoveLeftWord()
2018-08-06 00:38:38 +10:00
case key == KeyArrowLeft:
2021-10-17 13:00:44 +11:00
v.TextArea.MoveCursorLeft()
2022-10-02 12:47:57 -07:00
case key == KeyArrowRight && (mod&ModAlt) != 0:
v.TextArea.MoveRightWord()
2018-08-06 00:38:38 +10:00
case key == KeyArrowRight:
2021-10-17 13:00:44 +11:00
v.TextArea.MoveCursorRight()
case key == KeyEnter:
2021-10-17 13:00:44 +11:00
v.TextArea.TypeRune('\n')
case key == KeySpace:
v.TextArea.TypeRune(' ')
case key == KeyInsert:
v.TextArea.ToggleOverwrite()
case key == KeyCtrlU:
2021-10-17 13:00:44 +11:00
v.TextArea.DeleteToStartOfLine()
2022-09-23 20:01:44 +09:00
case key == KeyCtrlK:
v.TextArea.DeleteToEndOfLine()
2021-10-17 20:11:16 +11:00
case key == KeyCtrlA || key == KeyHome:
2021-10-17 13:00:44 +11:00
v.TextArea.GoToStartOfLine()
2021-10-17 20:11:16 +11:00
case key == KeyCtrlE || key == KeyEnd:
2021-10-17 13:00:44 +11:00
v.TextArea.GoToEndOfLine()
2022-09-23 20:01:44 +09:00
case key == KeyCtrlW:
v.TextArea.BackSpaceWord()
case key == KeyCtrlY:
v.TextArea.Yank()
2021-10-17 13:00:44 +11: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 13:00:44 +11:00
v.TextArea.TypeRune(ch)
default:
2021-10-17 13:00:44 +11:00
return false
2021-02-09 21:49:03 +11:00
}
2021-10-17 13:00:44 +11:00
v.RenderTextArea()
2018-08-06 00:38:38 +10:00
2021-10-17 13:00:44 +11:00
return true
2018-08-06 00:38:38 +10:00
}