Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion COPYING
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2017-2021 Avindra Goolcharan <[email protected]>
Copyright (c) 2017-2025 Avindra Goolcharan <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func main() {
switch args[1] {
case "fish":
dirp.PrintFishHook()
case "pwsh":
dirp.PrintPwshHook()
case "es":
dirp.PrintEsHook()
case "rc":
Expand Down
112 changes: 19 additions & 93 deletions src/hook.go
Original file line number Diff line number Diff line change
@@ -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)
}
16 changes: 16 additions & 0 deletions src/hooks/hook.es
Original file line number Diff line number Diff line change
@@ -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
}
20 changes: 20 additions & 0 deletions src/hooks/hook.fish
Original file line number Diff line number Diff line change
@@ -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 ..
16 changes: 16 additions & 0 deletions src/hooks/hook.pwsh
Original file line number Diff line number Diff line change
@@ -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 ..
16 changes: 16 additions & 0 deletions src/hooks/hook.rc
Original file line number Diff line number Diff line change
@@ -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
}
32 changes: 32 additions & 0 deletions src/hooks/hook.sh
Original file line number Diff line number Diff line change
@@ -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
31 changes: 15 additions & 16 deletions src/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io"
"os"
"os/exec"
"path/filepath"
"strings"
)

Expand All @@ -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
}
Expand Down
Loading