Skip to content

Instantly share code, notes, and snippets.

@epochblue
Created October 16, 2015 02:27
Show Gist options
  • Save epochblue/56715da3b6804a9bc265 to your computer and use it in GitHub Desktop.
Save epochblue/56715da3b6804a9bc265 to your computer and use it in GitHub Desktop.
A CLI-only read-later list.
#!/usr/bin/env bash
# readlater.sh -- a time waster by Bill Israel <bill.israel@gmail.com>
#
# Think of it as an overly simplified, text-based version of Instapaper
# without most of the cool features.
#
# The path to your read later file (I recommend it being a file in Dropbox)
RL_FILE="$HOME/Dropbox/readlater.txt"
TAB=$'\t'
# usage - Prints the usage message
usage() {
cat <<USAGE
usage: $0 {add|rm|list|get} {id|URL}
add Add somehing to the list. Expects a URL to save.
Example: readlater.sh add http://example.com/
rm Removes something from the list. Expects and id to delete.
Example: readlater.sh rm 8
list Lists all the currently saved URLs. No parameters expected.
Example: readlater.sh list
get Returns the URL for the specified id.
Example: readlater.sh get 8
USAGE
}
# Add something to the list
_add() {
# If the file doesn't exist yet, create it
[ ! -f "$RL_FILE" ] && touch "$RL_FILE"
URL=$1; shift
DESC="$@"
echo -e "$URL\t$DESC" >> $RL_FILE
echo -e "$URL saved\n"
}
# Remove something from the list
_rm() {
ID=$1
sed -i "" "${ID}d" $RL_FILE
echo -e "Deleted URL with id $ID\n"
}
# Print the list of items
_list() {
i=1
echo
while read line; do
URL=`echo "$line" | cut -f 1`
[[ $line =~ $TAB ]] && DESC=`echo "$line" | cut -f 2-` || DESC=""
echo -e " $i:\t$URL\t($DESC)"
let "i+=1"
done < $RL_FILE | column -t -s "$TAB"
echo
}
# Return the URL associated with an id
_get() {
ID=$1
sed -n "${ID}p" $RL_FILE | cut -f 1
}
CMD=$1; shift
case $CMD in
add)
[ -z "$1" ] && usage && exit 1
URL=$1; shift
_add $URL "$@"
;;
rm)
[ -z "$1" ] && usage && exit 1
_rm $1
;;
get)
[ -z "$1" ] && usage && exit 1
_get $1
;;
list)
_list
;;
*)
echo -e "Unknown command '$CMD'.\n"
usage
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment