-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add textbox custom command prompt #3548
Open
yongjoon-km
wants to merge
3
commits into
jesseduffield:master
Choose a base branch
from
yongjoon-km:custom-command-textbox
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package context | ||
|
||
import ( | ||
"github.com/jesseduffield/lazygit/pkg/gui/types" | ||
) | ||
|
||
type TextboxContext struct { | ||
*SimpleContext | ||
c *ContextCommon | ||
|
||
State TextboxContextState | ||
} | ||
|
||
type TextboxContextState struct { | ||
OnConfirm func() error | ||
OnClose func() error | ||
} | ||
|
||
var _ types.Context = (*TextboxContext)(nil) | ||
|
||
func NewTextboxContext( | ||
c *ContextCommon, | ||
) *TextboxContext { | ||
return &TextboxContext{ | ||
c: c, | ||
SimpleContext: NewSimpleContext(NewBaseContext(NewBaseContextOpts{ | ||
View: c.Views().Textbox, | ||
WindowName: "textbox", | ||
Key: TEXTBOX_CONTEXT_KEY, | ||
Kind: types.TEMPORARY_POPUP, | ||
Focusable: true, | ||
HasUncontrolledBounds: true, | ||
})), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package helpers | ||
|
||
import ( | ||
goContext "context" | ||
|
||
"github.com/jesseduffield/lazygit/pkg/gui/keybindings" | ||
"github.com/jesseduffield/lazygit/pkg/gui/types" | ||
"github.com/jesseduffield/lazygit/pkg/theme" | ||
"github.com/jesseduffield/lazygit/pkg/utils" | ||
) | ||
|
||
type TextboxHelper struct { | ||
c *HelperCommon | ||
} | ||
|
||
func NewTextboxHelper(c *HelperCommon) *TextboxHelper { | ||
return &TextboxHelper{ | ||
c: c, | ||
} | ||
} | ||
|
||
func (self *TextboxHelper) DeactivateTextboxPrompt() { | ||
self.c.Mutexes().PopupMutex.Lock() | ||
self.c.State().GetRepoState().SetCurrentPopupOpts(nil) | ||
self.c.Mutexes().PopupMutex.Unlock() | ||
|
||
self.c.Views().Textbox.Visible = false | ||
self.clearTextboxViewKeyBindings() | ||
} | ||
|
||
func (self *TextboxHelper) clearTextboxViewKeyBindings() { | ||
noop := func() error { return nil } | ||
self.c.Contexts().Textbox.State.OnConfirm = noop | ||
self.c.Contexts().Textbox.State.OnClose = noop | ||
} | ||
|
||
func (self *TextboxHelper) CreatePopupPanel(ctx goContext.Context, opts types.CreatePopupPanelOpts) error { | ||
self.c.Mutexes().PopupMutex.Lock() | ||
defer self.c.Mutexes().PopupMutex.Unlock() | ||
|
||
_, cancel := goContext.WithCancel(ctx) | ||
|
||
// we don't allow interruptions of non-loader popups in case we get stuck somehow | ||
// e.g. a credentials popup never gets its required user input so a process hangs | ||
// forever. | ||
// The proper solution is to have a queue of popup options | ||
currentPopupOpts := self.c.State().GetRepoState().GetCurrentPopupOpts() | ||
if currentPopupOpts != nil && !currentPopupOpts.HasLoader { | ||
self.c.Log.Error("ignoring create popup panel because a popup panel is already open") | ||
cancel() | ||
return nil | ||
} | ||
|
||
textboxView := self.c.Views().Textbox | ||
|
||
textboxView.Title = opts.Title | ||
// Introduce confirm key bindings of textbox to users | ||
textboxView.Subtitle = utils.ResolvePlaceholderString(self.c.Tr.TextboxSubTitle, | ||
map[string]string{ | ||
"textboxConfirmBinding": keybindings.Label(self.c.UserConfig.Keybinding.Universal.ConfirmInEditor), | ||
}) | ||
|
||
textboxView.Wrap = !opts.Editable | ||
textboxView.FgColor = theme.GocuiDefaultTextColor | ||
textboxView.Mask = runeForMask(opts.Mask) | ||
|
||
// Set view position | ||
width := self.getPopupPanelWidth() | ||
height := self.getPopupPanelHeight() | ||
x0, y0, x1, y1 := self.getPosition(width, height) | ||
self.c.GocuiGui().SetView(textboxView.Name(), x0, y0, x1, y1, 0) | ||
|
||
// Render text in textbox | ||
textboxView.Editable = opts.Editable | ||
textArea := textboxView.TextArea | ||
textArea.Clear() | ||
textArea.TypeString(opts.Prompt) | ||
textboxView.RenderTextArea() | ||
|
||
// Setting Handlers | ||
self.c.Contexts().Textbox.State.OnConfirm = self.wrappedPromptTextboxFunction(cancel, opts.HandleConfirmPrompt, func() string { return self.c.Views().Textbox.TextArea.GetContent() }) | ||
self.c.Contexts().Textbox.State.OnClose = self.wrappedTextboxFunction(cancel, opts.HandleClose) | ||
|
||
// Set text box to current popup | ||
self.c.State().GetRepoState().SetCurrentPopupOpts(&opts) | ||
|
||
return self.c.PushContext(self.c.Contexts().Textbox) | ||
} | ||
|
||
func (self *TextboxHelper) wrappedPromptTextboxFunction(cancel goContext.CancelFunc, function func(string) error, getResponse func() string) func() error { | ||
return self.wrappedTextboxFunction(cancel, func() error { | ||
return function(getResponse()) | ||
}) | ||
} | ||
|
||
func (self *TextboxHelper) wrappedTextboxFunction(cancel goContext.CancelFunc, function func() error) func() error { | ||
return func() error { | ||
cancel() | ||
|
||
if err := self.c.PopContext(); err != nil { | ||
return err | ||
} | ||
|
||
if function != nil { | ||
if err := function(); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func (self *TextboxHelper) getPosition(panelWidth int, panelHeight int) (int, int, int, int) { | ||
width, height := self.c.GocuiGui().Size() | ||
if panelHeight > height * 3 / 4 { | ||
panelHeight = height * 3 / 4 | ||
} | ||
return width / 2 - panelWidth / 2, | ||
height / 2 - panelHeight / 2 - panelHeight % 2 - 1, | ||
width / 2 + panelWidth / 2, | ||
height / 2 + panelHeight / 2 | ||
} | ||
|
||
func (self *TextboxHelper) getPopupPanelWidth() int { | ||
width, _ := self.c.GocuiGui().Size() | ||
panelWidth := 4 * width / 7 | ||
minWidth := 80 | ||
if panelWidth < minWidth { | ||
if width - 2 < minWidth { | ||
panelWidth = width - 2 | ||
} else { | ||
panelWidth = minWidth | ||
} | ||
} | ||
|
||
return panelWidth | ||
} | ||
|
||
func (self *TextboxHelper) getPopupPanelHeight() int { | ||
_, height := self.c.GocuiGui().Size() | ||
var panelHeight int | ||
maxHeight := 11 | ||
if height - 2 > maxHeight { | ||
panelHeight = maxHeight | ||
} else { | ||
panelHeight = height - 2 | ||
} | ||
|
||
return panelHeight | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package controllers | ||
|
||
import ( | ||
"github.com/jesseduffield/lazygit/pkg/gui/context" | ||
"github.com/jesseduffield/lazygit/pkg/gui/types" | ||
) | ||
|
||
type TextboxController struct { | ||
baseController | ||
c *ControllerCommon | ||
} | ||
|
||
var _ types.IController = &TextboxController{} | ||
|
||
func NewTextboxController( | ||
c *ControllerCommon, | ||
) *TextboxController { | ||
return &TextboxController{ | ||
baseController: baseController{}, | ||
c: c, | ||
} | ||
} | ||
|
||
func (self *TextboxController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding { | ||
bindings := []*types.Binding{ | ||
{ | ||
Key: opts.GetKey(opts.Config.Universal.ConfirmInEditor), | ||
Handler: func() error { return self.context().State.OnConfirm() }, | ||
Description: self.c.Tr.Confirm, | ||
DisplayOnScreen: true, | ||
}, | ||
{ | ||
Key: opts.GetKey(opts.Config.Universal.Return), | ||
Handler: func() error { return self.context().State.OnClose() }, | ||
Description: self.c.Tr.CloseCancel, | ||
DisplayOnScreen: true, | ||
}, | ||
} | ||
return bindings | ||
} | ||
|
||
func (self *TextboxController) GetOnFocusLost() func(types.OnFocusLostOpts) error { | ||
return func(types.OnFocusLostOpts) error { | ||
self.c.Helpers().Textbox.DeactivateTextboxPrompt() | ||
return nil | ||
} | ||
} | ||
|
||
func (self *TextboxController) Context() types.Context { | ||
return self.context() | ||
} | ||
|
||
func (self *TextboxController) context() *context.TextboxContext { | ||
return self.c.Contexts().Textbox | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I copy and paste lots from
confirmation_helper.go
It has many functionalities about input, menu, and commit_message.
Please tell me some ideas on how to organize functions in confirmation_helpers.go and textbox_helpers.go. 🙇