File: workspace_reset_controller.go

package info (click to toggle)
lazygit 0.57.0%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,748 kB
  • sloc: sh: 153; makefile: 76
file content (282 lines) | stat: -rw-r--r-- 7,621 bytes parent folder | download
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package controllers

import (
	"bytes"
	"errors"
	"fmt"
	"math"
	"math/rand"
	"time"

	"github.com/jesseduffield/gocui"
	"github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers"
	"github.com/jesseduffield/lazygit/pkg/gui/style"
	"github.com/jesseduffield/lazygit/pkg/gui/types"
)

// this is in its own file given that the workspace controller file is already quite long

func (self *FilesController) createResetMenu() error {
	red := style.FgRed

	nukeStr := "git reset --hard HEAD && git clean -fd"
	if len(self.c.Model().Submodules) > 0 {
		nukeStr = fmt.Sprintf("%s (%s)", nukeStr, self.c.Tr.AndResetSubmodules)
	}

	menuItems := []*types.MenuItem{
		{
			LabelColumns: []string{
				self.c.Tr.DiscardAllChangesToAllFiles,
				red.Sprint(nukeStr),
			},
			OnPress: func() error {
				self.c.Confirm(
					types.ConfirmOpts{
						Title:  self.c.Tr.Actions.NukeWorkingTree,
						Prompt: self.c.Tr.NukeTreeConfirmation,
						HandleConfirm: func() error {
							self.c.LogAction(self.c.Tr.Actions.NukeWorkingTree)
							if err := self.c.Git().WorkingTree.ResetAndClean(); err != nil {
								return err
							}

							if self.c.UserConfig().Gui.AnimateExplosion {
								self.animateExplosion()
							}

							self.c.Refresh(
								types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}},
							)
							return nil
						},
					})
				return nil
			},
			Key:     'x',
			Tooltip: self.c.Tr.NukeDescription,
		},
		{
			LabelColumns: []string{
				self.c.Tr.DiscardAnyUnstagedChanges,
				red.Sprint("git checkout -- ."),
			},
			OnPress: func() error {
				self.c.LogAction(self.c.Tr.Actions.DiscardUnstagedFileChanges)
				if err := self.c.Git().WorkingTree.DiscardAnyUnstagedFileChanges(); err != nil {
					return err
				}

				self.c.Refresh(
					types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}},
				)
				return nil
			},
			Key: 'u',
		},
		{
			LabelColumns: []string{
				self.c.Tr.DiscardUntrackedFiles,
				red.Sprint("git clean -fd"),
			},
			OnPress: func() error {
				self.c.LogAction(self.c.Tr.Actions.RemoveUntrackedFiles)
				if err := self.c.Git().WorkingTree.RemoveUntrackedFiles(); err != nil {
					return err
				}

				self.c.Refresh(
					types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}},
				)
				return nil
			},
			Key: 'c',
		},
		{
			LabelColumns: []string{
				self.c.Tr.DiscardStagedChanges,
				red.Sprint("stash staged and drop stash"),
			},
			Tooltip: self.c.Tr.DiscardStagedChangesDescription,
			OnPress: func() error {
				self.c.LogAction(self.c.Tr.Actions.RemoveStagedFiles)
				if !self.c.Helpers().WorkingTree.IsWorkingTreeDirtyExceptSubmodules() {
					return errors.New(self.c.Tr.NoTrackedStagedFilesStash)
				}
				if err := self.c.Git().Stash.SaveStagedChanges("[lazygit] tmp stash"); err != nil {
					return err
				}
				if err := self.c.Git().Stash.DropNewest(); err != nil {
					return err
				}

				self.c.Refresh(
					types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}},
				)
				return nil
			},
			Key: 'S',
		},
		{
			LabelColumns: []string{
				self.c.Tr.SoftReset,
				red.Sprint("git reset --soft HEAD"),
			},
			OnPress: func() error {
				self.c.LogAction(self.c.Tr.Actions.SoftReset)
				if err := self.c.Git().WorkingTree.ResetSoft("HEAD"); err != nil {
					return err
				}

				self.c.Refresh(
					types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}},
				)
				return nil
			},
			Key: 's',
		},
		{
			LabelColumns: []string{
				"mixed reset",
				red.Sprint("git reset --mixed HEAD"),
			},
			OnPress: func() error {
				self.c.LogAction(self.c.Tr.Actions.MixedReset)
				if err := self.c.Git().WorkingTree.ResetMixed("HEAD"); err != nil {
					return err
				}

				self.c.Refresh(
					types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}},
				)
				return nil
			},
			Key: 'm',
		},
		{
			LabelColumns: []string{
				self.c.Tr.HardReset,
				red.Sprint("git reset --hard HEAD"),
			},
			OnPress: func() error {
				return self.c.ConfirmIf(helpers.IsWorkingTreeDirtyExceptSubmodules(self.c.Model().Files, self.c.Model().Submodules),
					types.ConfirmOpts{
						Title:  self.c.Tr.Actions.HardReset,
						Prompt: self.c.Tr.ResetHardConfirmation,
						HandleConfirm: func() error {
							self.c.LogAction(self.c.Tr.Actions.HardReset)
							if err := self.c.Git().WorkingTree.ResetHard("HEAD"); err != nil {
								return err
							}

							self.c.Refresh(
								types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}},
							)
							return nil
						},
					})
			},
			Key: 'h',
		},
	}

	return self.c.Menu(types.CreateMenuOptions{Title: "", Items: menuItems})
}

func (self *FilesController) animateExplosion() {
	self.Explode(self.c.Views().Files, func() {
		self.c.PostRefreshUpdate(self.c.Contexts().Files)
	})
}

// Animates an explosion within the view by drawing a bunch of flamey characters
func (self *FilesController) Explode(v *gocui.View, onDone func()) {
	width := v.InnerWidth()
	height := v.InnerHeight()
	styles := []style.TextStyle{
		style.FgLightWhite.SetBold(),
		style.FgYellow.SetBold(),
		style.FgRed.SetBold(),
		style.FgBlue.SetBold(),
		style.FgBlack.SetBold(),
	}

	self.c.OnWorker(func(_ gocui.Task) error {
		max := 25
		for i := range max {
			image := getExplodeImage(width, height, i, max)
			style := styles[(i*len(styles)/max)%len(styles)]
			coloredImage := style.Sprint(image)
			self.c.OnUIThread(func() error {
				v.SetOrigin(0, 0)
				v.SetContent(coloredImage)
				return nil
			})
			time.Sleep(time.Millisecond * 20)
		}
		self.c.OnUIThread(func() error {
			v.Clear()
			onDone()
			return nil
		})
		return nil
	})
}

// Render an explosion in the given bounds.
func getExplodeImage(width int, height int, frame int, max int) string {
	// Predefine the explosion symbols
	explosionChars := []rune{'*', '.', '@', '#', '&', '+', '%'}

	// Initialize a buffer to build our string
	var buf bytes.Buffer

	// Initialize RNG seed
	random := rand.New(rand.NewSource(time.Now().UnixNano()))

	// calculate the center of explosion
	centerX, centerY := width/2, height/2

	// calculate the max radius (hypotenuse of the view)
	maxRadius := math.Hypot(float64(centerX), float64(centerY))

	// calculate frame as a proportion of max, apply square root to create the non-linear effect
	progress := math.Sqrt(float64(frame) / float64(max))

	// calculate radius of explosion according to frame and max
	radius := progress * maxRadius * 2

	// introduce a new radius for the inner boundary of the explosion (the shockwave effect)
	var innerRadius float64
	if progress > 0.5 {
		innerRadius = (progress - 0.5) * 2 * maxRadius
	}

	for y := range height {
		for x := range width {
			// calculate distance from center, scale x by 2 to compensate for character aspect ratio
			distance := math.Hypot(float64(x-centerX), float64(y-centerY)*2)

			// if distance is less than radius and greater than innerRadius, draw explosion char
			if distance <= radius && distance >= innerRadius {
				// Make placement random and less likely as explosion progresses
				if random.Float64() > progress {
					// Pick a random explosion char
					char := explosionChars[random.Intn(len(explosionChars))]
					buf.WriteRune(char)
				} else {
					buf.WriteRune(' ')
				}
			} else {
				// If not explosion, then it's empty space
				buf.WriteRune(' ')
			}
		}
		// End of line
		if y < height-1 {
			buf.WriteRune('\n')
		}
	}

	return buf.String()
}