add wishlist script #2

Merged
j3s merged 1 commits from jes/wishlist into main 2022-12-08 23:17:33 +00:00
2 changed files with 58 additions and 0 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
.env
nekobot
wishlist.txt

57
scripts/wishlist Executable file
View File

@ -0,0 +1,57 @@
#!/bin/sh
#
# wishlist records wishlist items
set -exu
cmd="$@"
wishlist="wishlist.txt"
[ -f "$wishlist" ] || touch "$wishlist"
die() {
printf "%s.\n" "$1"
exit 1
}
print_usage() {
printf "%s\n" "!neko wishlist <command> <args>
commands:
add <item> - add a wishlist item
remove <item> - remove a wishlist item
show - show the wishlist
clear - clear the wishlist"
exit 42
}
wishlist_add() {
item="$(printf "%s" "$@" | sed -E 's/^add +//g')"
[ -z "$item" ] && die "specify an item to add"
printf "%s\n" "$item" >> wishlist.txt
printf "%s\n" "$item added"
}
wishlist_remove() {
item="$(printf "%s" "$@" | sed -E 's/^remove +//g')"
[ -z "$item" ] && die "specify an item to remove"
sed -E "s/^$item$//g" -i "$wishlist"
printf "%s\n" "$item removed"
}
wishlist_show() {
cat "$wishlist"
exit 42
}
wishlist_clear() {
printf "" > "$wishlist"
printf "%s\n" "wishlist cleared"
}
case "$cmd" in
add*) wishlist_add "$@" ;;
remove*) wishlist_remove "$@" ;;
show) wishlist_show ;;
clear) wishlist_clear ;;
*) print_usage ;;
esac