From 3c2bcf50738abec3f8aa059902fbfe4cf14d5b0d Mon Sep 17 00:00:00 2001 From: Erreur32 Date: Sat, 24 Feb 2018 17:43:29 +0100 Subject: [PATCH] Update sheet.py prompt if delete sheet ;) --- cheat/sheet.py | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/cheat/sheet.py b/cheat/sheet.py index ff1ce27..bd66b2e 100644 --- a/cheat/sheet.py +++ b/cheat/sheet.py @@ -1,3 +1,4 @@ +import sys import os import shutil @@ -70,7 +71,52 @@ def path(sheet): def read(sheet): """ Returns the contents of the cheatsheet as a String """ if not exists(sheet): - die('No cheatsheet found for ' + sheet) + die('\033[1;31mNo\033[0m cheatsheet found for \033[1;31m' + sheet + '\033[0m') with open(path(sheet)) as cheatfile: return cheatfile.read() + +def remove(sheet, default="yes"): + """Ask a yes/no/quit sheet via raw_input() and return their answer. + + "sheet" is a string that is presented to the user. + "default" is the presumed answer if the user just hits . + It must be "yes" (the default), "no", "quit" or None (meaning + an answer is required of the user). + The "answer" return value is one of "yes", "no" or "quit". + """ + + if not exists(sheet): + die('\033[1;31mNo\033[0m cheatsheet found for \033[1;31m' + sheet + '\033[0m') + + sheet = sheets.get()[sheet] + + valid = {"yes":"yes", "y":"yes", "ye":"yes", + "no":"no", "n":"no", + "quit":"quit", "qui":"quit", "qu":"quit", "q":"quit"} + if default == None: + prompt = " [y/n/q] " + elif default == "yes": + prompt = " [Y/n/q] " + elif default == "no": + prompt = " [y/N/q] " + elif default == "quit": + prompt = " [y/n/Q] " + else: + raise ValueError("invalid default answer: '%s'" % default) + while 1: + sys.stdout.write(sheet + prompt) + choice = input().lower() + if default is not None and choice == '': + print("Delete\033[1;31m %s \033[0mfile " % sheet) + os.remove(sheet) + return default + elif choice in valid.keys(): + print("Delete\033[1;31m %s \033[0mfile " % sheet) + os.remove(sheet) + return valid[choice] + else: + sys.stdout.write("Please respond with 'yes', 'no' or 'quit'.\n") + + +