Skip to content

Commit 5a630ae

Browse files
committed
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.
1 parent 94aa110 commit 5a630ae

File tree

18 files changed

+272
-147
lines changed

18 files changed

+272
-147
lines changed

pkg/cheatsheet/generate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ func localisedTitle(tr *i18n.TranslationSet, str string) string {
116116
"commitDescription": tr.CommitDescriptionTitle,
117117
"commits": tr.CommitsTitle,
118118
"confirmation": tr.ConfirmationTitle,
119+
"prompt": tr.PromptTitle,
119120
"information": tr.InformationTitle,
120121
"main": tr.NormalTitle,
121122
"patchBuilding": tr.PatchBuildingTitle,

pkg/gui/context/context.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const (
4141

4242
MENU_CONTEXT_KEY types.ContextKey = "menu"
4343
CONFIRMATION_CONTEXT_KEY types.ContextKey = "confirmation"
44+
PROMPT_CONTEXT_KEY types.ContextKey = "prompt"
4445
SEARCH_CONTEXT_KEY types.ContextKey = "search"
4546
COMMIT_MESSAGE_CONTEXT_KEY types.ContextKey = "commitMessage"
4647
COMMIT_DESCRIPTION_CONTEXT_KEY types.ContextKey = "commitDescription"
@@ -73,6 +74,7 @@ var AllContextKeys = []types.ContextKey{
7374

7475
MENU_CONTEXT_KEY,
7576
CONFIRMATION_CONTEXT_KEY,
77+
PROMPT_CONTEXT_KEY,
7678
SEARCH_CONTEXT_KEY,
7779
COMMIT_MESSAGE_CONTEXT_KEY,
7880
SUBMODULES_CONTEXT_KEY,
@@ -106,6 +108,7 @@ type ContextTree struct {
106108
CustomPatchBuilderSecondary types.Context
107109
MergeConflicts *MergeConflictsContext
108110
Confirmation *ConfirmationContext
111+
Prompt *PromptContext
109112
CommitMessage *CommitMessageContext
110113
CommitDescription types.Context
111114
CommandLog types.Context
@@ -141,6 +144,7 @@ func (self *ContextTree) Flatten() []types.Context {
141144
self.Stash,
142145
self.Menu,
143146
self.Confirmation,
147+
self.Prompt,
144148
self.CommitMessage,
145149
self.CommitDescription,
146150

pkg/gui/context/prompt_context.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package context
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/gui/types"
5+
)
6+
7+
type PromptContext struct {
8+
*SimpleContext
9+
c *ContextCommon
10+
11+
State ConfirmationContextState
12+
}
13+
14+
var _ types.Context = (*PromptContext)(nil)
15+
16+
func NewPromptContext(
17+
c *ContextCommon,
18+
) *PromptContext {
19+
return &PromptContext{
20+
c: c,
21+
SimpleContext: NewSimpleContext(NewBaseContext(NewBaseContextOpts{
22+
View: c.Views().Prompt,
23+
WindowName: "prompt",
24+
Key: PROMPT_CONTEXT_KEY,
25+
Kind: types.TEMPORARY_POPUP,
26+
Focusable: true,
27+
HasUncontrolledBounds: true,
28+
})),
29+
}
30+
}

pkg/gui/context/setup.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func NewContextTree(c *ContextCommon) *ContextTree {
8484
c,
8585
),
8686
Confirmation: NewConfirmationContext(c),
87+
Prompt: NewPromptContext(c),
8788
CommitMessage: NewCommitMessageContext(c),
8889
CommitDescription: NewSimpleContext(
8990
NewBaseContext(NewBaseContextOpts{

pkg/gui/controllers.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ func (gui *Gui) resetHelpersAndControllers() {
193193
statusController := controllers.NewStatusController(common)
194194
commandLogController := controllers.NewCommandLogController(common)
195195
confirmationController := controllers.NewConfirmationController(common)
196+
promptController := controllers.NewPromptController(common)
196197
suggestionsController := controllers.NewSuggestionsController(common)
197198
jumpToSideWindowController := controllers.NewJumpToSideWindowController(common, gui.handleNextTab)
198199

@@ -399,6 +400,10 @@ func (gui *Gui) resetHelpersAndControllers() {
399400
confirmationController,
400401
)
401402

403+
controllers.AttachControllers(gui.State.Contexts.Prompt,
404+
promptController,
405+
)
406+
402407
controllers.AttachControllers(gui.State.Contexts.Suggestions,
403408
suggestionsController,
404409
)
Lines changed: 5 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package controllers
22

33
import (
4-
"fmt"
5-
6-
"github.com/jesseduffield/gocui"
74
"github.com/jesseduffield/lazygit/pkg/gui/context"
85
"github.com/jesseduffield/lazygit/pkg/gui/types"
96
)
@@ -39,45 +36,19 @@ func (self *ConfirmationController) GetKeybindings(opts types.KeybindingsOpts) [
3936
DisplayOnScreen: true,
4037
},
4138
{
42-
Key: opts.GetKey(opts.Config.Universal.TogglePanel),
43-
Handler: func() error {
44-
if len(self.c.Contexts().Suggestions.State.Suggestions) > 0 {
45-
self.switchToSuggestions()
46-
}
47-
return nil
48-
},
49-
},
50-
{
51-
Key: opts.GetKey(opts.Config.Universal.CopyToClipboard),
52-
Handler: self.handleCopyToClipboard,
53-
Description: self.c.Tr.CopyToClipboardMenu,
54-
DisplayOnScreen: true,
55-
GetDisabledReason: self.copyToClipboardEnabled,
39+
Key: opts.GetKey(opts.Config.Universal.CopyToClipboard),
40+
Handler: self.handleCopyToClipboard,
41+
Description: self.c.Tr.CopyToClipboardMenu,
42+
DisplayOnScreen: true,
5643
},
5744
}
5845

5946
return bindings
6047
}
6148

62-
func (self *ConfirmationController) GetMouseKeybindings(opts types.KeybindingsOpts) []*gocui.ViewMouseBinding {
63-
return []*gocui.ViewMouseBinding{
64-
{
65-
ViewName: self.c.Contexts().Suggestions.GetViewName(),
66-
FocusedView: self.c.Contexts().Confirmation.GetViewName(),
67-
Key: gocui.MouseLeft,
68-
Handler: func(gocui.ViewMouseBindingOpts) error {
69-
self.switchToSuggestions()
70-
// Let it fall through to the ListController's click handler so that
71-
// the clicked line gets selected:
72-
return gocui.ErrKeybindingNotHandled
73-
},
74-
},
75-
}
76-
}
77-
7849
func (self *ConfirmationController) GetOnFocusLost() func(types.OnFocusLostOpts) {
7950
return func(types.OnFocusLostOpts) {
80-
self.c.Helpers().Confirmation.DeactivateConfirmationPrompt()
51+
self.c.Helpers().Confirmation.DeactivateConfirmation()
8152
}
8253
}
8354

@@ -89,18 +60,6 @@ func (self *ConfirmationController) context() *context.ConfirmationContext {
8960
return self.c.Contexts().Confirmation
9061
}
9162

92-
func (self *ConfirmationController) switchToSuggestions() {
93-
subtitle := ""
94-
if self.c.State().GetRepoState().GetCurrentPopupOpts().HandleDeleteSuggestion != nil {
95-
// We assume that whenever things are deletable, they
96-
// are also editable, so we show both keybindings
97-
subtitle = fmt.Sprintf(self.c.Tr.SuggestionsSubtitle,
98-
self.c.UserConfig().Keybinding.Universal.Remove, self.c.UserConfig().Keybinding.Universal.Edit)
99-
}
100-
self.c.Views().Suggestions.Subtitle = subtitle
101-
self.c.Context().Replace(self.c.Contexts().Suggestions)
102-
}
103-
10463
func (self *ConfirmationController) handleCopyToClipboard() error {
10564
confirmationView := self.c.Views().Confirmation
10665
text := confirmationView.Buffer()
@@ -111,12 +70,3 @@ func (self *ConfirmationController) handleCopyToClipboard() error {
11170
self.c.Toast(self.c.Tr.MessageCopiedToClipboard)
11271
return nil
11372
}
114-
115-
func (self *ConfirmationController) copyToClipboardEnabled() *types.DisabledReason {
116-
if self.c.Views().Confirmation.Editable {
117-
// The empty text is intentional. We don't want to get a toast when invoking this, we only
118-
// want to prevent it from showing up in the options bar.
119-
return &types.DisabledReason{Text: ""}
120-
}
121-
return nil
122-
}

0 commit comments

Comments
 (0)