2022-01-29 10:09:20 +02:00
|
|
|
package context
|
|
|
|
|
2022-02-05 01:31:07 +02:00
|
|
|
import (
|
2022-02-05 05:42:56 +02:00
|
|
|
"github.com/jesseduffield/gocui"
|
2022-02-05 01:31:07 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
|
|
|
)
|
2022-01-29 10:09:20 +02:00
|
|
|
|
|
|
|
type BaseContext struct {
|
2022-01-30 00:53:28 +02:00
|
|
|
kind types.ContextKind
|
|
|
|
key types.ContextKey
|
2022-01-29 10:09:20 +02:00
|
|
|
ViewName string
|
2022-01-30 00:53:28 +02:00
|
|
|
windowName string
|
|
|
|
onGetOptionsMap func() map[string]string
|
2022-01-29 10:09:20 +02:00
|
|
|
|
2022-02-05 05:42:56 +02:00
|
|
|
keybindingsFns []types.KeybindingsFn
|
|
|
|
mouseKeybindingsFns []types.MouseKeybindingsFn
|
2022-02-05 01:31:07 +02:00
|
|
|
|
2022-02-05 07:56:36 +02:00
|
|
|
focusable bool
|
|
|
|
|
2022-01-29 10:09:20 +02:00
|
|
|
*ParentContextMgr
|
|
|
|
}
|
|
|
|
|
2022-02-05 01:31:07 +02:00
|
|
|
var _ types.IBaseContext = &BaseContext{}
|
|
|
|
|
2022-01-30 00:53:28 +02:00
|
|
|
type NewBaseContextOpts struct {
|
|
|
|
Kind types.ContextKind
|
|
|
|
Key types.ContextKey
|
|
|
|
ViewName string
|
|
|
|
WindowName string
|
2022-02-05 07:56:36 +02:00
|
|
|
Focusable bool
|
2022-01-30 00:53:28 +02:00
|
|
|
|
|
|
|
OnGetOptionsMap func() map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBaseContext(opts NewBaseContextOpts) *BaseContext {
|
|
|
|
return &BaseContext{
|
|
|
|
kind: opts.Kind,
|
|
|
|
key: opts.Key,
|
|
|
|
ViewName: opts.ViewName,
|
|
|
|
windowName: opts.WindowName,
|
|
|
|
onGetOptionsMap: opts.OnGetOptionsMap,
|
2022-02-05 07:56:36 +02:00
|
|
|
focusable: opts.Focusable,
|
2022-01-30 00:53:28 +02:00
|
|
|
ParentContextMgr: &ParentContextMgr{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-29 10:09:20 +02:00
|
|
|
func (self *BaseContext) GetOptionsMap() map[string]string {
|
2022-01-30 00:53:28 +02:00
|
|
|
if self.onGetOptionsMap != nil {
|
|
|
|
return self.onGetOptionsMap()
|
2022-01-29 10:09:20 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *BaseContext) SetWindowName(windowName string) {
|
2022-01-30 00:53:28 +02:00
|
|
|
self.windowName = windowName
|
2022-01-29 10:09:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *BaseContext) GetWindowName() string {
|
2022-01-30 00:53:28 +02:00
|
|
|
return self.windowName
|
2022-01-29 10:09:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *BaseContext) GetViewName() string {
|
|
|
|
return self.ViewName
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *BaseContext) GetKind() types.ContextKind {
|
2022-01-30 00:53:28 +02:00
|
|
|
return self.kind
|
2022-01-29 10:09:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *BaseContext) GetKey() types.ContextKey {
|
2022-01-30 00:53:28 +02:00
|
|
|
return self.key
|
2022-01-29 10:09:20 +02:00
|
|
|
}
|
2022-02-05 01:31:07 +02:00
|
|
|
|
|
|
|
func (self *BaseContext) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
|
|
|
|
bindings := []*types.Binding{}
|
|
|
|
for i := range self.keybindingsFns {
|
|
|
|
// the first binding in the bindings array takes precedence but we want the
|
|
|
|
// last keybindingsFn to take precedence to we add them in reverse
|
|
|
|
bindings = append(bindings, self.keybindingsFns[len(self.keybindingsFns)-1-i](opts)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return bindings
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *BaseContext) AddKeybindingsFn(fn types.KeybindingsFn) {
|
|
|
|
self.keybindingsFns = append(self.keybindingsFns, fn)
|
|
|
|
}
|
2022-02-05 05:42:56 +02:00
|
|
|
|
|
|
|
func (self *BaseContext) AddMouseKeybindingsFn(fn types.MouseKeybindingsFn) {
|
|
|
|
self.mouseKeybindingsFns = append(self.mouseKeybindingsFns, fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *BaseContext) GetMouseKeybindings(opts types.KeybindingsOpts) []*gocui.ViewMouseBinding {
|
|
|
|
bindings := []*gocui.ViewMouseBinding{}
|
|
|
|
for i := range self.mouseKeybindingsFns {
|
|
|
|
// the first binding in the bindings array takes precedence but we want the
|
|
|
|
// last keybindingsFn to take precedence to we add them in reverse
|
|
|
|
bindings = append(bindings, self.mouseKeybindingsFns[len(self.mouseKeybindingsFns)-1-i](opts)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return bindings
|
|
|
|
}
|
2022-02-05 07:56:36 +02:00
|
|
|
|
|
|
|
func (self *BaseContext) IsFocusable() bool {
|
|
|
|
return self.focusable
|
|
|
|
}
|