diff --git a/scripts/.local/bin/wallr b/scripts/.local/bin/wallr index 09339e1..1a4b663 100755 --- a/scripts/.local/bin/wallr +++ b/scripts/.local/bin/wallr @@ -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))