From fba83a2f347b4a8f0ccef0ac9cf5c73d3e8cffdf Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Fri, 13 May 2022 12:55:05 -0700 Subject: [PATCH] Improve script syntax --- rofi-checklist.sh | 64 ++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 34 deletions(-) diff --git a/rofi-checklist.sh b/rofi-checklist.sh index 97bde1c..6635c46 100755 --- a/rofi-checklist.sh +++ b/rofi-checklist.sh @@ -24,9 +24,7 @@ list=${list//"$EMPTY_RAW"/"$EMPTY"} # empty checkboxes list=${list//"$FILLED_RAW"/"$FILLED"} # filled checkboxes # Don't show clear all option if task list is empty -if [[ $list == "" ]]; then - clear="" -fi +[[ -z $list ]] && clear="" # Don't show clear completed option if there are no completed tasks count=${#list_raw_array[*]} @@ -34,15 +32,13 @@ i=0 completed_tasks="n" while [ $i -lt $count ]; do # %q escapes string, otherwise there are issues with square brackets - if [[ ${list_raw_array[$i]} == `printf "%q" "$FILLED_RAW"`* ]]; then + if [[ ${list_raw_array[$i]} = `printf "%q" "$FILLED_RAW"`* ]]; then completed_tasks="y" break fi i=$(($i + 1)) done -if [[ $completed_tasks == "n" ]]; then - clear_completed="" -fi +[[ $completed_tasks = "n" ]] && clear_completed="" # Run rofi, replace display checkmarks with raw syntax selection=`printf "%s%s%s\n" "$clear" "$clear_completed" "$list" | rofi -dmenu -i -selected-row 2 -p " Task:"` @@ -50,32 +46,32 @@ selection=${selection//"$EMPTY"/"$EMPTY_RAW"} selection=${selection//"$FILLED"/"$FILLED_RAW"} # Selection logic -if [[ $selection == $CLEAR ]]; then - list_raw="" -elif [[ $selection == $CLEAR_COMPLETED ]]; then - list_raw_array=($list_raw) - count=${#list_raw_array[*]} - i=0 - while [ $i -lt $count ] - do - if [[ ${list_raw_array[$i]} == `printf "%q" "$FILLED_RAW"`* ]]; then - unset 'list_raw_array[$i]' - fi - i=$(($i + 1)) - done - list_raw="${list_raw_array[*]}" -elif [[ $selection == `printf "%q" "$FILLED_RAW"`* ]]; then - replace="${selection}${NL}" - list_raw=${list_raw//"$replace"/""} - list_raw=${list_raw//"$selection"/""} -elif [[ $selection == `printf "%q" "$EMPTY_RAW"`* ]]; then - selection_filled=${selection//"$EMPTY_RAW"/"$FILLED_RAW"} - list_raw=${list_raw//"$selection"/"$selection_filled"} -elif [[ $selection != "" ]]; then - if [[ $list_raw != "" ]]; then - list_raw="${list_raw}${NL}" - fi - list_raw=`printf "%s%s %s\n" "$list_raw" "$EMPTY_RAW" "$selection"` -fi +case $selection in + $CLEAR) list_raw="" ;; + $CLEAR_COMPLETED) + list_raw_array=($list_raw) + count=${#list_raw_array[*]} + i=0 + while [ $i -lt $count ] + do + if [[ ${list_raw_array[$i]} = `printf "%q" "$FILLED_RAW"`* ]]; then + unset 'list_raw_array[$i]' + fi + i=$(($i + 1)) + done + list_raw="${list_raw_array[*]}" ;; + `printf "%q" "$FILLED_RAW"`*) + replace="${selection}${NL}" + list_raw=${list_raw//"$replace"/""} + list_raw=${list_raw//"$selection"/""} ;; + `printf "%q" "$EMPTY_RAW"`*) + selection_filled=${selection//"$EMPTY_RAW"/"$FILLED_RAW"} + list_raw=${list_raw//"$selection"/"$selection_filled"} ;; + *) + if [[ -n $selection ]]; then + [[ -n $list_raw ]] && list_raw="${list_raw}${NL}" + list_raw=`printf "%s%s %s\n" "$list_raw" "$EMPTY_RAW" "$selection"` + fi ;; +esac printf "%s\n" "$list_raw" >| $FILE