2023-02-04 12:33:13 +01:00
|
|
|
#!/bin/sh
|
|
|
|
# speedwm-sxp
|
|
|
|
|
2023-02-05 12:58:48 +01:00
|
|
|
# some functions we can use on demand
|
2023-02-10 22:16:11 +01:00
|
|
|
print_text() { printf "%s\n%s\nClear\nRemove\nExit" "$(grep -ve '^$' $CUSTOM_HISTFILE)" "------"; } # function which returns history
|
|
|
|
print_menu() { OP="$(print_text | "$RUNLAUNCHER" -g 1 -l 40 -p "$*")"; } # function which prints the menu
|
2023-02-04 12:33:13 +01:00
|
|
|
|
|
|
|
# basic variables
|
|
|
|
[ -z "$CUSTOM_HISTFILE" ] && CUSTOM_HISTFILE="$HOME/.config/speedwm/.custom_history"
|
|
|
|
[ -z "$RUNLAUNCHER" ] && RUNLAUNCHER="spmenu"
|
|
|
|
|
|
|
|
# make sure $CUSTOM_HISTFILE exists
|
|
|
|
mkdir -p "$(dirname "$CUSTOM_HISTFILE")"; touch "$CUSTOM_HISTFILE"
|
|
|
|
|
|
|
|
# parse current expression
|
2023-02-05 12:58:48 +01:00
|
|
|
parse() {
|
2023-02-10 22:16:11 +01:00
|
|
|
print_menu "$@"
|
|
|
|
case "$OP" in
|
|
|
|
"Clear") rm -f "$CUSTOM_HISTFILE"; mkdir -p "$(dirname "$CUSTOM_HISTFILE")"; touch "$CUSTOM_HISTFILE"; main "$@" ;;
|
|
|
|
"Exit") exit 0 ;;
|
|
|
|
"------") exit 0 ;;
|
|
|
|
"") exit 0 ;;
|
|
|
|
esac
|
2023-02-05 12:58:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
main() {
|
|
|
|
parse "Enter an S expression:"
|
|
|
|
[ "$OP" = "Remove" ] && \
|
|
|
|
remove_entry "$@" || \
|
2023-02-10 22:16:11 +01:00
|
|
|
perf_entry "$@"
|
2023-02-05 12:58:48 +01:00
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
# write expression
|
2023-02-10 22:16:11 +01:00
|
|
|
perf_entry() {
|
|
|
|
file_contents="$(grep -v "$OP" "$CUSTOM_HISTFILE")"
|
|
|
|
printf "%s\n" "$file_contents" > "$CUSTOM_HISTFILE" # prevent duplicates
|
2023-02-05 12:58:48 +01:00
|
|
|
|
|
|
|
# add entry
|
|
|
|
printf "%s\n" "$OP" >> "$CUSTOM_HISTFILE"
|
|
|
|
printf "%s\n" "$OP"
|
|
|
|
}
|
2023-02-04 12:33:13 +01:00
|
|
|
|
|
|
|
# write expression
|
2023-02-05 12:58:48 +01:00
|
|
|
remove_entry() {
|
|
|
|
# print menu again so we can choose an entry to remove
|
|
|
|
parse "Remove:"
|
|
|
|
[ "$OP" = "Remove" ] && remove_entry "$@"
|
2023-02-04 13:17:38 +01:00
|
|
|
|
2023-02-10 22:16:11 +01:00
|
|
|
# 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
|
2023-02-05 12:58:48 +01:00
|
|
|
}
|
2023-02-04 13:17:38 +01:00
|
|
|
|
2023-02-05 12:58:48 +01:00
|
|
|
main "$@"
|