Rename Key to KeyName

This is to free up the name Key for a type that can be either a KeyName or a
rune (or later a string when we upgrade to tcell v3).
This commit is contained in:
Stefan Haller 2026-04-07 07:55:06 +02:00
parent e633ae6aa6
commit bff77fc4f9
10 changed files with 110 additions and 110 deletions

View file

@ -11,7 +11,7 @@ import (
// NOTE: if you make changes to this table, be sure to update
// docs/keybindings/Custom_Keybindings.md as well
var LabelByKey = map[gocui.Key]string{
var LabelByKey = map[gocui.KeyName]string{
gocui.KeyF1: "<f1>",
gocui.KeyF2: "<f2>",
gocui.KeyF3: "<f3>",

View file

@ -6,16 +6,16 @@ package gocui
// Editor interface must be satisfied by gocui editors.
type Editor interface {
Edit(v *View, key Key, ch rune, mod Modifier) bool
Edit(v *View, key KeyName, ch rune, mod Modifier) bool
}
// 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.
type EditorFunc func(v *View, key Key, ch rune, mod Modifier) bool
type EditorFunc func(v *View, key KeyName, ch rune, mod Modifier) bool
// Edit calls f(v, key, ch, mod)
func (f EditorFunc) Edit(v *View, key Key, ch rune, mod Modifier) bool {
func (f EditorFunc) Edit(v *View, key KeyName, ch rune, mod Modifier) bool {
return f(v, key, ch, mod)
}
@ -23,7 +23,7 @@ func (f EditorFunc) Edit(v *View, key Key, ch rune, mod Modifier) bool {
var DefaultEditor Editor = EditorFunc(SimpleEditor)
// SimpleEditor is used as the default gocui editor.
func SimpleEditor(v *View, key Key, ch rune, mod Modifier) bool {
func SimpleEditor(v *View, key KeyName, ch rune, mod Modifier) bool {
switch {
case (key == KeyBackspace || key == KeyBackspace2) && (mod&ModAlt) != 0,
key == KeyCtrlW:

View file

@ -81,14 +81,14 @@ type ViewMouseBinding struct {
Modifier Modifier
// must be a mouse key
Key Key
Key KeyName
}
type ViewMouseBindingOpts struct {
X int // i.e. origin x + cursor x
Y int // i.e. origin y + cursor y
Key Key // which button was clicked (will be one of the Mouse* constants)
Key KeyName // which button was clicked (will be one of the Mouse* constants)
IsDoubleClick bool // true if this is a double click
}
@ -111,7 +111,7 @@ type RecordingConfig struct {
type clickInfo struct {
x int
y int
key Key
key KeyName
viewName string
time time.Time
}
@ -182,7 +182,7 @@ type Gui struct {
ErrorHandler func(error) error
ShouldHandleMouseEvent func(view *View, key Key) bool
ShouldHandleMouseEvent func(view *View, key KeyName) bool
screen tcell.Screen
suspendedMutex sync.Mutex
@ -630,11 +630,11 @@ func (g *Gui) SetRenderSearchStatusFunc(renderSearchStatusFunc func(*View, int,
// getKey takes an empty interface with a key and returns the corresponding
// typed Key or rune.
func getKey(key any) (Key, rune, error) {
func getKey(key any) (KeyName, rune, error) {
switch t := key.(type) {
case nil: // Ignore keybinding if `nil`
return 0, 0, nil
case Key:
case KeyName:
return t, 0, nil
case rune:
return 0, t, nil
@ -1422,7 +1422,7 @@ func (g *Gui) onKey(ev *GocuiEvent) error {
}
// remember the information for this click, and return true if it was a double click
func (g *Gui) recordClickInfo(x, y int, key Key, v *View) bool {
func (g *Gui) recordClickInfo(x, y int, key KeyName, v *View) bool {
if IsMouseScrollKey(key) {
g.lastClick = nil
return false

View file

@ -8,8 +8,8 @@ import (
"github.com/gdamore/tcell/v2"
)
// Key represents special keys or keys combinations.
type Key tcell.Key
// KeyName represents special keys or keys combinations.
type KeyName tcell.Key
// Modifier allows to define special keys combinations. They can be used
// in combination with Keys or Runes when a new keybinding is defined.
@ -18,14 +18,14 @@ type Modifier tcell.ModMask
// Keybindings are used to link a given key-press event with a handler.
type keybinding struct {
viewName string
key Key
key KeyName
ch rune
mod Modifier
handler func(*Gui, *View) error
}
// newKeybinding returns a new Keybinding object.
func newKeybinding(viewname string, key Key, ch rune, mod Modifier, handler func(*Gui, *View) error) (kb *keybinding) {
func newKeybinding(viewname string, key KeyName, ch rune, mod Modifier, handler func(*Gui, *View) error) (kb *keybinding) {
kb = &keybinding{
viewName: viewname,
key: key,
@ -51,101 +51,101 @@ func eventMatchesKey(ev *GocuiEvent, key any) bool {
}
// matchKeypress returns if the keybinding matches the keypress.
func (kb *keybinding) matchKeypress(key Key, ch rune, mod Modifier) bool {
return kb.key == key && kb.ch == ch && kb.mod == mod
func (kb *keybinding) matchKeypress(keyName KeyName, ch rune, mod Modifier) bool {
return kb.key == keyName && kb.ch == ch && kb.mod == mod
}
// Special keys.
const (
KeyF1 Key = Key(tcell.KeyF1)
KeyF2 = Key(tcell.KeyF2)
KeyF3 = Key(tcell.KeyF3)
KeyF4 = Key(tcell.KeyF4)
KeyF5 = Key(tcell.KeyF5)
KeyF6 = Key(tcell.KeyF6)
KeyF7 = Key(tcell.KeyF7)
KeyF8 = Key(tcell.KeyF8)
KeyF9 = Key(tcell.KeyF9)
KeyF10 = Key(tcell.KeyF10)
KeyF11 = Key(tcell.KeyF11)
KeyF12 = Key(tcell.KeyF12)
KeyInsert = Key(tcell.KeyInsert)
KeyDelete = Key(tcell.KeyDelete)
KeyHome = Key(tcell.KeyHome)
KeyEnd = Key(tcell.KeyEnd)
KeyPgdn = Key(tcell.KeyPgDn)
KeyPgup = Key(tcell.KeyPgUp)
KeyArrowUp = Key(tcell.KeyUp)
KeyShiftArrowUp = Key(tcell.KeyF62)
KeyArrowDown = Key(tcell.KeyDown)
KeyShiftArrowDown = Key(tcell.KeyF63)
KeyArrowLeft = Key(tcell.KeyLeft)
KeyArrowRight = Key(tcell.KeyRight)
KeyF1 KeyName = KeyName(tcell.KeyF1)
KeyF2 = KeyName(tcell.KeyF2)
KeyF3 = KeyName(tcell.KeyF3)
KeyF4 = KeyName(tcell.KeyF4)
KeyF5 = KeyName(tcell.KeyF5)
KeyF6 = KeyName(tcell.KeyF6)
KeyF7 = KeyName(tcell.KeyF7)
KeyF8 = KeyName(tcell.KeyF8)
KeyF9 = KeyName(tcell.KeyF9)
KeyF10 = KeyName(tcell.KeyF10)
KeyF11 = KeyName(tcell.KeyF11)
KeyF12 = KeyName(tcell.KeyF12)
KeyInsert = KeyName(tcell.KeyInsert)
KeyDelete = KeyName(tcell.KeyDelete)
KeyHome = KeyName(tcell.KeyHome)
KeyEnd = KeyName(tcell.KeyEnd)
KeyPgdn = KeyName(tcell.KeyPgDn)
KeyPgup = KeyName(tcell.KeyPgUp)
KeyArrowUp = KeyName(tcell.KeyUp)
KeyShiftArrowUp = KeyName(tcell.KeyF62)
KeyArrowDown = KeyName(tcell.KeyDown)
KeyShiftArrowDown = KeyName(tcell.KeyF63)
KeyArrowLeft = KeyName(tcell.KeyLeft)
KeyArrowRight = KeyName(tcell.KeyRight)
)
// Keys combinations.
const (
KeyCtrlTilde = Key(tcell.KeyF64) // arbitrary assignment
KeyCtrlSpace = Key(tcell.KeyCtrlSpace)
KeyCtrlA = Key(tcell.KeyCtrlA)
KeyCtrlB = Key(tcell.KeyCtrlB)
KeyCtrlC = Key(tcell.KeyCtrlC)
KeyCtrlD = Key(tcell.KeyCtrlD)
KeyCtrlE = Key(tcell.KeyCtrlE)
KeyCtrlF = Key(tcell.KeyCtrlF)
KeyCtrlG = Key(tcell.KeyCtrlG)
KeyBackspace = Key(tcell.KeyBackspace)
KeyCtrlH = Key(tcell.KeyCtrlH)
KeyTab = Key(tcell.KeyTab)
KeyBacktab = Key(tcell.KeyBacktab)
KeyCtrlI = Key(tcell.KeyCtrlI)
KeyCtrlJ = Key(tcell.KeyCtrlJ)
KeyCtrlK = Key(tcell.KeyCtrlK)
KeyCtrlL = Key(tcell.KeyCtrlL)
KeyEnter = Key(tcell.KeyEnter)
KeyCtrlM = Key(tcell.KeyCtrlM)
KeyCtrlN = Key(tcell.KeyCtrlN)
KeyCtrlO = Key(tcell.KeyCtrlO)
KeyCtrlP = Key(tcell.KeyCtrlP)
KeyCtrlQ = Key(tcell.KeyCtrlQ)
KeyCtrlR = Key(tcell.KeyCtrlR)
KeyCtrlS = Key(tcell.KeyCtrlS)
KeyCtrlT = Key(tcell.KeyCtrlT)
KeyCtrlU = Key(tcell.KeyCtrlU)
KeyCtrlV = Key(tcell.KeyCtrlV)
KeyCtrlW = Key(tcell.KeyCtrlW)
KeyCtrlX = Key(tcell.KeyCtrlX)
KeyCtrlY = Key(tcell.KeyCtrlY)
KeyCtrlZ = Key(tcell.KeyCtrlZ)
KeyEsc = Key(tcell.KeyEscape)
KeyCtrlUnderscore = Key(tcell.KeyCtrlUnderscore)
KeySpace = Key(32)
KeyBackspace2 = Key(tcell.KeyBackspace2)
KeyCtrl8 = Key(tcell.KeyBackspace2) // same key as in termbox-go
KeyCtrlTilde = KeyName(tcell.KeyF64) // arbitrary assignment
KeyCtrlSpace = KeyName(tcell.KeyCtrlSpace)
KeyCtrlA = KeyName(tcell.KeyCtrlA)
KeyCtrlB = KeyName(tcell.KeyCtrlB)
KeyCtrlC = KeyName(tcell.KeyCtrlC)
KeyCtrlD = KeyName(tcell.KeyCtrlD)
KeyCtrlE = KeyName(tcell.KeyCtrlE)
KeyCtrlF = KeyName(tcell.KeyCtrlF)
KeyCtrlG = KeyName(tcell.KeyCtrlG)
KeyBackspace = KeyName(tcell.KeyBackspace)
KeyCtrlH = KeyName(tcell.KeyCtrlH)
KeyTab = KeyName(tcell.KeyTab)
KeyBacktab = KeyName(tcell.KeyBacktab)
KeyCtrlI = KeyName(tcell.KeyCtrlI)
KeyCtrlJ = KeyName(tcell.KeyCtrlJ)
KeyCtrlK = KeyName(tcell.KeyCtrlK)
KeyCtrlL = KeyName(tcell.KeyCtrlL)
KeyEnter = KeyName(tcell.KeyEnter)
KeyCtrlM = KeyName(tcell.KeyCtrlM)
KeyCtrlN = KeyName(tcell.KeyCtrlN)
KeyCtrlO = KeyName(tcell.KeyCtrlO)
KeyCtrlP = KeyName(tcell.KeyCtrlP)
KeyCtrlQ = KeyName(tcell.KeyCtrlQ)
KeyCtrlR = KeyName(tcell.KeyCtrlR)
KeyCtrlS = KeyName(tcell.KeyCtrlS)
KeyCtrlT = KeyName(tcell.KeyCtrlT)
KeyCtrlU = KeyName(tcell.KeyCtrlU)
KeyCtrlV = KeyName(tcell.KeyCtrlV)
KeyCtrlW = KeyName(tcell.KeyCtrlW)
KeyCtrlX = KeyName(tcell.KeyCtrlX)
KeyCtrlY = KeyName(tcell.KeyCtrlY)
KeyCtrlZ = KeyName(tcell.KeyCtrlZ)
KeyEsc = KeyName(tcell.KeyEscape)
KeyCtrlUnderscore = KeyName(tcell.KeyCtrlUnderscore)
KeySpace = KeyName(32)
KeyBackspace2 = KeyName(tcell.KeyBackspace2)
KeyCtrl8 = KeyName(tcell.KeyBackspace2) // same key as in termbox-go
// The following assignments were used in termbox implementation.
// In tcell, these are not keys per se. But in gocui we have them
// mapped to the keys so we have to use placeholder keys.
KeyAltEnter = Key(tcell.KeyF64) // arbitrary assignments
MouseLeft = Key(tcell.KeyF63)
MouseRight = Key(tcell.KeyF62)
MouseMiddle = Key(tcell.KeyF61)
MouseRelease = Key(tcell.KeyF60)
MouseWheelUp = Key(tcell.KeyF59)
MouseWheelDown = Key(tcell.KeyF58)
MouseWheelLeft = Key(tcell.KeyF57)
MouseWheelRight = Key(tcell.KeyF56)
KeyCtrl2 = Key(tcell.KeyNUL) // termbox defines theses
KeyCtrl3 = Key(tcell.KeyEscape)
KeyCtrl4 = Key(tcell.KeyCtrlBackslash)
KeyCtrl5 = Key(tcell.KeyCtrlRightSq)
KeyCtrl6 = Key(tcell.KeyCtrlCarat)
KeyCtrl7 = Key(tcell.KeyCtrlUnderscore)
KeyCtrlSlash = Key(tcell.KeyCtrlUnderscore)
KeyCtrlRsqBracket = Key(tcell.KeyCtrlRightSq)
KeyCtrlBackslash = Key(tcell.KeyCtrlBackslash)
KeyCtrlLsqBracket = Key(tcell.KeyCtrlLeftSq)
KeyAltEnter = KeyName(tcell.KeyF64) // arbitrary assignments
MouseLeft = KeyName(tcell.KeyF63)
MouseRight = KeyName(tcell.KeyF62)
MouseMiddle = KeyName(tcell.KeyF61)
MouseRelease = KeyName(tcell.KeyF60)
MouseWheelUp = KeyName(tcell.KeyF59)
MouseWheelDown = KeyName(tcell.KeyF58)
MouseWheelLeft = KeyName(tcell.KeyF57)
MouseWheelRight = KeyName(tcell.KeyF56)
KeyCtrl2 = KeyName(tcell.KeyNUL) // termbox defines theses
KeyCtrl3 = KeyName(tcell.KeyEscape)
KeyCtrl4 = KeyName(tcell.KeyCtrlBackslash)
KeyCtrl5 = KeyName(tcell.KeyCtrlRightSq)
KeyCtrl6 = KeyName(tcell.KeyCtrlCarat)
KeyCtrl7 = KeyName(tcell.KeyCtrlUnderscore)
KeyCtrlSlash = KeyName(tcell.KeyCtrlUnderscore)
KeyCtrlRsqBracket = KeyName(tcell.KeyCtrlRightSq)
KeyCtrlBackslash = KeyName(tcell.KeyCtrlBackslash)
KeyCtrlLsqBracket = KeyName(tcell.KeyCtrlLeftSq)
)
// Modifiers.

View file

@ -162,7 +162,7 @@ type gocuiEventType uint8
type GocuiEvent struct {
Type gocuiEventType
Mod Modifier
Key Key
Key KeyName
Ch rune
Width int
Height int
@ -329,7 +329,7 @@ func (g *Gui) pollEvent() GocuiEvent {
return GocuiEvent{
Type: eventKey,
Key: Key(k),
Key: KeyName(k),
Ch: ch,
Mod: Modifier(mod),
}

View file

@ -4,7 +4,7 @@ import (
"github.com/jesseduffield/lazygit/pkg/gocui"
)
func (gui *Gui) handleEditorKeypress(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier, allowMultiline bool) bool {
func (gui *Gui) handleEditorKeypress(v *gocui.View, key gocui.KeyName, ch rune, mod gocui.Modifier, allowMultiline bool) bool {
if key == gocui.KeyEnter && allowMultiline {
v.TextArea.TypeCharacter("\n")
v.RenderTextArea()
@ -16,20 +16,20 @@ func (gui *Gui) handleEditorKeypress(v *gocui.View, key gocui.Key, ch rune, mod
// 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 {
func (gui *Gui) commitMessageEditor(v *gocui.View, key gocui.KeyName, ch rune, mod gocui.Modifier) bool {
matched := gui.handleEditorKeypress(v, key, ch, mod, false)
v.RenderTextArea()
gui.c.Contexts().CommitMessage.RenderSubtitle()
return matched
}
func (gui *Gui) commitDescriptionEditor(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) bool {
func (gui *Gui) commitDescriptionEditor(v *gocui.View, key gocui.KeyName, ch rune, mod gocui.Modifier) bool {
matched := gui.handleEditorKeypress(v, key, ch, mod, true)
v.RenderTextArea()
return matched
}
func (gui *Gui) promptEditor(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) bool {
func (gui *Gui) promptEditor(v *gocui.View, key gocui.KeyName, ch rune, mod gocui.Modifier) bool {
matched := gui.handleEditorKeypress(v, key, ch, mod, false)
v.RenderTextArea()
@ -46,7 +46,7 @@ func (gui *Gui) promptEditor(v *gocui.View, key gocui.Key, ch rune, mod gocui.Mo
return matched
}
func (gui *Gui) searchEditor(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) bool {
func (gui *Gui) searchEditor(v *gocui.View, key gocui.KeyName, ch rune, mod gocui.Modifier) bool {
matched := gui.handleEditorKeypress(v, key, ch, mod, false)
v.RenderTextArea()

View file

@ -882,7 +882,7 @@ func (gui *Gui) Run(startArgs appTypes.StartArgs) error {
g.ErrorHandler = gui.PopupHandler.ErrorHandler
gui.g.ShouldHandleMouseEvent = func(view *gocui.View, key gocui.Key) bool {
gui.g.ShouldHandleMouseEvent = func(view *gocui.View, key gocui.KeyName) bool {
if gui.helpers.Confirmation.IsPopupPanelFocused() && gui.currentViewName() != view.Name() &&
!gocui.IsMouseScrollKey(key) {
// we ignore click events on views that aren't popup panels, when a popup panel is focused.

View file

@ -37,7 +37,7 @@ func (self *GuiDriver) PressKey(keyStr string) {
case rune:
r = v
tcellKey = tcell.KeyRune
case gocui.Key:
case gocui.KeyName:
tcellKey = tcell.Key(v)
}

View file

@ -22,7 +22,7 @@ func LabelFromKey(key types.Key) string {
switch key := key.(type) {
case rune:
keyInt = int(key)
case gocui.Key:
case gocui.KeyName:
value, ok := config.LabelByKey[key]
if ok {
return value

View file

@ -273,8 +273,8 @@ func (self *app) renderTests() {
}
}
func (self *app) wrapEditor(f func(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) bool) func(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) bool {
return func(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) bool {
func (self *app) wrapEditor(f func(v *gocui.View, key gocui.KeyName, ch rune, mod gocui.Modifier) bool) func(v *gocui.View, key gocui.KeyName, ch rune, mod gocui.Modifier) bool {
return func(v *gocui.View, key gocui.KeyName, ch rune, mod gocui.Modifier) bool {
matched := f(v, key, ch, mod)
if matched {
self.filterWithString(v.TextArea.GetContent())