github.com/andrewrech/lazygit@v0.8.1/pkg/gui/stash_panel.go (about) 1 package gui 2 3 import ( 4 "fmt" 5 6 "github.com/jesseduffield/gocui" 7 "github.com/jesseduffield/lazygit/pkg/commands" 8 "github.com/jesseduffield/lazygit/pkg/utils" 9 ) 10 11 // list panel functions 12 13 func (gui *Gui) getSelectedStashEntry(v *gocui.View) *commands.StashEntry { 14 selectedLine := gui.State.Panels.Stash.SelectedLine 15 if selectedLine == -1 { 16 return nil 17 } 18 19 return gui.State.StashEntries[selectedLine] 20 } 21 22 func (gui *Gui) handleStashEntrySelect(g *gocui.Gui, v *gocui.View) error { 23 if gui.popupPanelFocused() { 24 return nil 25 } 26 27 if _, err := gui.g.SetCurrentView(v.Name()); err != nil { 28 return err 29 } 30 stashEntry := gui.getSelectedStashEntry(v) 31 if stashEntry == nil { 32 return gui.renderString(g, "main", gui.Tr.SLocalize("NoStashEntries")) 33 } 34 if err := gui.focusPoint(0, gui.State.Panels.Stash.SelectedLine, len(gui.State.StashEntries), v); err != nil { 35 return err 36 } 37 go func() { 38 // doing this asynchronously cos it can take time 39 diff, _ := gui.GitCommand.GetStashEntryDiff(stashEntry.Index) 40 _ = gui.renderString(g, "main", diff) 41 }() 42 return nil 43 } 44 45 func (gui *Gui) refreshStashEntries(g *gocui.Gui) error { 46 g.Update(func(g *gocui.Gui) error { 47 gui.State.StashEntries = gui.GitCommand.GetStashEntries() 48 49 gui.refreshSelectedLine(&gui.State.Panels.Stash.SelectedLine, len(gui.State.StashEntries)) 50 51 isFocused := gui.g.CurrentView().Name() == "stash" 52 list, err := utils.RenderList(gui.State.StashEntries, isFocused) 53 if err != nil { 54 return err 55 } 56 57 v := gui.getStashView() 58 v.Clear() 59 fmt.Fprint(v, list) 60 61 if err := gui.resetOrigin(v); err != nil { 62 return err 63 } 64 return nil 65 }) 66 return nil 67 } 68 69 func (gui *Gui) handleStashNextLine(g *gocui.Gui, v *gocui.View) error { 70 if gui.popupPanelFocused() { 71 return nil 72 } 73 74 panelState := gui.State.Panels.Stash 75 gui.changeSelectedLine(&panelState.SelectedLine, len(gui.State.StashEntries), false) 76 77 if err := gui.resetOrigin(gui.getMainView()); err != nil { 78 return err 79 } 80 return gui.handleStashEntrySelect(gui.g, v) 81 } 82 83 func (gui *Gui) handleStashPrevLine(g *gocui.Gui, v *gocui.View) error { 84 if gui.popupPanelFocused() { 85 return nil 86 } 87 88 panelState := gui.State.Panels.Stash 89 gui.changeSelectedLine(&panelState.SelectedLine, len(gui.State.StashEntries), true) 90 91 if err := gui.resetOrigin(gui.getMainView()); err != nil { 92 return err 93 } 94 return gui.handleStashEntrySelect(gui.g, v) 95 } 96 97 // specific functions 98 99 func (gui *Gui) handleStashApply(g *gocui.Gui, v *gocui.View) error { 100 return gui.stashDo(g, v, "apply") 101 } 102 103 func (gui *Gui) handleStashPop(g *gocui.Gui, v *gocui.View) error { 104 return gui.stashDo(g, v, "pop") 105 } 106 107 func (gui *Gui) handleStashDrop(g *gocui.Gui, v *gocui.View) error { 108 title := gui.Tr.SLocalize("StashDrop") 109 message := gui.Tr.SLocalize("SureDropStashEntry") 110 return gui.createConfirmationPanel(g, v, title, message, func(g *gocui.Gui, v *gocui.View) error { 111 return gui.stashDo(g, v, "drop") 112 }, nil) 113 } 114 115 func (gui *Gui) stashDo(g *gocui.Gui, v *gocui.View, method string) error { 116 stashEntry := gui.getSelectedStashEntry(v) 117 if stashEntry == nil { 118 errorMessage := gui.Tr.TemplateLocalize( 119 "NoStashTo", 120 Teml{ 121 "method": method, 122 }, 123 ) 124 return gui.createErrorPanel(g, errorMessage) 125 } 126 if err := gui.GitCommand.StashDo(stashEntry.Index, method); err != nil { 127 gui.createErrorPanel(g, err.Error()) 128 } 129 gui.refreshStashEntries(g) 130 return gui.refreshFiles() 131 } 132 133 func (gui *Gui) handleStashSave(stashFunc func(message string) error) error { 134 if len(gui.trackedFiles()) == 0 && len(gui.stagedFiles()) == 0 { 135 return gui.createErrorPanel(gui.g, gui.Tr.SLocalize("NoTrackedStagedFilesStash")) 136 } 137 return gui.createPromptPanel(gui.g, gui.getFilesView(), gui.Tr.SLocalize("StashChanges"), func(g *gocui.Gui, v *gocui.View) error { 138 if err := stashFunc(gui.trimmedContent(v)); err != nil { 139 gui.createErrorPanel(g, err.Error()) 140 } 141 gui.refreshStashEntries(g) 142 return gui.refreshFiles() 143 }) 144 }