summaryrefslogtreecommitdiffstats
path: root/pkg/gui/confirmation_panel.go
blob: 15c615bb52c5abb9d80953ccbe3abdbff35e96b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// lots of this has been directly ported from one of the example files, will brush up later

// Copyright 2014 The gocui Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package gui

import (
	"strings"
	"time"

	"github.com/fatih/color"
	"github.com/jesseduffield/gocui"
	"github.com/jesseduffield/lazygit/pkg/theme"
)

type createPopupPanelOpts struct {
	returnToView        *gocui.View
	hasLoader           bool
	returnFocusOnClose  bool
	editable            bool
	title               string
	prompt              string
	handleConfirm       func() error
	handleConfirmPrompt func(string) error
	handleClose         func() error
}

type askOpts struct {
	returnToView       *gocui.View
	returnFocusOnClose bool
	title              string
	prompt             string
	handleConfirm      func() error
	handleClose        func() error
}

func (gui *Gui) createLoaderPanel(currentView *gocui.View, prompt string) error {
	return gui.createPopupPanel(createPopupPanelOpts{
		returnToView:       currentView,
		prompt:             prompt,
		hasLoader:          true,
		returnFocusOnClose: true,
	})
}

func (gui *Gui) ask(opts askOpts) error {
	return gui.createPopupPanel(createPopupPanelOpts{
		returnToView:       opts.returnToView,
		title:              opts.title,
		prompt:             opts.prompt,
		returnFocusOnClose: opts.returnFocusOnClose,
		handleConfirm:      opts.handleConfirm,
		handleClose:        opts.handleClose,
	})
}

func (gui *Gui) prompt(currentView *gocui.View, title string, initialContent string, handleConfirm func(string) error) error {
	return gui.createPopupPanel(createPopupPanelOpts{
		returnToView:        currentView,
		title:               title,
		prompt:              initialContent,
		returnFocusOnClose:  true,
		editable:            true,
		handleConfirmPrompt: handleConfirm,
	})
}

func (gui *Gui) wrappedConfirmationFunction(function func() error, returnFocusOnClose bool) func(*gocui.Gui, *gocui.View) error {
	return func(g *gocui.Gui, v *gocui.View) error {

		if function != nil {
			if err := function(); err != nil {
				return err
			}
		}

		return gui.closeConfirmationPrompt(g, returnFocusOnClose)
	}
}

func (gui *Gui) wrappedPromptConfirmationFunction(function func(string) error, returnFocusOnClose bool) func(*gocui.Gui, *gocui.View) error {
	return func(g *gocui.Gui, v *gocui.View) error {

		if function != nil {
			if err := function(v.Buffer()); err != nil {
				return err
			}
		}

		return gui.closeConfirmationPrompt(g, returnFocusOnClose)
	}
}

func (gui *Gui) closeConfirmationPrompt(g *gocui.Gui, returnFocusOnClose bool) error {
	view, err := g.View("confirmation")
	if err != nil {
		return nil // if it's already been closed we can just return
	}
	view.Editable = false
	if returnFocusOnClose {
		if err := gui.returnFocus(g, view); err != nil {
			panic(err)
		}
	}
	g.DeleteKeybinding("confirmation", gocui.KeyEnter, gocui.ModNone)
	g.DeleteKeybinding("confirmation", gocui.KeyEsc, gocui.ModNone)
	return g.DeleteView("confirmation")
}

func (gui *Gui) getMessageHeight(wrap bool, message string, width int) int {
	lines := strings.Split(message, "\n")
	lineCount := 0
	// 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, wrap bool, prompt string) (int, int, int, int) {
	width, height := g.Size()
	// we want a minimum width up to a point, then we do it based on ratio.
	panelWidth := 4 * width / 7
	minWidth := 80
	if panelWidth < minWidth {
		if width-2 < minWidth {
			panelWidth = width - 2
		} else {
			panelWidth = minWidth
		}
	}
	panelHeight := gui.getMessageHeight(wrap, prompt, panelWidth)
	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 (gui *Gui) prepareConfirmationPanel(currentView *gocui.View, title, prompt string, hasLoader bool) (*gocui.View, error) {
	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.Error() != "unknown view" {
			return nil, err
		}
		confirmationView.HasLoader = hasLoader
		if hasLoader {
			gui.g.StartTicking()
		}
		confirmationView.Title = title
		confirmationView.Wrap = true
		confirmationView.FgColor = theme.GocuiDefaultTextColor
	}
	gui.g.Update(func(g *gocui.Gui) error {
		return gui.switchFocus(currentView, confirmationView)
	})
	return confirmationView, nil
}

func (gui *Gui) onNewPopupPanel() {
	viewNames := []string{"commitMessage",
		"credentials",
		"menu"}
	for _, viewName := range viewNames {
		_, _ = gui.g.SetViewOnBottom(viewName)
	}
}

func (gui *Gui) createPopupPanel(opts createPopupPanelOpts) error {
	gui.onNewPopupPanel()
	gui.g.Update(func(g *gocui.Gui) error {
		// delete the existing confirmation panel if it exists
		if view, _ := g.View("confirmation"); view != nil {
			if err := gui.closeConfirmationPrompt(g, true); err != nil {
				gui.Log.Error(err)
			}
		}
		confirmationView, err := gui.prepareConfirmationPanel(opts.returnToView, opts.title, opts.prompt, opts.hasLoader)
		if err != nil {
			return err
		}
		confirmationView.Editable = opts.editable
		if opts.editable {
			go func() {
				// TODO: remove this wait (right now if you remove it the EditGotoToEndOfLine method doesn't seem to work)
				time.Sleep(time.Millisecond)
				gui.g.Update(func(g *gocui.Gui) error {
					confirmationView.EditGotoToEndOfLine()
					return nil
				})
			}()
		}

		gui.renderString("confirmation", opts.prompt)
		return gui