mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-07-15 01:34:26 +02:00
introduce gui adapter
This commit is contained in:
73
pkg/gui/gui_adapter_impl.go
Normal file
73
pkg/gui/gui_adapter_impl.go
Normal file
@ -0,0 +1,73 @@
|
||||
package gui
|
||||
|
||||
import (
|
||||
"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"
|
||||
integrationTypes "github.com/jesseduffield/lazygit/pkg/integration/types"
|
||||
)
|
||||
|
||||
// this gives our integration test a way of interacting with the gui for sending keypresses
|
||||
// and reading state.
|
||||
type GuiAdapterImpl struct {
|
||||
gui *Gui
|
||||
}
|
||||
|
||||
var _ integrationTypes.GuiAdapter = &GuiAdapterImpl{}
|
||||
|
||||
func (self *GuiAdapterImpl) PressKey(keyStr string) {
|
||||
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,
|
||||
)
|
||||
}
|
||||
|
||||
func (self *GuiAdapterImpl) Keys() config.KeybindingConfig {
|
||||
return self.gui.Config.GetUserConfig().Keybinding
|
||||
}
|
||||
|
||||
func (self *GuiAdapterImpl) CurrentContext() types.Context {
|
||||
return self.gui.c.CurrentContext()
|
||||
}
|
||||
|
||||
func (self *GuiAdapterImpl) Model() *types.Model {
|
||||
return self.gui.State.Model
|
||||
}
|
||||
|
||||
func (self *GuiAdapterImpl) Fail(message string) {
|
||||
self.gui.g.Close()
|
||||
// need to give the gui time to close
|
||||
time.Sleep(time.Millisecond * 100)
|
||||
panic(message)
|
||||
}
|
||||
|
||||
// logs to the normal place that you log to i.e. viewable with `lazygit --logs`
|
||||
func (self *GuiAdapterImpl) Log(message string) {
|
||||
self.gui.c.Log.Warn(message)
|
||||
}
|
||||
|
||||
// logs in the actual UI (in the commands panel)
|
||||
func (self *GuiAdapterImpl) LogUI(message string) {
|
||||
self.gui.c.LogAction(message)
|
||||
}
|
||||
|
||||
func (self *GuiAdapterImpl) CheckedOutRef() *models.Branch {
|
||||
return self.gui.helpers.Refs.GetCheckedOutRef()
|
||||
}
|
@ -1,6 +1,10 @@
|
||||
package custom_commands
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"text/template"
|
||||
|
||||
"github.com/jesseduffield/lazygit/pkg/common"
|
||||
"github.com/jesseduffield/lazygit/pkg/config"
|
||||
)
|
||||
@ -101,3 +105,30 @@ func (self *Resolver) resolveMenuOption(option *config.CustomCommandMenuOption,
|
||||
Value: value,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(ResolveTemplate("old approach: {{index .PromptResponses 0}}, new approach: {{ .Form.a }}", CustomCommandObject{
|
||||
PromptResponses: []string{"a"},
|
||||
Form: map[string]string{"a": "B"},
|
||||
}))
|
||||
}
|
||||
|
||||
type CustomCommandObject struct {
|
||||
// deprecated. Use Responses instead
|
||||
PromptResponses []string
|
||||
Form map[string]string
|
||||
}
|
||||
|
||||
func ResolveTemplate(templateStr string, object interface{}) (string, error) {
|
||||
tmpl, err := template.New("template").Parse(templateStr)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
if err := tmpl.Execute(&buf, object); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return buf.String(), nil
|
||||
}
|
||||
|
@ -10,6 +10,10 @@ import (
|
||||
"github.com/jesseduffield/lazygit/pkg/utils"
|
||||
)
|
||||
|
||||
type IntegrationTest interface {
|
||||
Run(guiAdapter *GuiAdapterImpl)
|
||||
}
|
||||
|
||||
func (gui *Gui) handleTestMode() {
|
||||
if integration.PlayingIntegrationTest() {
|
||||
test, ok := integration.CurrentIntegrationTest()
|
||||
@ -21,17 +25,7 @@ func (gui *Gui) handleTestMode() {
|
||||
go func() {
|
||||
time.Sleep(time.Millisecond * 100)
|
||||
|
||||
shell := &integration.ShellImpl{}
|
||||
assert := &AssertImpl{gui: gui}
|
||||
keys := gui.Config.GetUserConfig().Keybinding
|
||||
input := NewInputImpl(gui, keys, assert, integration.KeyPressDelay())
|
||||
|
||||
test.Run(
|
||||
shell,
|
||||
input,
|
||||
assert,
|
||||
gui.c.UserConfig.Keybinding,
|
||||
)
|
||||
test.Run(&GuiAdapterImpl{gui: gui})
|
||||
|
||||
gui.g.Update(func(*gocui.Gui) error {
|
||||
return gocui.ErrQuit
|
||||
|
Reference in New Issue
Block a user