dotfiles/bin/bm

85 lines
2.0 KiB
Plaintext
Raw Normal View History

2020-12-20 17:00:00 +00:00
#!/bin/fish
set -q XDG_CONFIG_HOME; or set XDG_CONFIG_HOME ~/.config
set -q BOOKMARK_PATH; or set BOOKMARK_PATH $XDG_CONFIG_HOME/bookmarks.md
set LINK_PATTERN '\[(.*?)\]\((.*?)\)'
function _help
echo "Usage bm <command> ...args"
echo "commands:"
echo ' open [-c <category>]'
end
2020-12-21 03:03:15 +00:00
function list
argparse -n open c/category= -- $argv
_filter_category $_flag_category < $BOOKMARK_PATH | _filter_name $argv
end
function open
2020-12-20 17:00:00 +00:00
if test 0 -eq (count $argv)
echo search mode
else
2020-12-21 03:03:15 +00:00
list $argv | _open_urls
2020-12-20 17:00:00 +00:00
end
end
function _filter_category -a category
if not count $argv >/dev/null
cat -
return
end
set -e output_indent
while read LINE
if set heading (string match -r '^(#+) (.*)$' $LINE)
set heading_indent (string length $heading[2])
set heading_title $heading[-1]
if test -z "$output_indent"
if test -z "$output_indent" && string match "*$heading_title*" $category >/dev/null
set output_indent $heading_indent
end
else
if test "$heading_indent" -le "$output_indent"
set -e output_indent
else
echo $LINE
end
end
else if test -n "$output_indent"
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-20 17:00:00 +00:00
while read LINE
if set link (string match -r '\[(.*?)\]\((.*?)\)' $LINE)
if count $argv >/dev/null && not contains $link[2] $argv
continue
end
2020-12-21 03:03:15 +00:00
echo $LINE
2020-12-20 17:00:00 +00:00
end
end
end
2020-12-21 03:03:15 +00:00
function _open_urls
set -q BROWSER; or set BROWSER xdg-open
while read LINE
if set link (string match -r '\[(.*?)\]\((.*?)\)' $LINE)
$BROWSER $link[3] &
disown
end
end
end
2020-12-20 17:00:00 +00:00
switch $argv[1]
2020-12-21 03:03:15 +00:00
case open list
$argv[1..-1]
2020-12-20 17:00:00 +00:00
case '*'
_help
end