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

68 lines
1.8 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.
var DefaultEditor Editor = EditorFunc(simpleEditor)
// simpleEditor is used as the default gocui editor.
2021-02-18 19:39:33 +11:00
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()
2018-08-06 00:38:38 +10:00
case key == KeyArrowLeft:
2021-10-17 13:00:44 +11:00
v.TextArea.MoveCursorLeft()
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()
case key == KeyCtrlA:
2021-10-17 13:00:44 +11:00
v.TextArea.GoToStartOfLine()
case key == KeyCtrlE:
2021-10-17 13:00:44 +11:00
v.TextArea.GoToEndOfLine()
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
}