From 5f50b2e04229e52591b5f57b37dc4fe3c7fab0e7 Mon Sep 17 00:00:00 2001 From: jsnal Date: Thu, 14 May 2020 19:41:11 -0400 Subject: [PATCH 1/6] 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 From 864d0149ce63c135792aa13b7c3d14728201937f Mon Sep 17 00:00:00 2001 From: jsnal Date: Thu, 14 May 2020 19:47:08 -0400 Subject: [PATCH 2/6] feat(usage): update usage for new previewer feature Add the documentation for changing the FZF previewer binary. The help page kind of looks weird now that the alignment is off. That is something that might need to be refactored in the future. --- flash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flash b/flash index 583a6c1..f4f6ef7 100755 --- a/flash +++ b/flash @@ -151,7 +151,7 @@ if [ ! -e "$REVIEW_LOG" ]; then fi print_usage() { - echo -e "\n${LCYAN}fla.sh --- Flash card system by Bryan Jenks${NC} ${LBLUE}${NC}\n\n${YELLOW}${BOLD}Usage:${NC}\n\t${GREEN}flash -h:${NC} Print this help text\n\t${GREEN}flash -i:${NC} Print Information about the flashcard system\n\t${GREEN}flash -v:${NC} Print version Number\n\n" + echo -e "\n${LCYAN}fla.sh --- Flash card system by Bryan Jenks${NC} ${LBLUE}${NC}\n\n${YELLOW}${BOLD}Usage:${NC}\n\t${GREEN}flash -h:${NC} Print this help text\n\t${GREEN}flash -i:${NC} Print Information about the flashcard system\n\t${GREEN}flash -v:${NC} Print version Number\n\t${GREEN}flash -p [BINARY]:${NC} Change the previewer when selecting decks\n" } print_info() { From 0c11309a4d6a90845f6853869296060c5598486c Mon Sep 17 00:00:00 2001 From: jsnal Date: Thu, 14 May 2020 20:17:32 -0400 Subject: [PATCH 3/6] fix(shellcheck): fix oneline if-then-else shellcheck Fixes SC2015 and SC2086 https://github.com/koalaman/shellcheck/wiki/SC2015 https://github.com/koalaman/shellcheck/wiki/SC2086 --- flash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flash b/flash index f4f6ef7..67506ac 100755 --- a/flash +++ b/flash @@ -202,7 +202,7 @@ while getopts 'hivp:' flag; do 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 ; } ;; + p) if [[ $(command -v "$OPTARG" 2>&1) ]]; then PREVIEWER=$OPTARG; else echo "Unable to find previewer $OPTARG. Exiting..." && exit 1; fi ;; *) print_usage && exit 1 ;; esac done From 8683c4d974e9418e4a28c7ce9157ddf2d80524da Mon Sep 17 00:00:00 2001 From: jsnal Date: Fri, 15 May 2020 14:57:20 -0400 Subject: [PATCH 4/6] refactor(pull request): update usage and sleep command per pull request Add a list of supported binaries for the `-p` flag and use read instead of sleep when falling back onto the bat. --- flash | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/flash b/flash index 67506ac..0e7ffbb 100755 --- a/flash +++ b/flash @@ -151,7 +151,7 @@ if [ ! -e "$REVIEW_LOG" ]; then fi print_usage() { - echo -e "\n${LCYAN}fla.sh --- Flash card system by Bryan Jenks${NC} ${LBLUE}${NC}\n\n${YELLOW}${BOLD}Usage:${NC}\n\t${GREEN}flash -h:${NC} Print this help text\n\t${GREEN}flash -i:${NC} Print Information about the flashcard system\n\t${GREEN}flash -v:${NC} Print version Number\n\t${GREEN}flash -p [BINARY]:${NC} Change the previewer when selecting decks\n" + echo -e "\n${LCYAN}fla.sh --- Flash card system by Bryan Jenks${NC} ${LBLUE}${NC}\n\n${YELLOW}${BOLD}Usage:${NC}\n\t${GREEN}flash -h:${NC} Print this help text\n\t${GREEN}flash -i:${NC} Print Information about the flashcard system\n\t${GREEN}flash -v:${NC} Print version Number\n\t${GREEN}flash -p [BINARY]:${NC} Change the previewer when selecting decks. Default: bat\n\t\tSupported: bat, cat\n" } print_info() { @@ -213,7 +213,7 @@ 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' ;; + *) echo -e "$PREVIEWER is not a valid previewer. Use 'bat' or 'cat'. Falling back on default..." && read -s -p 'Press [Enter] to continue...' && PREVIEWER='bat' ;; esac done From 8a8710400942a38c3ef77ecf2668e25f62d0ff08 Mon Sep 17 00:00:00 2001 From: jsnal Date: Fri, 15 May 2020 15:00:24 -0400 Subject: [PATCH 5/6] fix(shellcheck): fix mangled backslashes with read Fixes SC2162 https://github.com/koalaman/shellcheck/wiki/SC2162 --- flash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flash b/flash index 0e7ffbb..1f63108 100755 --- a/flash +++ b/flash @@ -213,7 +213,7 @@ 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..." && read -s -p 'Press [Enter] to continue...' && PREVIEWER='bat' ;; + *) echo -e "$PREVIEWER is not a valid previewer. Use 'bat' or 'cat'. Falling back on default..." && read -r -s -p 'Press [Enter] to continue...' && PREVIEWER='bat' ;; esac done From 51ab50d08122c5c6df281537a2d7dc350a49413e Mon Sep 17 00:00:00 2001 From: jsnal Date: Fri, 15 May 2020 17:11:19 -0400 Subject: [PATCH 6/6] refactor(pull request): update quotes and new line Add quotes around variables so the script is more POSIX compliant. Also add new line for styling. --- flash | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/flash b/flash index 1f63108..9c745f6 100755 --- a/flash +++ b/flash @@ -209,11 +209,11 @@ done # Set some parameters for preview command used by FZF -while [[ -z $PREVIEWER_PARAMTERS ]]; do - case $PREVIEWER in +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..." && read -r -s -p 'Press [Enter] to continue...' && PREVIEWER='bat' ;; + *) echo -e "$PREVIEWER is not a valid previewer. Use 'bat' or 'cat'. Falling back on default...\n" && read -r -s -p 'Press [Enter] to continue...' && PREVIEWER='bat' ;; esac done