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
2.0 KiB
Go
Raw Normal View History

package gui
import (
"os"
"github.com/jesseduffield/gocui"
2022-01-29 10:09:20 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
// 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 {
// determine current directory, set it in LAZYGIT_NEW_DIR_FILE
dirName, err := os.Getwd()
if err != nil {
return err
}
2022-03-15 15:12:26 +02:00
return gui.recordDirectory(dirName)
}
2022-03-15 15:12:26 +02:00
func (gui *Gui) recordDirectory(dirName string) error {
2022-03-16 15:07:48 +02:00
newDirFilePath := os.Getenv("LAZYGIT_NEW_DIR_FILE")
if newDirFilePath == "" {
2022-03-15 15:12:26 +02:00
return nil
}
2022-02-06 04:42:17 +02:00
return gui.os.CreateFileWithContent(newDirFilePath, dirName)
}
func (gui *Gui) handleQuitWithoutChangingDirectory() error {
2022-01-28 11:44:36 +02:00
gui.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 {
2022-01-28 11:44:36 +02:00
gui.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.c.PushContext(parentContext)
}
for _, mode := range gui.modeStatuses() {
if mode.isActive() {
2020-08-23 01:23:59 +02:00
return mode.reset()
}
}
repoPathStack := gui.RepoPathStack
2022-01-28 11:44:36 +02:00
if !repoPathStack.IsEmpty() {
path := repoPathStack.Pop()
return gui.dispatchSwitchToRepo(path, true)
}
if gui.c.UserConfig.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()
}
if gui.c.UserConfig.ConfirmOnQuit {
return gui.c.Confirm(types.ConfirmOpts{
2022-01-28 11:44:36 +02:00
Title: "",
Prompt: gui.c.Tr.ConfirmQuit,
2022-01-28 11:44:36 +02:00
HandleConfirm: func() error {
2020-08-15 08:36:39 +02:00
return gocui.ErrQuit
},
})
}
return gocui.ErrQuit
}