1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-10 04:07:18 +02:00
lazygit/pkg/gui/controllers/snake_controller.go

80 lines
1.8 KiB
Go
Raw Normal View History

2022-12-30 02:34:01 +02:00
package controllers
import (
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/snake"
)
type SnakeController struct {
baseController
2023-03-23 09:47:29 +02:00
c *ControllerCommon
2022-12-30 02:34:01 +02:00
}
var _ types.IController = &SnakeController{}
func NewSnakeController(
2023-03-23 09:47:29 +02:00
common *ControllerCommon,
2022-12-30 02:34:01 +02:00
) *SnakeController {
return &SnakeController{
2023-03-23 09:47:29 +02:00
baseController: baseController{},
c: common,
2022-12-30 02:34:01 +02:00
}
}
func (self *SnakeController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
bindings := []*types.Binding{
{
Key: opts.GetKey(opts.Config.Universal.NextItem),
Handler: self.SetDirection(snake.Down),
},
{
Key: opts.GetKey(opts.Config.Universal.PrevItem),
Handler: self.SetDirection(snake.Up),
},
{
Key: opts.GetKey(opts.Config.Universal.PrevBlock),
Handler: self.SetDirection(snake.Left),
},
{
Key: opts.GetKey(opts.Config.Universal.NextBlock),
Handler: self.SetDirection(snake.Right),
},
{
Key: opts.GetKey(opts.Config.Universal.Return),
Handler: self.Escape,
},
}
return bindings
}
func (self *SnakeController) Context() types.Context {
2023-03-23 04:04:57 +02:00
return self.c.Contexts().Snake
2022-12-30 02:34:01 +02:00
}
func (self *SnakeController) GetOnFocus() func(types.OnFocusOpts) error {
return func(types.OnFocusOpts) error {
2023-03-23 09:47:29 +02:00
self.c.Helpers().Snake.StartGame()
return nil
}
}
func (self *SnakeController) GetOnFocusLost() func(types.OnFocusLostOpts) error {
return func(types.OnFocusLostOpts) error {
2023-03-23 09:47:29 +02:00
self.c.Helpers().Snake.ExitGame()
self.c.Helpers().Window.MoveToTopOfWindow(self.c.Contexts().Submodules)
return nil
}
}
2022-12-30 02:34:01 +02:00
func (self *SnakeController) SetDirection(direction snake.Direction) func() error {
return func() error {
2023-03-23 09:47:29 +02:00
self.c.Helpers().Snake.SetDirection(direction)
2022-12-30 02:34:01 +02:00
return nil
}
}
func (self *SnakeController) Escape() error {
2023-03-23 04:04:57 +02:00
return self.c.PushContext(self.c.Contexts().Submodules)
2022-12-30 02:34:01 +02:00
}