commit 7c0f6e1

hovercats  ·  2022-02-01 19:53:51 +0000 UTC
parent d4d42be
bmks: rewrote for practise. removed fzf functionality
2 files changed,  +47, -57
M README
M bmks
M README
+4, -5
 1@@ -8,11 +8,10 @@ Overview
 2 --------
 3 
 4   - bmks
 5-    - A browser independent bookmark script. 
 6-    - Can use dmenu and fzf, and probably rofi and bemenu etc aswell
 7-    - Originally published on the Suckless website, but it was written in bash,
 8-      so I ran shellcheck on it, and corrected it accordingly. Should now be
 9-      posix.
10+    - A browser independent bookmark script 
11+    - Uses dmenu, but should easily be usabe with fzf, rofi, bemenu etc
12+    - Based off of the bmks script found on Suckless' website
13+    - Ive since rewritten it with some twists for practise
14 
15   - dmpv
16     - Play videos in ~/Videos in mpv from dmenu
M bmks
+43, -52
  1@@ -1,76 +1,67 @@
  2-#!/bin/sh
  3+#!/bin/sh -e
  4 
  5-# set prefered launcher
  6-PREFERED_LAUNCHER=dmenu
  7-# set path where urls will be stored
  8-URL_FILE_PATH=$HOME/.bmks/
  9-# name of file urls will be stored in
 10-URL_FILE_NAME=urls
 11+# A simple bookmarking script that uses dmenu
 12 
 13-show_usage() {
 14-	printf "bmks: unix bookmark management that sucks less
 15+file_path="$HOME/.config/bmks/"
 16+filename=urls
 17+bookmarks="$file_path/$filename"
 18+browser=surf
 19+launcher=dmenu
 20 
 21-usage:
 22+usage () {
 23+    printf "bmks: A simple bookmarking script that uses dmenu
 24+
 25+Usage:
 26 bmks help
 27-	show this help message
 28+  show this message
 29 bmks add <url>
 30-	add a new bookmark
 31+  add a new bookmark
 32 bmks del
 33-	remove a bookmark
 34+  remove a bookmark
 35 bmks ls
 36-	show all bookmarks
 37-bmks dmenu
 38-	manual switch for displaying bookmarks in dmenu
 39+  list bookmarks
 40 bmks fzf
 41-	manual switch for displaying bookmarks in fzf
 42+  use fzf instead of dmenu
 43 
 44-Configuration is done by directly editing the script. Two launchers are available (dmenu and fzf). You can specify which one to use by adding to the PREFERED_LAUNCHER variable directly in the script. Both will display a menu that will allow you to choose a bookmark and open it in your default browser.
 45+Configuration is done directly in script by editing the variables.
 46+"
 47 
 48-If you would prefer to have your bookmarks stored in alternate location there are also variables that can be changed for that. The default is /home/user/.bmks/urls\n"
 49 }
 50-
 51-bmks_add() {
 52-	[ -z "$url" ] && printf "Error: url must be provided\n\n" && show_usage && exit 0
 53-	printf "Description: "
 54-	read -r description
 55-	[ -z "$description" ] && echo "$url" >> "$URL_FILE_PATH/$URL_FILE_NAME"
 56-	[ -n "$description" ] && echo "$description - $url" >> "$URL_FILE_PATH/$URL_FILE_NAME"
 57+bmks_add () { 
 58+	[ -z "$url" ] && printf "Error: url must be provided\n" && exit 0
 59+  printf "Description: "
 60+  read -r description
 61+  [ -z "$description" ] && echo "$url" >> "$bookmarks"
 62+  [ -n "$description" ] && echo "$description" - "$url" >> "$bookmarks"
 63 }
 64 
 65-bmks_ls() {
 66-	bmks_check
 67-	sort "$URL_FILE_PATH/$URL_FILE_NAME"
 68+bmks_ls () {
 69+    sort "$bookmarks"
 70 }
 71 
 72-bmks_del() {
 73-	bmks_check
 74-	case "$PREFERED_LAUNCHER" in
 75-		dmenu) sed -i "/$(sort "$URL_FILE_PATH/$URL_FILE_NAME" | dmenu -l "$(wc -l "$URL_FILE_PATH/$URL_FILE_NAME")")/d" "$URL_FILE_PATH/$URL_FILE_NAME" ;;
 76-		fzf) sed -i "/$(sort "$URL_FILE_PATH/$URL_FILE_NAME" | fzf)/d" "$URL_FILE_PATH/$URL_FILE_NAME" ;;
 77-	esac
 78+go_to () {
 79+    case $launcher in
 80+		dmenu) sort "$bookmarks" | dmenu -i -l "$(wc -l "$bookmarks")" | awk '{print $(NF)}' | xargs -I '{}' "$browser" {} ;;
 81+    esac
 82 }
 83 
 84-bmks_display() {
 85-	bmks_check
 86-	case "$PREFERED_LAUNCHER" in
 87-		dmenu) sort "$URL_FILE_PATH/$URL_FILE_NAME" | dmenu -l "$(wc -l "$URL_FILE_PATH/$URL_FILE_NAME")" | awk '{print $(NF)}' | xargs -I '{}' "$BROWSER" {} ;;
 88-		fzf) sort "$URL_FILE_PATH/$URL_FILE_NAME" | fzf | awk '{print $(NF)}' | xargs -I '{}' "$BROWSER" {} ;;
 89-	esac
 90+bmks_del () {
 91+  case "$launcher" in
 92+    dmenu) sed "/$(sort "$bookmarks" | dmenu -i -l "$(wc -l "$bookmarks")")/d" "$bookmarks" > _ && mv -f _ "$bookmarks" ;;
 93+esac
 94 }
 95 
 96-bmks_check() {
 97-	[ ! -s "$URL_FILE_PATH/$URL_FILE_NAME" ] && printf "Error: No bookmarks found to display. Try adding some!\n\n" && show_usage && exit 0
 98+bmks_check () {
 99+    [ ! -s "$bookmarks" ] && printf "No bookmarks found. please add one" && usage && exit 0
100 }
101 
102-[ ! -d "$URL_FILE_PATH" ] && mkdir "$URL_FILE_PATH"
103-[ ! -f "$URL_FILE_PATH/$URL_FILE_NAME" ] && touch "$URL_FILE_PATH/$URL_FILE_NAME"
104+[ ! -d "$file_path" ] && mkdir -p "$file_path"
105+[ ! -f "$filename" ] && :> "$file_path/$filename" 
106 
107 case "$1" in
108-	"help") show_usage ;;
109-	"add") url=$2; bmks_add ;;
110-	"del") bmks_del ;;
111-	"ls") bmks_ls ;;
112-	"dmenu") PREFERED_LAUNCHER=$1; bmks_display ;;
113-	"fzf") PREFERED_LAUNCHER=$1; bmks_display ;;
114-	*) bmks_display ;;
115+    help) usage ;;
116+    add) url=$2 bmks_add ;;
117+    ls) bmks_ls ;;
118+    del) bmks_del ;;
119+    *) go_to ;;
120 esac