diff --git a/COPYING b/COPYING index b1d2724..751e9dc 100644 --- a/COPYING +++ b/COPYING @@ -1,4 +1,4 @@ -Copyright (c) 2017-2021 Avindra Goolcharan +Copyright (c) 2017-2025 Avindra Goolcharan Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/main.go b/main.go index ef5b200..fa975c1 100644 --- a/main.go +++ b/main.go @@ -33,6 +33,8 @@ func main() { switch args[1] { case "fish": dirp.PrintFishHook() + case "pwsh": + dirp.PrintPwshHook() case "es": dirp.PrintEsHook() case "rc": diff --git a/src/hook.go b/src/hook.go index 5f5bb2b..712b1e5 100644 --- a/src/hook.go +++ b/src/hook.go @@ -1,113 +1,39 @@ package dirp import ( - "fmt" - "strings" + "embed" + "os" ) +//go:embed hooks/* +var folder embed.FS + // PrintHook emits shell code for Bash, ZSH, sh, BusyBox, etc func PrintHook() { - // 1) Remove existing "dir" aliases, if any exist - // 2) Detect and prefer pushd over cd - // 3) Provide "dir" function - // 4) export dir function for Bash users - - // important: do not use $status as a variable - // because it's a readonly reserved special var in ZSH - fmt.Println(` -unalias dir >/dev/null 2>&1 - -_DIRP_CD=cd -type pushd >/dev/null 2>&1 && _DIRP_CD=pushd - -dir () { - stdout=$(dirp $@) - stat=$? - if [ -n $stdout ]; then - if [ $stat -eq 2 ]; then - $EDITOR "$stdout" - return $? - fi - - echo "Switching to $stdout... " - $_DIRP_CD "$stdout" - fi - } - - alias d=dir - alias d.="dir ." - alias d..="dir .." - - export -f dir >/dev/null 2>&1 - `) + hook, _ := folder.ReadFile("hooks/hook.sh") + os.Stdout.Write(hook) } // PrintFishHook emits shell code for Fish func PrintFishHook() { - fmt.Println(` - - function dir - set stdout (dirp $argv) - if [ $status = 2 ] - $EDITOR "$stdout" - return $status - end - - if [ "x$stdout" = "x" ] - echo -n "How are we doing @ " - uptime - return $status - end - - echo "Switching to $stdout" - pushd "$stdout" - end - - abbr -a -g d dir - abbr -a -g d. dir . - abbr -a -g d.. dir .. - - `) + hook, _ := folder.ReadFile("hooks/hook.fish") + os.Stdout.Write(hook) } // PrintRcHook emits code for rc, the plan 9 shell func PrintRcHook() { - src := `fn dir { - stdout=` + "`" + `{dirp $*}; - if (~ $bqstatus 2 ) { - $EDITOR $stdout; - return $status; - }; - - if (~ "x$stdout" "x" ) { - echo -n How are we doing @; - uptime; - return $status; - }; - - echo Switching to $stdout; - cd $stdout - }` - - fmt.Println(strings.ReplaceAll(src, "\t", " ")) + hook, _ := folder.ReadFile("hooks/hook.rc") + os.Stdout.Write(hook) } // PrintEsHook emits code for es, a shell based on rc func PrintEsHook() { - fmt.Println(`fn dir { - stdout=` + "`" + `{dirp $*}; - if {~ $bqstatus 2 } { - $EDITOR $stdout; - return $status; - }; - - if {~ "x$stdout" "x" } { - echo -n "How are we doing @"; - uptime; - return $status; - }; - - echo Switching to $stdout; - cd $stdout -}`) + hook, _ := folder.ReadFile("hooks/hook.es") + os.Stdout.Write(hook) +} + +// PrintPwshHook emits code for PowerShell +func PrintPwshHook() { + hook, _ := folder.ReadFile("hooks/hook.pwsh") + os.Stdout.Write(hook) } diff --git a/src/hooks/hook.es b/src/hooks/hook.es new file mode 100644 index 0000000..7e44556 --- /dev/null +++ b/src/hooks/hook.es @@ -0,0 +1,16 @@ +fn dir { + stdout=` + "`" + `{dirp $*}; + if {~ $bqstatus 2 } { + $EDITOR $stdout; + return $status; + }; + + if {~ "x$stdout" "x" } { + echo -n "How are we doing @"; + uptime; + return $status; + }; + + echo Switching to $stdout; + cd $stdout +} \ No newline at end of file diff --git a/src/hooks/hook.fish b/src/hooks/hook.fish new file mode 100644 index 0000000..7874bfb --- /dev/null +++ b/src/hooks/hook.fish @@ -0,0 +1,20 @@ +function dir + set stdout (dirp $argv) + if [ $status = 2 ] + $EDITOR "$stdout" + return $status + end + + if [ "x$stdout" = x ] + echo -n "How are we doing @ " + uptime + return $status + end + + echo "Switching to $stdout" + pushd "$stdout" +end + +abbr -a -g d dir +abbr -a -g d. dir . +abbr -a -g d.. dir .. diff --git a/src/hooks/hook.pwsh b/src/hooks/hook.pwsh new file mode 100644 index 0000000..0dfd5e0 --- /dev/null +++ b/src/hooks/hook.pwsh @@ -0,0 +1,16 @@ +Remove-Alias dir + +function dir { + $dir = dirp @args + + if ($LASTEXITCODE -eq 2) { + notepad $dir + } + + pushd $dir +} + + +Set-Alias -Name d -Value dir +#Set-Alias -Name d. -Value dir . +#Set-Alias -Name d.. -Value dir .. \ No newline at end of file diff --git a/src/hooks/hook.rc b/src/hooks/hook.rc new file mode 100644 index 0000000..476ca4e --- /dev/null +++ b/src/hooks/hook.rc @@ -0,0 +1,16 @@ +fn dir { + stdout=` + "`" + `{dirp $*}; + if (~ $bqstatus 2 ) { + $EDITOR $stdout; + return $status; + }; + + if (~ "x$stdout" "x" ) { + echo -n How are we doing @; + uptime; + return $status; + }; + + echo Switching to $stdout; + cd $stdout +} \ No newline at end of file diff --git a/src/hooks/hook.sh b/src/hooks/hook.sh new file mode 100644 index 0000000..d86af6a --- /dev/null +++ b/src/hooks/hook.sh @@ -0,0 +1,32 @@ +# 1) Remove existing "dir" aliases, if any exist +# 2) Detect and prefer pushd over cd +# 3) Provide "dir" function +# 4) export dir function for Bash users + +# important: do not use $status as a variable +# because it's a readonly reserved special var in ZSH + +unalias dir >/dev/null 2>&1 + +_DIRP_CD=cd +type pushd >/dev/null 2>&1 && _DIRP_CD=pushd + +dir () { + stdout=$(dirp $@) + stat=$? + if [ -n $stdout ]; then + if [ $stat -eq 2 ]; then + $EDITOR "$stdout" + return $? + fi + + echo "Switching to $stdout... " + $_DIRP_CD "$stdout" + fi +} + +alias d=dir +alias d.="dir ." +alias d..="dir .." + +export -f dir >/dev/null 2>&1 \ No newline at end of file diff --git a/src/proc.go b/src/proc.go index 8969fe4..1741231 100644 --- a/src/proc.go +++ b/src/proc.go @@ -4,6 +4,7 @@ import ( "io" "os" "os/exec" + "path/filepath" "strings" ) @@ -16,26 +17,24 @@ func IsDir(path string) bool { return fileInfo.IsDir() } -// execFindDir at the given path -// "-mindepth 1" is used to excl working dir -// https://superuser.com/a/590470/59068 -func execFindDir(path string) string { - routine := []string{"find", path, "-mindepth", "1", "-maxdepth", "1", "-type", "d"} - result, err := execWith(strings.NewReader(""), routine) +// FindDirs at path. Used by driller + +func FindDirs(path string) ConfigSelection { + fullPath, _ := filepath.Abs(path) + dir, err := os.Open(fullPath) if err != nil { - return "" + return nil } - return result -} + names, _ := dir.Readdirnames(-1) + defer dir.Close() -// FindDirs at path -func FindDirs(path string) ConfigSelection { - dirs := strings.Split(execFindDir(path), "\n") - cfg := make(ConfigSelection, len(dirs)) - for k := range dirs { - D := dirs[k] - cfg[D] = D + cfg := make(ConfigSelection) + for _, name := range names { + resolvedPath := filepath.Join(fullPath, name) + if IsDir(resolvedPath) { + cfg[resolvedPath] = resolvedPath + } } return cfg }