WIP: wishlist improvements #4
1 changed files with 76 additions and 0 deletions
76
scripts/shopping
Executable file
76
scripts/shopping
Executable file
|
@ -0,0 +1,76 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
# record shopping list items
|
||||
|
||||
from sys import argv, exit
|
||||
|
||||
list_file = "wishlist.txt"
|
||||
|
||||
|
||||
## utilities
|
||||
|
||||
def die(msg: str):
|
||||
print(msg)
|
||||
exit(1)
|
||||
|
||||
def fetch_list() -> list:
|
||||
try:
|
||||
with open(list_file, 'r') as f:
|
||||
return list(map(lambda item: item.strip(), f.readlines()))
|
||||
except:
|
||||
die("wuh oh, i can't open the shopping list file!")
|
||||
|
||||
def save_list(the_list: list):
|
||||
try:
|
||||
with open(list_file, 'w') as f:
|
||||
f.write("\n".join(the_list))
|
||||
except:
|
||||
die("oh noes! the shopping list couldn't be saved!")
|
||||
|
||||
|
||||
## shopping list management
|
||||
|
||||
def add(item: str):
|
||||
if len(item) == 0:
|
||||
die("what do u want to add?")
|
||||
the_list = fetch_list()
|
||||
the_list.append(item)
|
||||
save_list(the_list)
|
||||
print(f"#{len(the_list)-1}: \"{item}\" added!")
|
||||
|
||||
def remove(index: int):
|
||||
the_list = fetch_list()
|
||||
if index < 0 or index >= len(the_list):
|
||||
die("that item does not exist!")
|
||||
new_list = filter(lambda item: item != the_list[index], the_list)
|
||||
save_list(new_list)
|
||||
print(f"#{index} removed!")
|
||||
|
||||
def show():
|
||||
the_list = fetch_list()
|
||||
for i,item in enumerate(the_list):
|
||||
print(f"{i}: {item}")
|
||||
exit(42)
|
||||
|
||||
def usage():
|
||||
print("!neko shopping <command> <args>")
|
||||
print("commands:")
|
||||
print(" add <item> add an item")
|
||||
print(" remove <number> remove an item")
|
||||
print(" show show the list")
|
||||
exit(42)
|
||||
|
||||
|
||||
## entry point
|
||||
if __name__ == '__main__':
|
||||
cmd = arg = ""
|
||||
try:
|
||||
cmd, arg = argv[1].split(' ', 1)
|
||||
except:
|
||||
cmd = argv[1]
|
||||
|
||||
match cmd:
|
||||
case "add": add(arg)
|
||||
case "remove": remove(int(arg))
|
||||
case "show": show()
|
||||
case _: usage()
|
Loading…
Reference in a new issue