-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmarkrc
165 lines (136 loc) · 3.67 KB
/
markrc
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/bin/bash
export MARKPATH=$HOME/.marks
if [ ! -d "$MARKPATH" ]; then
mkdir -p "$MARKPATH"
fi
# Print error messges
function _err()
{
echo -e "\033[1;31m[ERROR] $@\033[0m" >&2
}
# Print notice messages
function _info()
{
echo "[INFO] $@"
}
# Jump to predefined mark
function jump()
{
local name=$1
if [ -z "$name" ]; then
cd # jump to home directory
elif [ "$name" = "-" ]; then
cd - # jump to last visited directory
elif [ -d "$name" ]; then
cd "$name" # jump to the directory directly
else # jump to mark directory
# Generate all complete results
local mark_list mark_len
# Pathname may contains space, so set the IFS before
IFS=$'\n' mark_list=($(compgen -f -- "$MARKPATH/$name"))
mark_len=${#mark_list[@]}
if [ $mark_len -eq 0 ]; then
# invalid mark name, print error
_err "No such mark: $name"
return 1
elif [ $mark_len -eq 1 ]; then
# only one result, jump right now
cd -P "${mark_list[0]}"
else # multiple results, show select menu
select name in ${mark_list[@]}; do
cd -P "$name" && break
done
fi
fi
}
# Mark a directory
function mark()
{
local cur="$(pwd)"
local name="$1"
if [ -z "$name" ]; then
# use basename as mark name by default
name=$(basename "$cur")
fi
# Convert mark name to lowercase
name=$(echo "$name" | tr A-Z a-z)
# Create a symbolic link to current directory
if ln -snf "$cur" "$MARKPATH/$name"; then
_info "Create mark '$name -> $cur' successfully"
return 0
else
_err "Create mark '$name -> $cur' failed"
return 1
fi
}
# Unmark a directory
function unmark()
{
local name="$1"
if [ -z "$name" ]; then # Default to unmark current directory
name=$(marks | awk -F'->' '{ if ($2 ~ "'"$(pwd)"'$") {print $1} }' | sed 's/ *$//g')
fi
if [ -z "$name" ]; then
_err "No mark points to current directory"
return 1
elif [ ! -L "$MARKPATH/$name" ]; then
# No valid mark found
_err "No such mark: $name"
return 1
else
local target="$(readlink $MARKPATH/$name)"
# Delete the symbolic link
if unlink "$MARKPATH/$name"; then
_info "Delete mark '$name -> $target' successfully"
return 0
else
_err "Delete mark '$name -> $target' failed"
return 1
fi
fi
}
# List the predefined marks
function marks()
{
local file
for file in $MARKPATH/*; do
if [ ! -L "$file" ]; then
continue
fi
if [ ! -e "$file" ]; then # clean broken symbolic links
unlink "$file"
else
printf "%-15s -> %s\n" "$(basename "$file")" "$(readlink "$file")"
fi
done
}
# Set auto bash completion for command jump or umark
function _completemarks()
{
local cword=${COMP_WORDS[COMP_CWORD]}
local mark_list
IFS=$'\n' mark_list=($(marks | awk -F '->' '{print $1}' | sed 's/ *$//g'))
# Only do complete on first argument
if [ $COMP_CWORD -eq 1 ]; then
COMPREPLY=($(compgen -W "${mark_list[*]}" -- "$cword"))
else
COMPREPLY=()
fi
return 0
}
# Create aliases for convenience
# Think: jump or go to
alias j=jump
alias g=jump
# Think: unmark or delete
alias u=unmark
alias d=unmark
# Think: mark or save a mark
alias m=mark
alias s=mark
# Think: list marks or print marks
alias l=marks
alias p=marks
# Set auto complete for mark-directory
complete -o default -o nospace -F _completemarks jump j g
complete -o nospace -F _completemarks unmark u d