2022-08-09 12:27:44 +02:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
2022-12-24 07:46:01 +02:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2022-08-09 12:27:44 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gdamore/tcell/v2"
|
|
|
|
"github.com/jesseduffield/gocui"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/config"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/keybindings"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
2022-08-09 13:27:12 +02:00
|
|
|
integrationTypes "github.com/jesseduffield/lazygit/pkg/integration/types"
|
2022-08-09 12:27:44 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// this gives our integration test a way of interacting with the gui for sending keypresses
|
|
|
|
// and reading state.
|
2022-08-13 04:56:04 +02:00
|
|
|
type GuiDriver struct {
|
2022-08-09 12:27:44 +02:00
|
|
|
gui *Gui
|
|
|
|
}
|
|
|
|
|
2022-08-13 04:56:04 +02:00
|
|
|
var _ integrationTypes.GuiDriver = &GuiDriver{}
|
2022-08-09 12:27:44 +02:00
|
|
|
|
2022-08-13 04:56:04 +02:00
|
|
|
func (self *GuiDriver) PressKey(keyStr string) {
|
2022-08-09 12:27:44 +02:00
|
|
|
key := keybindings.GetKey(keyStr)
|
|
|
|
|
|
|
|
var r rune
|
|
|
|
var tcellKey tcell.Key
|
|
|
|
switch v := key.(type) {
|
|
|
|
case rune:
|
|
|
|
r = v
|
|
|
|
tcellKey = tcell.KeyRune
|
|
|
|
case gocui.Key:
|
|
|
|
tcellKey = tcell.Key(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
self.gui.g.ReplayedEvents.Keys <- gocui.NewTcellKeyEventWrapper(
|
|
|
|
tcell.NewEventKey(tcellKey, r, tcell.ModNone),
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-08-13 04:56:04 +02:00
|
|
|
func (self *GuiDriver) Keys() config.KeybindingConfig {
|
2022-08-09 12:27:44 +02:00
|
|
|
return self.gui.Config.GetUserConfig().Keybinding
|
|
|
|
}
|
|
|
|
|
2022-08-13 04:56:04 +02:00
|
|
|
func (self *GuiDriver) CurrentContext() types.Context {
|
2022-08-09 12:27:44 +02:00
|
|
|
return self.gui.c.CurrentContext()
|
|
|
|
}
|
|
|
|
|
2023-02-24 12:42:27 +02:00
|
|
|
func (self *GuiDriver) ContextForView(viewName string) types.Context {
|
2023-03-21 11:57:52 +02:00
|
|
|
context, ok := self.gui.helpers.View.ContextForView(viewName)
|
2023-02-24 12:42:27 +02:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return context
|
|
|
|
}
|
|
|
|
|
2022-08-13 04:56:04 +02:00
|
|
|
func (self *GuiDriver) Fail(message string) {
|
2023-02-19 05:49:19 +02:00
|
|
|
currentView := self.gui.g.CurrentView()
|
|
|
|
fullMessage := fmt.Sprintf(
|
|
|
|
"%s\nFinal Lazygit state:\n%s\nUpon failure, focused view was '%s'.\nLog:\n%s", message,
|
|
|
|
self.gui.g.Snapshot(),
|
|
|
|
currentView.Name(),
|
|
|
|
strings.Join(self.gui.CmdLog, "\n"),
|
|
|
|
)
|
|
|
|
|
2022-08-09 12:27:44 +02:00
|
|
|
self.gui.g.Close()
|
|
|
|
// need to give the gui time to close
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
2023-02-19 05:49:19 +02:00
|
|
|
panic(fullMessage)
|
2022-08-09 12:27:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// logs to the normal place that you log to i.e. viewable with `lazygit --logs`
|
2022-08-13 04:56:04 +02:00
|
|
|
func (self *GuiDriver) Log(message string) {
|
2022-08-09 12:27:44 +02:00
|
|
|
self.gui.c.Log.Warn(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// logs in the actual UI (in the commands panel)
|
2022-08-13 04:56:04 +02:00
|
|
|
func (self *GuiDriver) LogUI(message string) {
|
2022-08-09 12:27:44 +02:00
|
|
|
self.gui.c.LogAction(message)
|
|
|
|
}
|
|
|
|
|
2022-08-13 04:56:04 +02:00
|
|
|
func (self *GuiDriver) CheckedOutRef() *models.Branch {
|
2022-08-09 12:27:44 +02:00
|
|
|
return self.gui.helpers.Refs.GetCheckedOutRef()
|
|
|
|
}
|
2022-08-14 12:13:39 +02:00
|
|
|
|
|
|
|
func (self *GuiDriver) MainView() *gocui.View {
|
|
|
|
return self.gui.mainView()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *GuiDriver) SecondaryView() *gocui.View {
|
|
|
|
return self.gui.secondaryView()
|
|
|
|
}
|
2022-08-22 11:34:02 +02:00
|
|
|
|
|
|
|
func (self *GuiDriver) View(viewName string) *gocui.View {
|
|
|
|
view, err := self.gui.g.View(viewName)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return view
|
|
|
|
}
|