1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-10-25 05:37:37 +03:00

Refactor: add a separate Prompt view

So far, confirmations and prompts were handled by the same view, context, and
controller, with a bunch of conditional code based on whether the view is
editable. This was more or less ok so far, since it does save a little bit of
code duplication; however, now we need separate views, because we don't have
dynamic keybindings, but we want to map "confirm" to different keys in
confirmations (the "universal.confirm" user config) and prompts (hard-coded to
enter, because it doesn't make sense to customize it there).

It also allows us to get rid of the conditional code, which is a nice benefit;
and the code duplication is actually not *that* bad.
This commit is contained in:
Stefan Haller
2025-08-31 15:36:52 +02:00
parent 94aa1101c9
commit 5a630aeda1
18 changed files with 273 additions and 148 deletions

View File

@@ -42,7 +42,7 @@ func (self *SuggestionsController) GetKeybindings(opts types.KeybindingsOpts) []
},
{
Key: opts.GetKey(opts.Config.Universal.TogglePanel),
Handler: self.switchToConfirmation,
Handler: self.switchToPrompt,
},
{
Key: opts.GetKey(opts.Config.Universal.Remove),
@@ -55,11 +55,11 @@ func (self *SuggestionsController) GetKeybindings(opts types.KeybindingsOpts) []
Handler: func() error {
if self.context().State.AllowEditSuggestion {
if selectedItem := self.c.Contexts().Suggestions.GetSelected(); selectedItem != nil {
self.c.Contexts().Confirmation.GetView().TextArea.Clear()
self.c.Contexts().Confirmation.GetView().TextArea.TypeString(selectedItem.Value)
self.c.Contexts().Confirmation.GetView().RenderTextArea()
self.c.Contexts().Prompt.GetView().TextArea.Clear()
self.c.Contexts().Prompt.GetView().TextArea.TypeString(selectedItem.Value)
self.c.Contexts().Prompt.GetView().RenderTextArea()
self.c.Contexts().Suggestions.RefreshSuggestions()
return self.switchToConfirmation()
return self.switchToPrompt()
}
}
return nil
@@ -73,26 +73,26 @@ func (self *SuggestionsController) GetKeybindings(opts types.KeybindingsOpts) []
func (self *SuggestionsController) GetMouseKeybindings(opts types.KeybindingsOpts) []*gocui.ViewMouseBinding {
return []*gocui.ViewMouseBinding{
{
ViewName: self.c.Contexts().Confirmation.GetViewName(),
ViewName: self.c.Contexts().Prompt.GetViewName(),
FocusedView: self.c.Contexts().Suggestions.GetViewName(),
Key: gocui.MouseLeft,
Handler: func(gocui.ViewMouseBindingOpts) error {
return self.switchToConfirmation()
return self.switchToPrompt()
},
},
}
}
func (self *SuggestionsController) switchToConfirmation() error {
func (self *SuggestionsController) switchToPrompt() error {
self.c.Views().Suggestions.Subtitle = ""
self.c.Views().Suggestions.Highlight = false
self.c.Context().Replace(self.c.Contexts().Confirmation)
self.c.Context().Replace(self.c.Contexts().Prompt)
return nil
}
func (self *SuggestionsController) GetOnFocusLost() func(types.OnFocusLostOpts) {
return func(types.OnFocusLostOpts) {
self.c.Helpers().Confirmation.DeactivateConfirmationPrompt()
self.c.Helpers().Confirmation.DeactivatePrompt()
}
}