dotfiles/bin/bm

136 lines
3.5 KiB
Plaintext
Raw Normal View History

2020-12-20 17:00:00 +00:00
#!/bin/fish
2021-08-28 14:16:37 +00:00
# fuzzy bookmark opener, read links from `~/.config/bookmarks.md` to fzf/dmenu and then open selected links with
# default browser
# `~/.config/bookmarks.md` sample:
# ```markdown
# # email
# - [hotmail](https://hotmail.com)
# - [gmail](https://gmail.com)
# ```
2022-09-08 13:56:51 +00:00
set -q BOOKMARK_PATH; or set BOOKMARK_PATH $HOME/Documents/bookmarks.md
2020-12-22 04:03:28 +00:00
if set -q BOOKMARK_SEARCHER
set BOOKMARK_SEARCHER (string split ' ' $BOOKMARK_SEARCHER)
else
set BOOKMARK_SEARCHER fzf --layout reverse
end
2020-12-20 17:00:00 +00:00
function _help
2020-12-22 04:03:28 +00:00
echo "Organize your bookmark with markdown"
echo
echo "Usage bm [command ...args]"
echo
2020-12-20 17:00:00 +00:00
echo "commands:"
2020-12-22 04:03:28 +00:00
echo ' list [query]'
echo ' open <query>'
echo
echo 'examples:'
echo ' bm list "# daily" "# emails" list all links under daily/emails folder'
echo ' bm list "reddit" "github" list all links contains specified keywords'
echo ' bm open "# daily" "# emails" open all links under daily/emails folder'
echo ' bm open "reddit" "github" open all links contains specified keywords'
echo ' bm search with $BOOKMARK_SEARCHER and then open selected'
2020-12-20 17:00:00 +00:00
end
2021-06-12 02:59:50 +00:00
function list
2020-12-22 04:03:28 +00:00
set -e categories
set -e names
for arg in $argv
if string match -q '#*' $arg
set --append categories $arg
else
set --append names $arg
end
end
_filter_category $categories < $BOOKMARK_PATH | _filter_name $names
2020-12-21 03:03:15 +00:00
end
2021-06-12 02:59:50 +00:00
function open
2020-12-22 04:03:28 +00:00
if not count $argv &>/dev/null
_search
2020-12-20 17:00:00 +00:00
else
2020-12-21 03:03:15 +00:00
list $argv | _open_urls
2020-12-20 17:00:00 +00:00
end
end
2021-01-22 03:18:14 +00:00
function update
set url (xsel -ob)
if ! string match 'http*' $url
echo "no url found on clipboard"
exit 1
end
set query (sed -r '/^(#.*|\s*)$/d' $BOOKMARK_PATH | $BOOKMARK_SEARCHER)
echo url: $url
echo query: $query
mv $BOOKMARK_PATH $BOOKMARK_PATH.bak
awk '$0 == "'$query'" {
sub(/\(http.*\)/, "('$url')")
} { print }' $BOOKMARK_PATH.bak > $BOOKMARK_PATH
end
2020-12-22 04:03:28 +00:00
function _filter_category
2020-12-20 17:00:00 +00:00
if not count $argv >/dev/null
cat -
return
end
2020-12-22 04:03:28 +00:00
set categories $argv
2020-12-20 17:00:00 +00:00
set -e output_indent
while read LINE
if set heading (string match -r '^(#+) (.*)$' $LINE)
set heading_indent (string length $heading[2])
if test -z "$output_indent"
2020-12-22 04:03:28 +00:00
contains $LINE $categories && set output_indent $heading_indent
2020-12-20 17:00:00 +00:00
else
2020-12-22 04:03:28 +00:00
test "$heading_indent" -le "$output_indent" && set -e output_indent
2020-12-20 17:00:00 +00:00
end
2020-12-22 04:03:28 +00:00
end
if test -n "$output_indent"
2020-12-20 17:00:00 +00:00
echo $LINE
end
end
end
2020-12-21 03:03:15 +00:00
function _filter_name
if not count $argv >/dev/null
cat -
return
end
2020-12-22 04:03:28 +00:00
set NAMES_PATTERN (string join '|' $argv)
2020-12-20 17:00:00 +00:00
while read LINE
2020-12-22 04:03:28 +00:00
if not string match -q -r $NAMES_PATTERN $LINE
continue
2020-12-20 17:00:00 +00:00
end
2020-12-22 04:03:28 +00:00
echo $LINE
2020-12-20 17:00:00 +00:00
end
end
2020-12-21 03:03:15 +00:00
function _open_urls
2020-12-22 04:03:28 +00:00
set LINK_PATTERN '\[(.*?)\]\((.*?)\)'
2020-12-21 03:03:15 +00:00
while read LINE
2020-12-22 04:03:28 +00:00
if set link (string match -r $LINK_PATTERN $LINE)
echo '' | x-open $link[3] &>/dev/null &
2020-12-21 03:03:15 +00:00
disown
sleep 0.1
2020-12-21 03:03:15 +00:00
end
end
end
2020-12-20 17:00:00 +00:00
2020-12-22 04:03:28 +00:00
function _search
set query (sed -r '/^\s*$/d' $BOOKMARK_PATH | $BOOKMARK_SEARCHER)
test -z "$query" && return
if string match '#*' $query
open $query
else
echo $query | _open_urls
end
end
2020-12-20 17:00:00 +00:00
switch $argv[1]
2021-06-12 02:59:50 +00:00
case list open help update
2020-12-21 03:03:15 +00:00
$argv[1..-1]
2020-12-20 17:00:00 +00:00
case '*'
2020-12-22 04:03:28 +00:00
_search
2020-12-20 17:00:00 +00:00
end