feat(flash): allow the use of previewers other than bat

This commit adds a way of changing the FZF previewer by using the `-p`
flag. An example of this might look like:

$ flash -p cat

This will use cat has the FZF previewer instead of bat. However, it keeps bat as
the default program in all cases unless specifically changed by the `-p`
parameter. There are also checks to see if the provided binary actually
exists on the system. If it exists on the system, it checks if it
supported by flash. If it isn't supported by flash, it falls back onto
bat as the default previewer. For now, only bat and cat are supported but it.
can easily be expanded.
This commit is contained in:
jsnal 2020-05-14 19:41:11 -04:00
parent c746f2545b
commit 5f50b2e042
1 changed files with 14 additions and 3 deletions

17
flash
View File

@ -26,6 +26,7 @@
# USER CUSTOMIZABLE VARIABLES # USER CUSTOMIZABLE VARIABLES
CARD_POOL_SIZE=10 # How large the pool size is for shuf to draw from CARD_POOL_SIZE=10 # How large the pool size is for shuf to draw from
SEARCH_DEPTH=999 # How many levels to recursively search for .csv's in .local/share/flash SEARCH_DEPTH=999 # How many levels to recursively search for .csv's in .local/share/flash
PREVIEWER='bat' # What fzf previewer to use when searching through decks
# ANSI FOREGROUND ESCAPE COLORS # ANSI FOREGROUND ESCAPE COLORS
RED='\033[0;31m' RED='\033[0;31m'
@ -196,18 +197,28 @@ ${LCYAN}The Scoring System:${NC}\n
" "
} }
while getopts 'hiv' flag; do while getopts 'hivp:' flag; do
case "${flag}" in case "${flag}" in
h) print_usage && exit ;; h) print_usage && exit ;;
i) print_info && exit ;; i) print_info && exit ;;
v) echo -e "\n${YELLOW}fla.sh Current Version:${NC} ${RED}1.1${NC}\n" && exit ;; v) echo -e "\n${YELLOW}fla.sh Current Version:${NC} ${RED}1.1${NC}\n" && exit ;;
p) [[ $(command -v $OPTARG 2>&1) ]] && PREVIEWER=$OPTARG || { echo "Unable to find previewer $OPTARG. Exiting..." && exit 1 ; } ;;
*) print_usage && exit 1 ;; *) print_usage && exit 1 ;;
esac esac
done done
# Show pretty FZF preview of decks using BAT # Set some parameters for preview command used by FZF
DECK="$(find . -maxdepth "$SEARCH_DEPTH" -iname "*.csv" | fzf --preview='bat --theme="Solarized (dark)" --style=numbers --color=always {} | head -500')" while [[ -z $PREVIEWER_PARAMTERS ]]; do
case $PREVIEWER in
bat) PREVIEWER_PARAMTERS="--theme='Solarized (dark)' --style=numbers --color=always" ;;
cat) PREVIEWER_PARAMTERS="--number" ;;
*) echo -e "$PREVIEWER is not a valid previewer. Use 'bat' or 'cat'. Falling back on default..." && sleep 1 && PREVIEWER='bat' ;;
esac
done
# Show pretty FZF preview of decks using $PREVIEWER. Default: BAT
DECK="$(find . -maxdepth "$SEARCH_DEPTH" -iname "*.csv" | fzf --preview="$PREVIEWER $PREVIEWER_PARAMTERS {} | head -500")"
# If no deck is selected in fzf menu # If no deck is selected in fzf menu
# return user to start location # return user to start location