From d05b845ef0a40a5663f09535553f7d42b5ff88b4 Mon Sep 17 00:00:00 2001 From: Frank Mayer Date: Wed, 2 Oct 2024 10:05:31 +0200 Subject: [PATCH] fix: zu chdir history --- internal/shell/parser/exe.go | 35 ++++++++++++++++++++------ internal/shell/parser/shell_unix.go | 2 +- internal/shell/parser/shell_windows.go | 2 +- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/internal/shell/parser/exe.go b/internal/shell/parser/exe.go index 4fdbfb5..a5851a3 100644 --- a/internal/shell/parser/exe.go +++ b/internal/shell/parser/exe.go @@ -236,6 +236,24 @@ func pushDir(p string) error { } } +func pushDirZu(p string) error { + cdHistoryMut.Lock() + defer cdHistoryMut.Unlock() + + wd, err := os.Getwd() + if err != nil { + return err + } + cdHistory = append(cdHistory, wd) + + if err := zu.To(p); err != nil { + return err + } else { + _ = os.Setenv("PWD", p) + return nil + } +} + func (e *exe) cd() error { switch len(e.Args) { case 0: @@ -251,14 +269,15 @@ func (e *exe) cd() error { } } -func (e *exe) z() error { +func (e *exe) zu() error { switch len(e.Args) { case 0: - return pushDir(env.GetUser().HomeDir) + return pushDirZu(env.GetUser().HomeDir) case 1: - if e.Args[0] == "-c" { + switch e.Args[0] { + case "-c", "--clear": return zu.Clear() - } else if e.Args[0] == "-l" { + case "-l", "--list": if l, err := zu.List(); err != nil { return err } else { @@ -267,11 +286,13 @@ func (e *exe) z() error { } return nil } - } else { - return zu.To(e.Args[0]) + case "-": + return popDir() + default: + return pushDirZu(e.Args[0]) } default: - return errors.New("z: too many operands") + return pushDirZu(strings.Join(e.Args, " ")) } } diff --git a/internal/shell/parser/shell_unix.go b/internal/shell/parser/shell_unix.go index 5a15b9b..e3f75ca 100644 --- a/internal/shell/parser/shell_unix.go +++ b/internal/shell/parser/shell_unix.go @@ -29,7 +29,7 @@ func (e *exe) internal(stdin io.Reader, stdout io.Writer, stderr io.Writer) (boo case "cd": return true, e.cd() case "zu": - return true, e.z() + return true, e.zu() case "smashfetch": return true, e.smashfetch(stdout) case "sleep": diff --git a/internal/shell/parser/shell_windows.go b/internal/shell/parser/shell_windows.go index 76cb426..06d2656 100644 --- a/internal/shell/parser/shell_windows.go +++ b/internal/shell/parser/shell_windows.go @@ -35,7 +35,7 @@ func (e *exe) internal(stdin io.Reader, stdout io.Writer, stderr io.Writer) (boo case "cd": return true, e.cd() case "zu": - return true, e.z() + return true, e.zu() case "smashfetch": return true, e.smashfetch(stdout) case "sleep":