1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/gui/quitting.go

87 lines
1.9 KiB
Go
Raw Normal View History

package gui
import (
"os"
"github.com/jesseduffield/gocui"
)
// when a user runs lazygit with the LAZYGIT_NEW_DIR_FILE env variable defined
// we will write the current directory to that file on exit so that their
// shell can then change to that directory. That means you don't get kicked
// back to the directory that you started with.
func (gui *Gui) recordCurrentDirectory() error {
if os.Getenv("LAZYGIT_NEW_DIR_FILE") == "" {
return nil
}
// determine current directory, set it in LAZYGIT_NEW_DIR_FILE
dirName, err := os.Getwd()
if err != nil {
return err
}
return gui.OSCommand.CreateFileWithContent(os.Getenv("LAZYGIT_NEW_DIR_FILE"), dirName)
}
func (gui *Gui) handleQuitWithoutChangingDirectory() error {
gui.State.RetainOriginalDir = true
2020-08-15 09:23:16 +02:00
return gui.quit()
}
2020-08-15 09:23:16 +02:00
func (gui *Gui) handleQuit() error {
gui.State.RetainOriginalDir = false
2020-08-15 09:23:16 +02:00
return gui.quit()
}
func (gui *Gui) handleTopLevelReturn() error {
currentContext := gui.currentContext()
parentContext, hasParent := currentContext.GetParentContext()
if hasParent && currentContext != nil && parentContext != nil {
// TODO: think about whether this should be marked as a return rather than adding to the stack
return gui.pushContext(parentContext)
}
for _, mode := range gui.modeStatuses() {
if mode.isActive() {
2020-08-23 01:23:59 +02:00
return mode.reset()
}
}
repoPathStack := gui.RepoPathStack
if len(repoPathStack) > 0 {
n := len(repoPathStack) - 1
path := repoPathStack[n]
gui.RepoPathStack = repoPathStack[:n]
return gui.dispatchSwitchToRepo(path, true)
}
2020-10-03 06:54:55 +02:00
if gui.Config.GetUserConfig().QuitOnTopLevelReturn {
2020-08-15 09:23:16 +02:00
return gui.handleQuit()
}
return nil
}
2020-08-15 09:23:16 +02:00
func (gui *Gui) quit() error {
if gui.State.Updating {
2020-08-15 09:23:16 +02:00
return gui.createUpdateQuitConfirmation()
}
2020-10-03 06:54:55 +02:00
if gui.Config.GetUserConfig().ConfirmOnQuit {
2020-08-15 08:38:16 +02:00
return gui.ask(askOpts{
title: "",
2020-10-04 02:00:48 +02:00
prompt: gui.Tr.ConfirmQuit,
2020-08-15 08:36:39 +02:00
handleConfirm: func() error {
return gocui.ErrQuit
},
})
}
return gocui.ErrQuit
}