Files
sxbm/sxbm
2021-04-13 10:32:24 +06:00

127 lines
2.5 KiB
Bash
Executable File

#!/bin/sh
#
# Description: A cli bookmark manager written in POSIX shell
#
# Author: NRK
##########
# CONFIG #
##########
# Where all the data gets stored
# Respects XDG_DATA_HOME if set
[ -z "$XDG_DATA_HOME" ] && XDG_DATA_HOME="$HOME/.local/share"
DATA_DIR="${XDG_DATA_HOME}/sxbm"
DATA_FILE="${DATA_DIR}/bookmarks"
# Colors
# For a list of ANSI color codes, check the link below
# https://gist.github.com/Prakasaka/219fe5695beeb4d6311583e79933a009
COL_LINK="\033[1;31m" # Red
COL_TITLE="\033[0;33m" # Yellow
COL_LINE_NUM="\033[1;34m" # Blue
# 1 to enable encryption, 0 to disable it
ENCRYPTION="0"
# Auto fetch title. 1 to enable, 0 to disable
TITLE_FETCH="1"
#################
### functions ###
#################
die(){
[ -z "$1" ] ||
printf "$@\n" > /dev/stderr
exit 1
}
usage(){
printf "Usage: sxbm <command> [<args>]\n"
printf "
COMMANDS:
add <link> [title] [+tags]
ls|list [-c|--disable-colors] [title] [+tags]
open [-f|--force] <line_num|title|+tags>
rm|remove [-f|--force] <line_num|title|+tags>
edit [line_num|title|+tags]
-h|--help print this text and exit
-v|--version print the version and exit\n\n"
}
bm_list(){
while [ -n "$1" ]; do
case "$1" in
"-c"|"--disable-colors")
COL_LINK=""; COL_TITLE=""; COL_LINE_NUM="";
shift
;;
"-s"|"--strict")
local TAG_STRICT="1"
shift
;;
*)
break
;;
esac
done
expr "$1" : '[0-9]' > /dev/null &&
local QUERY_LINE="$1" ||
while [ -n "$1" ]; do
case "$1" in
+*)
local QUERY_TAG="${QUERY_TAG}$1 "
shift
;;
*)
local QUERY_TITLE="${QUERY_TITLE}$1 "
shift
;;
esac
done
awk -v COL_LINK="$COL_LINK" -v COL_TITLE="$COL_TITLE" \
-v COL_LINE_NUM=$COL_LINE_NUM -v QUERY_TITLE="$QUERY_TITLE" \
' BEGIN{IGNORECASE=1}
$0 ~ QUERY_TITLE { LINK=$1; $1="";
gsub(/+\w+/,"");
print COL_LINE_NUM NR ") " COL_LINK LINK " " COL_TITLE $0 ; }' \
"$DATA_FILE"
}
############
### main ###
############
[ -z "$1" ] && usage && die "No command given"
[ -d "$DATA_DIR" ] ||
mkdir "$DATA_DIR" ||
die "Unable to create $DATA_DIR"
case "$1" in
"add")
;;
"ls"|"list")
shift
bm_list "$@"
;;
"open")
;;
"rm"|"remove")
;;
"edit")
;;
"-h"|"--help")
usage && die
;;
"-v"|"--version")
;;
*)
die "Invalid command. Use 'sxbm -h' for help!"
;;
esac