summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam Rankin <william@bydasein.com>2020-06-13 18:44:58 +1000
committerWilliam Rankin <william@bydasein.com>2020-06-13 18:44:58 +1000
commitfb0047488ea86759429a578c6469586791b45203 (patch)
treeaf1717eeed6c8da3d4d3071a7bd61ad08721168f
downloadblip-fb0047488ea86759429a578c6469586791b45203.zip
Initial
-rwxr-xr-xblip200
1 files changed, 200 insertions, 0 deletions
diff --git a/blip b/blip
new file mode 100755
index 0000000..0c34df7
--- /dev/null
+++ b/blip
@@ -0,0 +1,200 @@
+#!/usr/bin/env bash
+
+# umask
+# set -o pipefail
+
+VERSION=0.1.0
+
+if [[ -n $BOOKMARKS_HOME ]]
+then PREFIX="$BOOKMARKS_HOME"
+elif [[ -r $HOME/.bookmarks ]]
+then PREFIX="$HOME/.bookmarks"
+else PREFIX="${XDG_CONFIG_HOME:-$HOME/.config}/bookmarks"
+fi
+
+PROGRAM="${0##*/}"
+FILE="${BOOKMARKS_FILE:-$PREFIX/bookmarks.tsv}"
+BROWSE_LIMIT="${BOOKMARKS_BROWSE_LIMIT:-12}"
+SYSTEM="$(uname -s)"
+
+case "$SYSTEM" in
+ (Linux) BROWSE="$(which xdg-open)" ;;
+ (Darwin) BROWSE="$(which open)" ;;
+ (*) echo "Unsupported system"
+ exit 2;;
+esac
+
+function blip_help {
+ cat <<-EOF
+=======================================
+ blip v$VERSION
+
+ bookmarked links in plaintext
+
+ William Rankin william@bydasein.com
+=======================================
+
+Usage: blip [COMMAND] QUERY
+
+ QUERY can be a match string or a bookmark number.
+
+Commands:
+
+ $PROGRAM init [DIR]
+ initialize an empty bookmarks repository in
+ $PREFIX or DIR
+
+ $PROGRAM add [-c | URL] [TAG TAG ...]
+ add URL to bookmarks file
+
+ $PROGRAM list|find [-t -u] QUERY
+ list bookmarks matching QUERY
+
+ $PROGRAM open|browse QUERY
+ open bookmarks matching QUERY in browser
+
+ $PROGRAM tag QUERY TAG [TAG ...]
+ add TAGs to bookmarks matching QUERY
+
+ $PROGRAM edit QUERY
+ edit bookmarks matching QUERY in \$EDITOR
+
+ $PROGRAM fetchtitles
+ fetch all bookmark titles from the web
+
+ $PROGRAM help
+ print this help message
+
+ $PROGRAM version
+ print version info
+
+ $PROGRAM STRING
+ identical to $PROGRAM list QUERY
+
+ $PROGRAM NUMBER
+ identical to $PROGRAM open NUMBER
+
+ Most commands can be used in unambiguous diminutive form, e.g.
+ blip a URL
+ blip ls QUERY
+ blip b QUERY
+
+EOF
+}
+
+function list {
+ [[ -n $PRINT_LINE ]] && local NR='NR,'
+ [[ $1 =~ [0-9]+ ]] && local LINE="$1"
+
+ if [[ -n $LINE ]]
+ then
+ URLS=$(awk -F '\t' "FNR == $LINE { print $NR $PRINT_FIELDS }" "$FILE")
+ else
+ URLS=$(awk -F '\t' -v q="$*" "$SEARCH_SCOPE ~ q { print $NR $PRINT_FIELDS }" "$FILE")
+ fi
+
+ [[ -n $URLS ]] && echo "$URLS" | column -t
+}
+
+function browse {
+ [[ $1 =~ ^[0-9]+$ ]] && local LINE="$1"
+
+ if [[ -n $LINE ]]
+ then
+ URLS=($(awk -F '\t' "FNR == $LINE { print \$1 }" "$FILE"))
+ elif [[ -n $* ]]
+ then
+ URLS=($(awk -F '\t' -v q="$*" "$SEARCH_SCOPE ~ q { print \$1 }" "$FILE"))
+ fi
+
+ if [[ ${#URLS[@]} -gt $BROWSE_LIMIT ]]
+ then
+ cat >&2 <<-EOF
+ Error:
+ Attempted to open ${#URLS[@]} bookmarks at once but limit is $BROWSE_LIMIT
+ Try opening fewer bookmarks or set a higher \$BOOKMARKS_BROWSE_LIMIT
+ EOF
+ else
+ for URL in "${URLS[@]}"
+ do
+ "$BROWSE" "$URL"
+ done
+ fi
+}
+
+PRINT_LINE=1
+PRINT_FIELDS='$1'
+SEARCH_SCOPE='$0'
+
+if [[ $1 =~ ^(-h|(--)?help)$ ]]
+then
+ blip_help
+ exit 0
+elif [[ $1 = init ]]
+then
+ if [[ -r $FILE ]]
+ then
+ cat <<-EOF
+ Bookmark file already exists at:
+ $FILE
+
+ EOF
+ else
+ touch "$FILE"
+ cat <<-EOF
+ Created bookmark file at:
+ $FILE
+
+ EOF
+ fi
+elif [[ $1 =~ ^a(dd?)?$ ]]
+then
+ # check and add URL as new bookmark
+ # TODO
+ # TODO check if exists?
+ echo add -- not yet implemented
+ exit 9
+elif [[ $1 =~ ^t(ag?)?$ ]]
+then
+ # tag bookmark
+ shift 1
+ # TODO
+ echo tag -- not yet implemented
+ exit 9
+elif [[ $1 =~ ^o(p(en?)?)?$|^b(r(o(w(se?)?)?)?)?$ ]]
+then
+ # call $BROWSE on bookmark URL
+ shift 1
+ browse "$@"
+ exit 0
+elif [[ $1 =~ ^e(d(it?)?)?$ ]]
+then
+ # call $EDITOR on bookmark
+ shift 1
+ # TODO
+ echo edit -- not yet implemented
+ exit 9
+elif [[ $1 =~ ^fetch(titles?)?$ ]]
+then
+ # fetch webpage titles
+ # TODO
+ echo fetch -- not yet implemented
+ exit 9
+elif [[ $1 =~ ^(l(i?(st?)?)?|f(i(nd?)?)?)$ ]]
+then
+ # list matching bookmarks
+ shift 1
+ list "$@"
+elif [[ $1 =~ ^[0-9]+$ ]]
+then
+ browse "$1"
+ exit 0
+elif [[ -n $1 ]]
+then
+ list "$@"
+ exit 0
+else
+ # print help
+ blip_help
+ exit 1
+fi