Skip to content

Commit 9bde789

Browse files
committed
port "dir cfg" to bash
* exit 2 from `dirp` signals that stdout contains path to config file
1 parent 93a4f18 commit 9bde789

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
build:
2-
@go build .
2+
@go build -ldflags "-s -w" .
33

44
run:
55
@go run .
66

77
test:
8-
@go test ./...
8+
@go test ./...

main.go

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"fmt"
45
"os"
56

67
dirp "github.com/avindra/dirp/src"
@@ -29,6 +30,9 @@ func main() {
2930
} else {
3031
dirp.PrintHook() // fish
3132
}
33+
} else if arg0 == "cfg" {
34+
fmt.Print(dirp.GetConfigPath())
35+
os.Exit(2)
3236
}
3337
return
3438
}

src/hook.go

+15-11
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,16 @@ if [[ $? -eq 0 ]]; then
1212
fi
1313
1414
function dir() {
15-
dir=$(dirp $@)
16-
if [[ -n $dir ]]; then
17-
echo "Switching to $dir... "
18-
pushd "$dir"
15+
response=$(dirp $@)
16+
status=$?
17+
if [[ -n $response ]]; then
18+
if [[ $status -eq 2 ]]; then
19+
$EDITOR "$response"
20+
return $?
21+
fi
22+
23+
echo "Switching to $response... "
24+
pushd "$response"
1925
fi
2026
}
2127
@@ -25,21 +31,19 @@ function dir() {
2531
// PrintHook emits shell code for Fish
2632
func PrintHook() {
2733
fmt.Println(`function dir
28-
if [ "$argv[1]" = "cfg" ]
29-
$EDITOR "$HOME/.config/dir/list"
34+
set response (dirp $argv)
35+
if [ $status = 2 ]
36+
$EDITOR "$response"
3037
return $status
31-
else
32-
# default
33-
set selection (dirp $argv)
3438
end
3539
36-
if [ "x$selection" = "x" ]
40+
if [ "x$response" = "x" ]
3741
echo -n "How are we doing @ "
3842
uptime
3943
return $status
4044
end
4145
42-
echo "Switching to $selection"
46+
echo "Switching to $response"
4347
pushd "$selection"
4448
end`)
4549
}

0 commit comments

Comments
 (0)