From 5f50b2e04229e52591b5f57b37dc4fe3c7fab0e7 Mon Sep 17 00:00:00 2001 From: jsnal Date: Thu, 14 May 2020 19:41:11 -0400 Subject: [PATCH] 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. --- flash | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/flash b/flash index e39854e..583a6c1 100755 --- a/flash +++ b/flash @@ -26,6 +26,7 @@ # USER CUSTOMIZABLE VARIABLES 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 + PREVIEWER='bat' # What fzf previewer to use when searching through decks # ANSI FOREGROUND ESCAPE COLORS 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 h) print_usage && exit ;; i) print_info && 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 ;; esac done -# Show pretty FZF preview of decks using BAT - DECK="$(find . -maxdepth "$SEARCH_DEPTH" -iname "*.csv" | fzf --preview='bat --theme="Solarized (dark)" --style=numbers --color=always {} | head -500')" +# Set some parameters for preview command used by FZF +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 # return user to start location