summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2018-12-18 22:19:32 +1100
committerJesse Duffield <jessedduffield@gmail.com>2018-12-18 22:19:32 +1100
commit865c7c2332d71704b8a6bc22472313daf4997f16 (patch)
treea21ca94ab87395f99e7ea537e3ad95eb177c4511
parent11c7cbe3ac2adf4f6e717640df7a49e5a916d753 (diff)
minor refactor of credentials panel into its own file
-rw-r--r--pkg/gui/commit_message_panel.go84
-rw-r--r--pkg/gui/credentials_panel.go108
-rw-r--r--pkg/gui/keybindings.go4
-rw-r--r--pkg/gui/view_helpers.go18
4 files changed, 110 insertions, 104 deletions
diff --git a/pkg/gui/commit_message_panel.go b/pkg/gui/commit_message_panel.go
index 71f18e1f0..33eb5b218 100644
--- a/pkg/gui/commit_message_panel.go
+++ b/pkg/gui/commit_message_panel.go
@@ -51,90 +51,6 @@ func (gui *Gui) handleCommitFocused(g *gocui.Gui, v *gocui.View) error {
return gui.renderString(g, "options", message)
}
-type credentials chan string
-
-// waitForPassUname wait for a username or password input from the credentials popup
-func (gui *Gui) waitForPassUname(g *gocui.Gui, currentView *gocui.View, passOrUname string) string {
- gui.credentials = make(chan string)
- credentialsView, _ := g.View("credentials")
- if passOrUname == "username" {
- credentialsView.Title = gui.Tr.SLocalize("CredentialsUsername")
- credentialsView.Mask = 0
- } else {
- credentialsView.Title = gui.Tr.SLocalize("CredentialsPassword")
- credentialsView.Mask = '*'
- }
- g.Update(func(g *gocui.Gui) error {
- err := gui.switchFocus(g, currentView, credentialsView)
- if err != nil {
- return err
- }
- gui.RenderCommitLength()
- return nil
- })
-
- // wait for username/passwords input
- userInput := <-gui.credentials
- return userInput
-}
-
-func (gui *Gui) handlePushConfirm(g *gocui.Gui, v *gocui.View) error {
- message := gui.trimmedContent(v)
- if message == "" {
- // make sure to input something
- // if not dune the push progress will run forever
- message = "-"
- }
- gui.credentials <- message
- err := gui.refreshFiles(g)
- if err != nil {
- return err
- }
- v.Clear()
- err = v.SetCursor(0, 0)
- if err != nil {
- return err
- }
- _, err = g.SetViewOnBottom("credentials")
- if err != nil {
- return err
- }
- nextView, err := gui.g.View("confirmation")
- if err != nil {
- nextView = gui.getFilesView(g)
- }
- err = gui.switchFocus(g, nil, nextView)
- if err != nil {
- return err
- }
- return gui.refreshCommits(g)
-}
-
-func (gui *Gui) handlePushClose(g *gocui.Gui, v *gocui.View) error {
- _, err := g.SetViewOnBottom("credentials")
- if err != nil {
- return err
- }
-
- gui.credentials <- "-"
- return gui.switchFocus(g, nil, gui.getFilesView(g))
-}
-
-func (gui *Gui) handleCredentialsViewFocused(g *gocui.Gui, v *gocui.View) error {
- if _, err := g.SetViewOnTop("credentials"); err != nil {
- return err
- }
-
- message := gui.Tr.TemplateLocalize(
- "CloseConfirm",
- Teml{
- "keyBindClose": "esc",
- "keyBindConfirm": "enter",
- },
- )
- return gui.renderString(g, "options", message)
-}
-
func (gui *Gui) simpleEditor(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) {
switch {
case key == gocui.KeyBackspace || key == gocui.KeyBackspace2:
diff --git a/pkg/gui/credentials_panel.go b/pkg/gui/credentials_panel.go
new file mode 100644
index 000000000..6c8f8b4ef
--- /dev/null
+++ b/pkg/gui/credentials_panel.go
@@ -0,0 +1,108 @@
+package gui
+
+import (
+ "strings"
+
+ "github.com/jesseduffield/gocui"
+)
+
+type credentials chan string
+
+// waitForPassUname wait for a username or password input from the credentials popup
+func (gui *Gui) waitForPassUname(g *gocui.Gui, currentView *gocui.View, passOrUname string) string {
+ gui.credentials = make(chan string)
+ g.Update(func(g *gocui.Gui) error {
+ credentialsView, _ := g.View("credentials")
+ if passOrUname == "username" {
+ credentialsView.Title = gui.Tr.SLocalize("CredentialsUsername")
+ credentialsView.Mask = 0
+ } else {
+ credentialsView.Title = gui.Tr.SLocalize("CredentialsPassword")
+ credentialsView.Mask = '*'
+ }
+ err := gui.switchFocus(g, currentView, credentialsView)
+ if err != nil {
+ return err
+ }
+ gui.RenderCommitLength()
+ return nil
+ })
+
+ // wait for username/passwords input
+ userInput := <-gui.credentials
+ return userInput
+}
+
+func (gui *Gui) handleSubmitCredential(g *gocui.Gui, v *gocui.View) error {
+ message := gui.trimmedContent(v)
+ if message == "" {
+ // sending an obviously incorrect password so that the program isn't stuck waiting
+ message = "-"
+ }
+ gui.credentials <- message
+ err := gui.refreshFiles(g)
+ if err != nil {
+ return err
+ }
+ v.Clear()
+ err = v.SetCursor(0, 0)
+ if err != nil {
+ return err
+ }
+ _, err = g.SetViewOnBottom("credentials")
+ if err != nil {
+ return err
+ }
+ nextView, err := gui.g.View("confirmation")
+ if err != nil {
+ nextView = gui.getFilesView(g)
+ }
+ err = gui.switchFocus(g, nil, nextView)
+ if err != nil {
+ return err
+ }
+ return gui.refreshCommits(g)
+}
+
+func (gui *Gui) handleCloseCredentialsView(g *gocui.Gui, v *gocui.View) error {
+ _, err := g.SetViewOnBottom("credentials")
+ if err != nil {
+ return err
+ }
+
+ gui.credentials <- "-"
+ return gui.switchFocus(g, nil, gui.getFilesView(g))
+}
+
+func (gui *Gui) handleCredentialsViewFocused(g *gocui.Gui, v *gocui.View) error {
+ if _, err := g.SetViewOnTop("credentials"); err != nil {
+ return err
+ }
+
+ message := gui.Tr.TemplateLocalize(
+ "CloseConfirm",
+ Teml{
+ "keyBindClose": "esc",
+ "keyBindConfirm": "enter",
+ },
+ )
+ return gui.renderString(g, "options", message)
+}
+
+// HandleCredentialsPopup handles the views after executing a command that might ask for credentials
+func (gui *Gui) HandleCredentialsPopup(g *gocui.Gui, popupOpened bool, cmdErr error) {
+ if popupOpened {
+ _, _ = gui.g.SetViewOnBottom("credentials")
+ }
+ if cmdErr != nil {
+ errMessage := cmdErr.Error()
+ if strings.Contains(errMessage, "exit status 128") {
+ errMessage = gui.Tr.SLocalize("PassUnameWrong")
+ }
+ // we are not logging this error because it may contain a password
+ _ = gui.createSpecificErrorPanel(errMessage, gui.getFilesView(gui.g), false)
+ } else {
+ _ = gui.closeConfirmationPrompt(g)
+ _ = gui.refreshSidePanels(g)
+ }
+}
diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go
index b996e2e95..576fee59a 100644
--- a/pkg/gui/keybindings.go
+++ b/pkg/gui/keybindings.go
@@ -397,12 +397,12 @@ func (gui *Gui) GetKeybindings() []*Binding {
ViewName: "credentials",
Key: gocui.KeyEnter,
Modifier: gocui.ModNone,
- Handler: gui.handlePushConfirm,
+ Handler: gui.handleSubmitCredential,
}, {
ViewName: "credentials",
Key: gocui.KeyEsc,
Modifier: gocui.ModNone,
- Handler: gui.handlePushClose,
+ Handler: gui.handleCloseCredentialsView,
}, {
ViewName: "menu",
Key: gocui.KeyEsc,
diff --git a/pkg/gui/view_helpers.go b/pkg/gui/view_helpers.go
index 4befe1e0d..75270e388 100644
--- a/pkg/gui/view_helpers.go
+++ b/pkg/gui/view_helpers.go
@@ -313,24 +313,6 @@ func (gui *Gui) resizeCurrentPopupPanel(g *gocui.Gui) error {
return nil
}
-// HandleCredentialsPopup handles the views after executing a command that might ask for credentials
-func (gui *Gui) HandleCredentialsPopup(g *gocui.Gui, popupOpened bool, cmdErr error) {
- if popupOpened {
- _, _ = gui.g.SetViewOnBottom("credentials")
- }
- if cmdErr != nil {
- errMessage := cmdErr.Error()
- if strings.Contains(errMessage, "exit status 128") {
- errMessage = gui.Tr.SLocalize("PassUnameWrong")
- }
- // we are not logging this error because it may contain a password
- _ = gui.createSpecificErrorPanel(errMessage, gui.getFilesView(gui.g), false)
- } else {
- _ = gui.closeConfirmationPrompt(g)
- _ = gui.refreshSidePanels(g)
- }
-}
-
func (gui *Gui) resizePopupPanel(g *gocui.Gui, v *gocui.View) error {
// If the confirmation panel is already displayed, just resize the width,
// otherwise continue