wallr: Add direct article selection option

Added `-a` option which can be supplied with an article id that wallr
will open directly.

Article ids can take the absolute form of e.g. `-a 1048` opening article
with id 1048.
They can also take the relative form of e.g. `-a +2` which will open the
third-to-last entry in wallabag, i.e. count backwards from newest.
That means you can use `-a +0` for example to always directly open the
newest entry.
This commit is contained in:
Marty Oehme 2021-03-16 10:17:24 +01:00
parent a85519e1cb
commit 3763c7e581
Signed by: Marty
GPG Key ID: B7538B8F50A1C800
1 changed files with 44 additions and 11 deletions

View File

@ -10,22 +10,31 @@ WALLR_MARKREAD=true
# get listing of all (read/unread/starred/all) wallabag articles
get_articles() {
WALLR_article_listing=""
if [ "$WALLR_FILTER" = "all" ]; then
wallabag list
WALLR_article_listing=$(wallabag list)
elif [ "$WALLR_FILTER" = "starred" ]; then
wallabag list -s
WALLR_article_listing=$(wallabag list -s)
elif [ "$WALLR_FILTER" = "unread" ]; then
wallabag list -n
WALLR_article_listing=$(wallabag list -n)
elif [ "$WALLR_FILTER" = "last" ]; then
WALLR_article_listing=$(wallabag list -q 1)
else
wallabag list -r
WALLR_article_listing=$(wallabag list -r)
fi
echo "$WALLR_article_listing" | head -n-1 | tail -n+2
}
# return the ID of article from listing version
# list articles and choose with fzf
fuzzy_select() {
[ -z "$1" ] && exit 1
echo "$1" | fzf "${WALLR_FZF_OPTS}"
}
# strip off only ID article listing
get_id() {
[ -z "$1" ] && exit 1
echo "$1" | head -n-1 | tail -n+2 | fzf "${WALLR_FZF_OPTS}" | sed -e 's/^ \+//' | cut -f1 -d' '
exit 1
echo "$1" | sed -e 's/^ \+//' | cut -f1 -d' '
}
# grab content of article with ID passed in as parameter
@ -54,14 +63,30 @@ check_missing_reqs() {
fi
}
open_in_pager() {
get_content "$1" | "$WALLR_READER"
}
main() {
arts=$(get_articles "$WALLR_FILTER")
id="$(get_id "$arts")"
# already selected an id
if [ -n "$WALLR_ARTICLE_SELECTED" ]; then
# relative selection
if printf "%s" "$WALLR_ARTICLE_SELECTED" | grep -qe '^+'; then
WALLR_FILTER="last"
newestid=$(get_id "$(get_articles)")
id=$((newestid - "$WALLR_ARTICLE_SELECTED"))
fi
# select with fuzzy menu
else
arts=$(get_articles "$WALLR_FILTER")
art="$(fuzzy_select "$arts")"
id="$(get_id "$art")"
fi
if [ "$WALLR_READER" = "BROWSER" ]; then
open_link "$id"
else
get_content "$id" | "$WALLR_READER"
open_in_pager "$id"
if "$WALLR_MARKREAD"; then
mark_read "$id"
fi
@ -87,13 +112,18 @@ show_help() {
"" \
" -n List only unread articles." \
"" \
" -a [number] Do not list articles but directly open article number." \
" Takes either explicit number (1096 opens wallabag article" \
" with same id), or in the form of '+1' or '+102' which will" \
" open most recent counting backwards." \
"" \
" -q Do not mark articles opened as read." \
"" \
" -h Print out this help." \
""
}
while getopts "h?qnorse" opt; do
while getopts "h?qnorsea:" opt; do
case "$opt" in
h | \?)
show_help
@ -119,6 +149,9 @@ while getopts "h?qnorse" opt; do
q)
WALLR_MARKREAD="false"
;;
a)
WALLR_ARTICLE_SELECTED="$OPTARG"
;;
esac
done
shift $((OPTIND - 1))