Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Mar 15, 2012
0 parents commit 3afbcdb
Showing 1 changed file with 122 additions and 0 deletions.
122 changes: 122 additions & 0 deletions bin/bm
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#!/usr/bin/env bash

# version

VERSION="0.0.1"

# bookmarks dir

DIR=~/Dropbox

# bookmarks file

BOOKMARKS=$DIR/bookmarks

#
# Output usage info
#

usage() {
cat <<EOF
Usage: bm [options] [cmd]
Commands:
# add a bookmark with the given url, description, and optional tags
add <url> [desc] [tag...]
# open the first bookmark matching <query>
open <query>
# search the bookmarks via full-text <query>
search <query>
# list bookmarks available
ls, list
# clear all bookmarks
clear
Options:
-V, --version output bm version
-h, --help output this help information
EOF
}

#
# Add a bookmark
#
# <url> [description] [tag ...]
#

save_bookmark() {
local url=$1
local desc=$2
local tags=${@:3}
echo
echo " Added bookmark"
echo
echo " url: $url"
echo " description: $desc"
echo " tags: $tags"
echo
echo "$url|$desc|$tags" >> $BOOKMARKS
}

#
# List all bookmarks
#

list_bookmarks() {
echo
cat $BOOKMARKS \
| awk '
BEGIN { FS = "|" }
{ printf " \033[36m%28s\033[0m %-30s \033[90m(%s)\033[0m\n", $1, $2, $3 }'
echo
}

#
# Search all bookmarks with <query>
#

search_bookmarks() {
list_bookmarks | grep $1
}

#
# Open first bookmark matching <query>
#

open_bookmark() {
cat $BOOKMARKS \
| grep $1 \
| cut -d '|' -f 1 \
| xargs open
}

#
# Remove all bookmarks
#

clear_bookmarks() {
rm $BOOKMARKS
}

# parse args

while test $# -ne 0; do
arg=$1; shift
case $arg in
-V|--version) echo $VERSION; exit ;;
-h|--help) usage; exit ;;
ls|list) list_bookmarks; exit ;;
search) search_bookmarks "$@"; exit ;;
open) open_bookmark "$@"; exit ;;
add) save_bookmark "$@"; exit ;;
clear) clear_bookmarks; exit ;;
esac
done

0 comments on commit 3afbcdb

Please sign in to comment.