From a776b419ff9ecaaf5bf2d51ed16796b1ffc30b46 Mon Sep 17 00:00:00 2001 From: speedie Date: Sun, 5 Feb 2023 12:58:48 +0100 Subject: [PATCH] add support for removal of entries --- scripts/speedwm-sxp | 52 +++++++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/scripts/speedwm-sxp b/scripts/speedwm-sxp index 3db196e..39b6e2b 100755 --- a/scripts/speedwm-sxp +++ b/scripts/speedwm-sxp @@ -1,8 +1,9 @@ #!/bin/sh # speedwm-sxp -# function which returns history -MENU_DATA() { cat "$CUSTOM_HISTFILE"; printf "%s\nClear\nExit\n" "------"; } +# some functions we can use on demand +print_text() { printf "%s\n%s\nClear\nRemove\nExit\n" "$(cat "$CUSTOM_HISTFILE")" "------"; } # function which returns history +print() { OP="$(print_text | "$RUNLAUNCHER" -g 1 -l 40 -p "$*")"; } # function which prints the menu # basic variables [ -z "$CUSTOM_HISTFILE" ] && CUSTOM_HISTFILE="$HOME/.config/speedwm/.custom_history" @@ -11,20 +12,43 @@ MENU_DATA() { cat "$CUSTOM_HISTFILE"; printf "%s\nClear\nExit\n" "------"; } # make sure $CUSTOM_HISTFILE exists mkdir -p "$(dirname "$CUSTOM_HISTFILE")"; touch "$CUSTOM_HISTFILE" -# prompt the user -OP="$(MENU_DATA | "$RUNLAUNCHER" -g 1 -l 40 -p 'Enter an S expression:')" - # parse current expression -[ "$OP" = "Clear" ] && printf "" > "$CUSTOM_HISTFILE" && exit -[ "$OP" = "Exit" ] && exit 0 -[ "$OP" = "------" ] && exit 0 -[ -z "$OP" ] && exit 0 +parse() { + print "$@" + [ "$OP" = "Clear" ] && printf "" > "$CUSTOM_HISTFILE" && exit + [ "$OP" = "Exit" ] && exit 0 + [ "$OP" = "------" ] && exit 0 + [ -z "$OP" ] && exit 0 +} + +main() { + parse "Enter an S expression:" + [ "$OP" = "Remove" ] && \ + remove_entry "$@" || \ + add_entry "$@" + + return 0 +} # write expression +add_entry() { + FILE_CONTENTS="$(grep -v "$OP" "$CUSTOM_HISTFILE")" + printf "%s\n" "$FILE_CONTENTS" > "$CUSTOM_HISTFILE" # prevent duplicates -FILE_CONTENTS="$(grep -v "$OP" "$CUSTOM_HISTFILE")" -printf "%s\n" "$FILE_CONTENTS" > "$CUSTOM_HISTFILE" # prevent duplicates + # add entry + printf "%s\n" "$OP" >> "$CUSTOM_HISTFILE" + printf "%s\n" "$OP" +} -# add entry -printf "%s\n" "$OP" >> "$CUSTOM_HISTFILE" -printf "%s\n" "$OP" +# write expression +remove_entry() { + # print menu again so we can choose an entry to remove + parse "Remove:" + [ "$OP" = "Remove" ] && remove_entry "$@" + + # add everything except the entry to $FILE_CONTENTS and write that back to the file + FILE_CONTENTS="$(grep -v "$OP" "$CUSTOM_HISTFILE")" || return 1 + printf "%s\n" "$FILE_CONTENTS" > "$CUSTOM_HISTFILE" || return 1 +} + +main "$@"