-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathb.sh
More file actions
142 lines (123 loc) · 3.01 KB
/
b.sh
File metadata and controls
142 lines (123 loc) · 3.01 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
# b, a simple bookmarking system
# by Rocky Meza
BOOKMARKS_FILE=$HOME/.b_bookmarks
read -r -d '' USAGE <<HEREDOC
b, a simple bookmarking system
Installation:
Download or copy and paste b.sh and b_completion.sh
to a directory.
In your .zshrc or .bashrc file add:
source /path/to/b.sh
source /path/to/b_completion.sh
Close terminal
or
source .zshrc
or
source .bashrc
Usage:
b [bookmark] [directory]
b [bookmark] [file]
Options:
-h, --help Show this help screen
Notes:
If b is run with no arguments, it will list all of the bookmarks.
If it is given a bookmark that is a directory, it will attempt to cd into that bookmark.
If it is given a bookmark that is a file, it will attempt to open that bookmark.
If it is given a bookmark and directory or file, it will create that bookmark.
Examples:
$ b home /home/user
Added home,/home/user to bookmark list
$ b p /home/user/.profile
Added p,/home/user/.profile to bookmark list
$ b
List of bookmarks:
home,/home/user
p,/home/user/.profile
...
$ b home
will cd to the home directory
$ echo \`b home\`
/home/user
$ b p
will open ~/.profile
If your computer is a Mac, it will use the \`open\` command, otherwise it
will use the \`\$EDITOR\`.
HEREDOC
## private
# Creates the bookmark database if it doesn't exist.
__b_init()
{
if [[ ! -f "$BOOKMARKS_FILE" ]]; then
touch $BOOKMARKS_FILE
fi
}
# List all of the bookmarks in the database.
__b_list()
{
echo "List of bookmarks:"
# sorry
cat "$BOOKMARKS_FILE"
}
# Will add a bookmark to the database if it doesn't already exist. `add` will
# also expand the bookmark. You can use relative paths or things like `.`,
# `..`, and `~`.
__b_add()
{
__b_find_mark $1
if [[ -n "$mark" ]]; then
echo "That bookmark is already in use."
else
dir=`readlink -f $2`
echo "$1,$dir" >> $BOOKMARKS_FILE
echo "Added $1,$dir to bookmarks list"
fi
}
# Will `cd` to the bookmarked directory. If no bookmark matches the one
# specified, it will print an error.
open_command="$EDITOR"
if [[ `uname` = "Darwin" ]]; then
open_command=open
fi
__b_cd()
{
__b_find_mark "$1"
if [[ -n "$mark" ]]; then
dir=$(echo $mark | sed 's/^[^,]*,\(.*\)/\1/')
# if not a tty, print to stdout
if [ ! -t 1 ] ; then
echo -n "$dir"
elif [[ -d $dir ]]; then
cd $dir
if [[ -f "$dir/.b_hook" ]]; then
source "$dir/.b_hook"
fi
else
$open_command "$dir"
fi
else
echo "That bookmark does not exist." >&2
fi
}
__b_find_mark()
{
mark=$(grep "^$1," < $BOOKMARKS_FILE)
}
## public
# This is the entry point. It parses the arguments and then delegates to other
# functions.
b()
{
if [[ "$#" -eq 1 ]]; then
if [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
echo "$USAGE"
else
__b_cd $1
fi
elif [[ "$#" -eq 2 ]]; then
__b_add $1 $2
else
__b_list
fi
}
# main
__b_init