summaryrefslogtreecommitdiffstats
path: root/pkg/gui/confirmation_panel.go
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2019-02-11 21:02:53 +1100
committerJesse Duffield <jessedduffield@gmail.com>2019-02-11 21:02:53 +1100
commit3d343e9b574a2c99ebf5b30dc9a4dac2886f6d73 (patch)
treeef6b2f8c08a29349bcc56a16260dfefdb3ee872d /pkg/gui/confirmation_panel.go
parenta3656154906c1117f9c9bbe100aa585e43417897 (diff)
parent3a607061a2303d9f45d308de652fbebe7300b43c (diff)
Merge branch 'master' into feature/rebasing
Diffstat (limited to 'pkg/gui/confirmation_panel.go')
-rw-r--r--pkg/gui/confirmation_panel.go59
1 files changed, 39 insertions, 20 deletions
diff --git a/pkg/gui/confirmation_panel.go b/pkg/gui/confirmation_panel.go
index 66e859be3..b2a202ddd 100644
--- a/pkg/gui/confirmation_panel.go
+++ b/pkg/gui/confirmation_panel.go
@@ -37,19 +37,24 @@ func (gui *Gui) closeConfirmationPrompt(g *gocui.Gui) error {
return g.DeleteView("confirmation")
}
-func (gui *Gui) getMessageHeight(message string, width int) int {
+func (gui *Gui) getMessageHeight(wrap bool, message string, width int) int {
lines := strings.Split(message, "\n")
lineCount := 0
- for _, line := range lines {
- lineCount += len(line)/width + 1
+ // if we need to wrap, calculate height to fit content within view's width
+ if wrap {
+ for _, line := range lines {
+ lineCount += len(line)/width + 1
+ }
+ } else {
+ lineCount = len(lines)
}
return lineCount
}
-func (gui *Gui) getConfirmationPanelDimensions(g *gocui.Gui, prompt string) (int, int, int, int) {
+func (gui *Gui) getConfirmationPanelDimensions(g *gocui.Gui, wrap bool, prompt string) (int, int, int, int) {
width, height := g.Size()
panelWidth := width / 2
- panelHeight := gui.getMessageHeight(prompt, panelWidth)
+ panelHeight := gui.getMessageHeight(wrap, prompt, panelWidth)
return width/2 - panelWidth/2,
height/2 - panelHeight/2 - panelHeight%2 - 1,
width/2 + panelWidth/2,
@@ -67,7 +72,7 @@ func (gui *Gui) createPromptPanel(g *gocui.Gui, currentView *gocui.View, title s
}
func (gui *Gui) prepareConfirmationPanel(currentView *gocui.View, title, prompt string) (*gocui.View, error) {
- x0, y0, x1, y1 := gui.getConfirmationPanelDimensions(gui.g, prompt)
+ x0, y0, x1, y1 := gui.getConfirmationPanelDimensions(gui.g, true, prompt)
confirmationView, err := gui.g.SetView("confirmation", x0, y0, x1, y1, 0)
if err != nil {
if err != gocui.ErrUnknownView {
@@ -84,10 +89,15 @@ func (gui *Gui) prepareConfirmationPanel(currentView *gocui.View, title, prompt
}
func (gui *Gui) onNewPopupPanel() {
- gui.g.SetViewOnBottom("commitMessage")
- gui.g.SetViewOnBottom("menu")
+ viewNames = []string{"commitMessage",
+ "credentials",
+ "menu"}
+ for _, viewName := range viewNames {
+ _, _ = gui.g.SetViewOnBottom(viewName)
+ }
}
+// it is very important that within this function we never include the original prompt in any error messages, because it may contain e.g. a user password
func (gui *Gui) createConfirmationPanel(g *gocui.Gui, currentView *gocui.View, title, prompt string, handleConfirm, handleClose func(*gocui.Gui, *gocui.View) error) error {
gui.onNewPopupPanel()
g.Update(func(g *gocui.Gui) error {
@@ -137,18 +147,27 @@ func (gui *Gui) createMessagePanel(g *gocui.Gui, currentView *gocui.View, title,
return gui.createConfirmationPanel(g, currentView, title, prompt, nil, nil)
}
-func (gui *Gui) createErrorPanel(g *gocui.Gui, message string) error {
- go func() {
- // when reporting is switched on this log call sometimes introduces
- // a delay on the error panel popping up. Here I'm adding a second wait
- // so that the error is logged while the user is reading the error message
- time.Sleep(time.Second)
- gui.Log.Error(message)
- }()
-
- // gui.Log.WithField("staging", "staging").Info("creating confirmation panel")
- currentView := g.CurrentView()
+// createSpecificErrorPanel allows you to create an error popup, specifying the
+// view to be focused when the user closes the popup, and a boolean specifying
+// whether we will log the error. If the message may include a user password,
+// this function is to be used over the more generic createErrorPanel, with
+// willLog set to false
+func (gui *Gui) createSpecificErrorPanel(message string, nextView *gocui.View, willLog bool) error {
+ if willLog {
+ go func() {
+ // when reporting is switched on this log call sometimes introduces
+ // a delay on the error panel popping up. Here I'm adding a second wait
+ // so that the error is logged while the user is reading the error message
+ time.Sleep(time.Second)
+ gui.Log.Error(message)
+ }()
+ }
+
colorFunction := color.New(color.FgRed).SprintFunc()
coloredMessage := colorFunction(strings.TrimSpace(message))
- return gui.createConfirmationPanel(g, currentView, gui.Tr.SLocalize("Error"), coloredMessage, nil, nil)
+ return gui.createConfirmationPanel(gui.g, nextView, gui.Tr.SLocalize("Error"), coloredMessage, nil, nil)
+}
+
+func (gui *Gui) createErrorPanel(g *gocui.Gui, message string) error {
+ return gui.createSpecificErrorPanel(message, g.CurrentView(), true)
}