Created
November 3, 2021 07:04
-
-
Save SymphonySimper/9a4c741fbdf892d7db04a69b4bccdf21 to your computer and use it in GitHub Desktop.
bmark with repo function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
bmark_loc="$XDG_DATA_HOME"/bmark/bookmarks | |
repo="[email protected]:SymphonySimper/bmark.git" | |
repo_parent_loc="$(echo $bmark_loc | sed 's/bmark\/bookmarks//g')" | |
repo_loc="$(echo $bmark_loc | sed 's/bookmarks//g')" | |
err(){ echo "$(syl r; syl B)${1}$(syl R)"; } | |
succ(){ echo "$(syl g; syl B)${1}$(syl R)"; } | |
git_c="git -C $repo_loc" | |
append_bmark(){ printf "%*s %4s %s\n" -24 "$1" "$2" "$3" >> $bmark_loc; } | |
sort_bmark(){ sort $bmark_loc -o $bmark_loc; } | |
add_bmark(){ | |
if grep -qw "^$1" $bmark_loc; then | |
err "$1 already exists" | |
else | |
read -p "Is this an app [y/N] " ans | |
case "$ans" in | |
[yY]*) | |
echo 'added as a chromium app' | |
append_bmark "$1" 'app' "$2" | |
;; | |
*) append_bmark "$1" 'site' "$2" ;; | |
esac | |
succ "$1 added to bmarks" | |
sort_bmark | |
fi | |
} | |
rm_bmark(){ | |
if grep -qw "^$1" $bmark_loc; then | |
sed "/^$1/d" -i $bmark_loc | |
succ "$1 deleted" | |
sort_bmark | |
else | |
err "bmark $1 not found" | |
fi | |
} | |
cp_bmark(){ | |
is_there="$(grep -w "^$1" $bmark_loc)" | |
if [ -n "$is_there" ]; then | |
echo "$(echo "$is_there" | awk '{ print $3 }')" \ | |
| xclip -sel c | |
echo "bmark \`$1\` is copied to clipboard" | |
else | |
err "bmark $1 not found" | |
fi | |
} | |
open_bmark(){ | |
is_there="$(grep -w "^$1" $bmark_loc)" | |
if [ -n "$is_there" ]; then | |
url="$(echo "$is_there" | awk '{ print $3 }')" | |
if echo "$is_there" | grep -wq 'app'; then | |
$CHROME_EXECUTABLE --app="${url}" > /dev/null 2>&1 & | |
else | |
$BROWSER "${url}" > /dev/null 2>&1 & | |
fi | |
else | |
err "bmark $1 not found" | |
fi | |
} | |
clone_repo(){ | |
if [ -f "$HOME"/.ssh/id*.pub ]; then | |
git -C $repo_parent_loc clone $repo | |
fi | |
} | |
push_repo(){ | |
if $git_c status | grep -q 'modified'; then | |
$git_c commit -m "$1" \ | |
&& $git_c push | |
else | |
succ "Nothing to commit" | |
fi | |
} | |
status_repo(){ $git_c status -v; } | |
if $git_c status | grep -q 'modified'; then | |
$git_c add -u | |
fi | |
if [ ! -d $repo_loc ]; then | |
err "$bmark_loc does not exist" | |
read -p "Would you like to clone it? [Y/n]" ANS | |
case "$ANS" in | |
[yY]*) clone_repo ;; | |
[nN]*) exit 1 ;; | |
*) clone_repo ;; | |
esac | |
fi | |
case "$1" in | |
add) shift; add_bmark "$1" "$2";; | |
rm) shift; rm_bmark "$1" ;; | |
edit) $EDITOR $bmark_loc; sort_bmark ;; | |
clone) clone_repo ;; | |
status) status_repo ;; | |
push) shift; push_repo "$1" ;; | |
cp) shift; cp_bmark "$1" ;; | |
list) cat $bmark_loc ;; | |
open) shift; open_bmark "$1" ;; | |
*) err "Invalid argument" ;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment