-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgd
More file actions
146 lines (131 loc) · 3.32 KB
/
Copy pathgd
File metadata and controls
146 lines (131 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/bin/zsh
gd() {
local GOTOUNIXDIR="$HOME/.gotounixlinks"
local num_par=$#
# Initialize the directory file if it doesn't exist
if [[ ! -f $GOTOUNIXDIR ]]; then
print "File does not exist. Creating file: $GOTOUNIXDIR"
touch $GOTOUNIXDIR
fi
# Print all saved paths with line numbers
print_path() {
cat -n $GOTOUNIXDIR
}
# Check if the provided path is valid
valid_path() {
local dir_path=$1
if [[ -n "$dir_path" ]] && [[ -d "$dir_path" ]]; then
return 0 # True
fi
print "No such file or path: $dir_path"
return 1
}
# Search for a path in the saved file
search_path() {
local dir_path=$1
grep -q -i "$dir_path" $GOTOUNIXDIR
return $?
}
# Add a path to the saved file
add_path() {
local to_add description
if [[ $# -eq 0 ]]; then
to_add=$PWD
else
to_add=$1
shift
description=$*
fi
if search_path "$to_add"; then
print "$to_add already remembered."
elif valid_path "$to_add"; then
print "Adding: $to_add"
if [[ -n "$description" ]]; then
print "$to_add $description" >> $GOTOUNIXDIR
else
print "$to_add" >> $GOTOUNIXDIR
fi
fi
print_path
}
# Go to a saved path
goto_path() {
local dir_str=$1
local dir_path
if search_path "$dir_str"; then
dir_path=$(grep -m 1 -i "$dir_str" $GOTOUNIXDIR | cut -d" " -f1)
pushd "$dir_path" || return 1
else
print "Path containing '$dir_str' not found."
return 1
fi
}
# Show help information
show_help() {
print "Usage: gd [-a|-g|-l|-h] [PATH|PATTERN|NUMBER]
Examples:
gd -a /tmp [description] # Add /tmp to remembered directories with optional description
gd -a # Add current directory to remembered directories
gd -g pattern # Go to directory matching pattern
gd -l # List all remembered directories
gd number # Go to directory at line number
gd pattern # Go to directory matching pattern
gd -h # Show this help"
}
# If no arguments provided, just list paths
if [[ $num_par -eq 0 ]]; then
print_path
return 0
fi
# Check if first argument is a number (shortcut to goto line number)
if [[ $1 =~ ^[0-9]+$ ]]; then
local line_num=$1
local dir_path=$(sed -n "${line_num}p" $GOTOUNIXDIR | cut -d" " -f1)
if [[ -n "$dir_path" ]]; then
pushd "$dir_path" || return 1
else
print "No directory at line $line_num"
return 1
fi
return 0
fi
# Process options
local OPTIND=1
while getopts ":aglh" OPTION; do
case ${OPTION} in
a)
shift $((OPTIND-1))
add_path "$@"
return 0
;;
g)
shift $((OPTIND-1))
if [[ -z "$1" ]]; then
print "Error: -g requires a pattern argument"
return 1
fi
goto_path "$1"
return $?
;;
l)
print_path
return 0
;;
h)
show_help
return 0
;;
\?)
print -u2 "Invalid option: -$OPTARG"
show_help
return 2
;;
esac
done
# If we get here without returning, assume the first argument is a pattern to search
shift $((OPTIND-1))
if [[ -n "$1" ]]; then
goto_path "$1"
return $?
fi
}