From 12425f0aa760759d916ef16bbd028b2c2de5577b Mon Sep 17 00:00:00 2001 From: mjarkk Date: Sat, 20 Oct 2018 17:37:55 +0200 Subject: [PATCH] First good success --- pkg/commands/os.go | 3 +- pkg/gui/commit_message_panel.go | 66 +++++++++++++++++++++++++++++++++ pkg/gui/confirmation_panel.go | 1 + pkg/gui/files_panel.go | 19 +++++----- pkg/gui/gui.go | 14 +++++++ pkg/gui/keybindings.go | 10 +++++ pkg/gui/view_helpers.go | 9 ++++- 7 files changed, 109 insertions(+), 13 deletions(-) diff --git a/pkg/commands/os.go b/pkg/commands/os.go index ae7e9c325..a64b286f4 100644 --- a/pkg/commands/os.go +++ b/pkg/commands/os.go @@ -92,8 +92,7 @@ func (c *OSCommand) RunCommandWithOutputLive(command string, output func(string) for scanner.Scan() { toWrite := output(re.ReplaceAllString(scanner.Text(), "")) if len(toWrite) > 0 { - _, err := tty.Write([]byte(toWrite + "\n")) - logrus.Error(err.Error()) + _, _ = tty.Write([]byte(toWrite + "\n")) } } }() diff --git a/pkg/gui/commit_message_panel.go b/pkg/gui/commit_message_panel.go index c26b5573a..329ccec62 100644 --- a/pkg/gui/commit_message_panel.go +++ b/pkg/gui/commit_message_panel.go @@ -3,6 +3,7 @@ package gui import ( "strconv" "strings" + "sync" "github.com/jesseduffield/gocui" ) @@ -51,6 +52,71 @@ func (gui *Gui) handleCommitFocused(g *gocui.Gui, v *gocui.View) error { return gui.renderString(g, "options", message) } +var unamePassMessage = "" +var waitForGroup sync.WaitGroup +var waitForGroupActie = false + +// waitForPassUname wait for a username or password input from the pushPassUname popup +func (gui *Gui) waitForPassUname(g *gocui.Gui, currentView *gocui.View, passOrUname string) string { + pushPassUnameView := gui.getPushPassUnameView(g) + if passOrUname == "username" { + pushPassUnameView.Title = gui.Tr.SLocalize("PushUsername") + // pushPassUnameView.Mask = 1 + } else { + pushPassUnameView.Title = gui.Tr.SLocalize("PushPassword") + pushPassUnameView.Mask = '*' + } + g.Update(func(g *gocui.Gui) error { + g.SetViewOnTop("pushPassUname") + gui.switchFocus(g, currentView, pushPassUnameView) + gui.RenderCommitLength() + return nil + }) + waitForGroupActie = true + waitForGroup.Add(1) + waitForGroup.Wait() + + return unamePassMessage +} + +func (gui *Gui) handlePushConfirm(g *gocui.Gui, v *gocui.View) error { + message := gui.trimmedContent(v) + unamePassMessage = message + if waitForGroupActie { + defer waitForGroup.Done() + } + gui.refreshFiles(g) + v.Clear() + v.SetCursor(0, 0) + g.SetViewOnBottom("pushPassUname") + gui.switchFocus(g, v, gui.getFilesView(g)) + return gui.refreshCommits(g) +} + +func (gui *Gui) handlePushClose(g *gocui.Gui, v *gocui.View) error { + g.SetViewOnBottom("pushPassUname") + unamePassMessage = "" + if waitForGroupActie { + defer waitForGroup.Done() + } + return gui.switchFocus(g, v, gui.getFilesView(g)) +} + +func (gui *Gui) handlePushFocused(g *gocui.Gui, v *gocui.View) error { + if _, err := g.SetViewOnTop("pushPassUname"); 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/confirmation_panel.go b/pkg/gui/confirmation_panel.go index 58577e430..ef7c97e6e 100644 --- a/pkg/gui/confirmation_panel.go +++ b/pkg/gui/confirmation_panel.go @@ -85,6 +85,7 @@ func (gui *Gui) prepareConfirmationPanel(currentView *gocui.View, title, prompt func (gui *Gui) onNewPopupPanel() { gui.g.SetViewOnBottom("commitMessage") + gui.g.SetViewOnBottom("pushPassUname") } func (gui *Gui) createConfirmationPanel(g *gocui.Gui, currentView *gocui.View, title, prompt string, handleConfirm, handleClose func(*gocui.Gui, *gocui.View) error) error { diff --git a/pkg/gui/files_panel.go b/pkg/gui/files_panel.go index 513aa336d..8083c0e6a 100644 --- a/pkg/gui/files_panel.go +++ b/pkg/gui/files_panel.go @@ -355,22 +355,21 @@ func (gui *Gui) pullFiles(g *gocui.Gui, v *gocui.View) error { return nil } -func (gui *Gui) pushWithForceFlag(currentView *gocui.View, force bool) error { +func (gui *Gui) pushWithForceFlag(g *gocui.Gui, currentView *gocui.View, force bool) error { if err := gui.createMessagePanel(gui.g, currentView, "", gui.Tr.SLocalize("PushWait")); err != nil { return err } go func() { branchName := gui.State.Branches[0].Name err := gui.GitCommand.Push(branchName, force, func(passOrUname string) string { - if passOrUname == "password" { - // TODO: ask for password - return "some password" - } - // TODO: ask for username - return "some username" + return gui.waitForPassUname(g, currentView, passOrUname) }) if err != nil { - _ = gui.createErrorPanel(gui.g, err.Error()) + errMessage := err.Error() + if errMessage == "exit status 128" { + errMessage = gui.Tr.SLocalize("PassUnameWrong") + } + _ = gui.createErrorPanel(gui.g, errMessage) } else { _ = gui.closeConfirmationPrompt(gui.g) _ = gui.refreshCommits(gui.g) @@ -384,10 +383,10 @@ func (gui *Gui) pushFiles(g *gocui.Gui, v *gocui.View) error { // if we have pullables we'll ask if the user wants to force push _, pullables := gui.GitCommand.UpstreamDifferenceCount() if pullables == "?" || pullables == "0" { - return gui.pushWithForceFlag(v, false) + return gui.pushWithForceFlag(g, v, false) } err := gui.createConfirmationPanel(g, nil, gui.Tr.SLocalize("ForcePush"), gui.Tr.SLocalize("ForcePushPrompt"), func(g *gocui.Gui, v *gocui.View) error { - return gui.pushWithForceFlag(v, true) + return gui.pushWithForceFlag(g, v, true) }, nil) return err } diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 7e7648cfe..2baf354c0 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -268,6 +268,20 @@ func (gui *Gui) layout(g *gocui.Gui) error { } } + if gui.getPushPassUnameView(g) == nil { + // doesn't matter where this view starts because it will be hidden + if pushPassUnameView, err := g.SetView("pushPassUname", 0, 0, width/2, height/2, 0); err != nil { + if err != gocui.ErrUnknownView { + return err + } + g.SetViewOnBottom("pushPassUname") + pushPassUnameView.Title = gui.Tr.SLocalize("PushUsername") + pushPassUnameView.FgColor = gocui.ColorWhite + pushPassUnameView.Editable = true + pushPassUnameView.Editor = gocui.EditorFunc(gui.simpleEditor) + } + } + if appStatusView, err := g.SetView("appStatus", -1, optionsTop, width, optionsTop+2, 0); err != nil { if err != gocui.ErrUnknownView { return err diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index 78271a3f7..a30dba5f4 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -372,6 +372,16 @@ func (gui *Gui) GetKeybindings() []*Binding { Key: gocui.KeyEsc, Modifier: gocui.ModNone, Handler: gui.handleCommitClose, + }, { + ViewName: "pushPassUname", + Key: gocui.KeyEnter, + Modifier: gocui.ModNone, + Handler: gui.handlePushConfirm, + }, { + ViewName: "pushPassUname", + Key: gocui.KeyEsc, + Modifier: gocui.ModNone, + Handler: gui.handlePushClose, }, { ViewName: "menu", Key: gocui.KeyEsc, diff --git a/pkg/gui/view_helpers.go b/pkg/gui/view_helpers.go index 6c3e5505c..a5414448f 100644 --- a/pkg/gui/view_helpers.go +++ b/pkg/gui/view_helpers.go @@ -94,6 +94,8 @@ func (gui *Gui) newLineFocused(g *gocui.Gui, v *gocui.View) error { return nil case "commitMessage": return gui.handleCommitFocused(g, v) + case "pushPassUname": + return gui.handlePushFocused(g, v) case "main": // TODO: pull this out into a 'view focused' function gui.refreshMergePanel(g) @@ -288,6 +290,11 @@ func (gui *Gui) getCommitMessageView(g *gocui.Gui) *gocui.View { return v } +func (gui *Gui) getPushPassUnameView(g *gocui.Gui) *gocui.View { + v, _ := g.View("pushPassUname") + return v +} + func (gui *Gui) getBranchesView(g *gocui.Gui) *gocui.View { v, _ := g.View("branches") return v @@ -304,7 +311,7 @@ func (gui *Gui) currentViewName(g *gocui.Gui) string { func (gui *Gui) resizeCurrentPopupPanel(g *gocui.Gui) error { v := g.CurrentView() - if v.Name() == "commitMessage" || v.Name() == "confirmation" { + if v.Name() == "commitMessage" || v.Name() == "pushPassUname" || v.Name() == "confirmation" { return gui.resizePopupPanel(g, v) } return nil