From 09fc011e92731d0dd78b44ff9681a761661c5a30 Mon Sep 17 00:00:00 2001 From: Silvio Rhatto Date: Wed, 12 Jul 2023 11:23:34 -0300 Subject: Improve the 'show' command (2) --- show | 44 ++++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/show b/show index a68264b..004ef08 100755 --- a/show +++ b/show @@ -11,8 +11,8 @@ FILELIST=".filelist" LIST="$DOCS/$FILELIST" DOC_PATTERN=".*\.(txt|doc|docx|rtf|pdf|sxc|csv|odt|odf|ods|xls|xlsx|ppt|epub|mobi|djvu|lit)" FIND_OPTS="-not -path '*.git*' -regextype posix-egrep -iregex" -ITEM="$1" -OPTION="$2" +ARG="$1" +EXTRA_ARG="$2" DATE="`date +%s`" MAX_AGE="86400" @@ -33,10 +33,17 @@ function __update_filelist { } # Check -if [ -z "$ITEM" ]; then - echo "usage: $BASENAME [option] []" +if [ -z "$ARG" ]; then + echo "usage: $BASENAME [option] [] [extra-args]" + echo " $BASENAME --refresh" + echo " $BASENAME -- " + echo " $BASENAME --open " + echo " $BASENAME --rifle -p 4" + echo " $BASENAME --koreader " + echo " $BASENAME --watch " + echo " $BASENAME " exit 1 -elif [ "$ITEM" == "--refresh" ]; then +elif [ "$ARG" == "--refresh" ]; then __update_filelist elif [ ! -d "$DOCS" ]; then echo "missing $DOCS folder" @@ -57,14 +64,14 @@ fi #fi # Dispatch -#find $DOCS -iname "*$ITEM*" | head -1 | while read entry; do xdg-open "$entry"; done -if [ "$ITEM" == "--search" ]; then +#find $DOCS -iname "*$ARG*" | head -1 | while read entry; do xdg-open "$entry"; done +if [ "$ARG" == "--search" ]; then shift grep -i -- "$*" $LIST -elif [ "$ITEM" == "--watch" ]; then +elif [ "$ARG" == "--watch" ]; then # Convert symlink to full path and start inotifywait loop folder="`readlink -f $DOCS`" - subfolder="$OPTION" + subfolder="$EXTRA_ARG" echo "Watching changes at $folder/$subfolder..." #while inotifywait -e modify -e create -e move -e delete -r "$folder"; do @@ -74,15 +81,24 @@ elif [ "$ITEM" == "--watch" ]; then $PROGRAM --refresh done else - if [ "${ITEM:0:2}" == "--" ] && [ ! -z "$OPTION" ]; then - OPEN="${ITEM:2}" - ITEM="$OPTION" + shift 2 + EXTRA_ARGS="$*" + + # Allows for specifying a custom command to open the file + if [ "${ARG:0:2}" == "--" ] && [ ! -z "$EXTRA_ARG" ]; then + OPEN="${ARG:2}" + ARG="$EXTRA_ARG" + + # Shortcuts for xdg-open + if [ "$OPEN" == "open" ] || [ "$OPEN" == "read" ]; then + OPEN="xdg-open" + fi else OPEN="xdg-open" fi - grep -i -- "$ITEM" $LIST | while read entry; do + grep -i -- "$ARG" $LIST | while read entry; do echo "Opening $entry..." - cd $DOCS && $OPEN "$entry" + cd $DOCS && $OPEN $EXTRA_ARGS "$entry" done fi -- cgit v1.2.3