2019-10-07 03:34:12 +02:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/jesseduffield/gocui"
|
2022-01-28 11:44:36 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/popup"
|
2019-10-07 03:34:12 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
2019-10-07 03:34:12 +02:00
|
|
|
|
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-03-16 15:07:48 +02:00
|
|
|
return gui.OSCommand.CreateFileWithContent(newDirFilePath, dirName)
|
2019-10-07 03:34:12 +02:00
|
|
|
}
|
|
|
|
|
2021-04-02 10:20:40 +02:00
|
|
|
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()
|
2019-10-07 03:34:12 +02:00
|
|
|
}
|
|
|
|
|
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()
|
2019-10-07 03:34:12 +02:00
|
|
|
}
|
|
|
|
|
2021-04-02 10:20:40 +02:00
|
|
|
func (gui *Gui) handleTopLevelReturn() error {
|
2020-08-21 13:05:21 +02:00
|
|
|
currentContext := gui.currentContext()
|
2020-08-24 00:26:42 +02:00
|
|
|
|
|
|
|
parentContext, hasParent := currentContext.GetParentContext()
|
|
|
|
if hasParent && currentContext != nil && parentContext != nil {
|
2020-08-21 13:05:21 +02:00
|
|
|
// TODO: think about whether this should be marked as a return rather than adding to the stack
|
2022-01-16 05:46:53 +02:00
|
|
|
return gui.c.PushContext(parentContext)
|
2020-08-21 13:05:21 +02:00
|
|
|
}
|
|
|
|
|
2020-08-22 03:44:03 +02:00
|
|
|
for _, mode := range gui.modeStatuses() {
|
|
|
|
if mode.isActive() {
|
2020-08-23 01:23:59 +02:00
|
|
|
return mode.reset()
|
2020-08-22 03:44:03 +02:00
|
|
|
}
|
2020-08-21 01:12:45 +02:00
|
|
|
}
|
|
|
|
|
2021-04-03 04:43:43 +02:00
|
|
|
repoPathStack := gui.RepoPathStack
|
2022-01-28 11:44:36 +02:00
|
|
|
if !repoPathStack.IsEmpty() {
|
|
|
|
path := repoPathStack.Pop()
|
2020-09-29 01:02:44 +02:00
|
|
|
|
2021-04-05 05:22:03 +02:00
|
|
|
return gui.dispatchSwitchToRepo(path, true)
|
2020-09-29 01:02:44 +02:00
|
|
|
}
|
|
|
|
|
2022-01-16 05:46:53 +02:00
|
|
|
if gui.c.UserConfig.QuitOnTopLevelReturn {
|
2020-08-15 09:23:16 +02:00
|
|
|
return gui.handleQuit()
|
2020-07-18 11:41:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-15 09:23:16 +02:00
|
|
|
func (gui *Gui) quit() error {
|
2019-10-07 03:34:12 +02:00
|
|
|
if gui.State.Updating {
|
2020-08-15 09:23:16 +02:00
|
|
|
return gui.createUpdateQuitConfirmation()
|
2019-10-07 03:34:12 +02:00
|
|
|
}
|
2020-08-12 12:30:28 +02:00
|
|
|
|
2022-01-16 05:46:53 +02:00
|
|
|
if gui.c.UserConfig.ConfirmOnQuit {
|
|
|
|
return gui.c.Ask(popup.AskOpts{
|
2022-01-28 11:44:36 +02:00
|
|
|
Title: "",
|
2022-01-16 05:46:53 +02:00
|
|
|
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
|
|
|
|
},
|
|
|
|
})
|
2019-10-07 03:34:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return gocui.ErrQuit
|
|
|
|
}
|