def start_load(self): """Loads a list from a file. :return: null """ # Request filename. line_y, line_x = util.center_start(self._list_height-2, self._list_width-2, 1, 16) self._list_display.addstr(line_y+4, 1, ' '*(self._list_width-2)) self._list_display.addstr(line_y+4, line_x, "Enter filename: ") self._list_display.refresh() # Get filename curses.echo() filename = self._list_display.getstr().decode(encoding="utf-8") filename = 'shopping_lists/data/' + filename curses.noecho() # Try to load list, and recursively call start_command if the file isn't loaded. try: new_list = Recipe.create_from_file(filename) # Add ingredients to the shopping list. new_list.add_to(self._shopping_list) # Alert user that list was updated.. line_y, line_x = util.center_start(self._list_height-2, self._list_width-2, 1, len(filename)+13) self._list_display.addstr(line_y+5, line_x, "{} fully loaded".format(filename)) self._list_display.refresh() except (FileNotFoundError, IsADirectoryError): # Alert user that file was not found. line_y, line_x = util.center_start(self._list_height-2, self._list_width-2, 1, 13) self._list_display.addstr(line_y+5, line_x, "File not found.") self._list_display.refresh() # Retry getting a command self.start_shopping_list()
def __init__(self, console_height, console_width, game_height, game_width, conway): """Initialize ConwayScreen. :param console_height: the height of the console :param console_width: the width of the console :param game_height: the height of the game :param game_width: the width of the game :param conway: a Conway graph :return: null """ self.conway = conway self.game_pad = curses.newpad(game_height, game_width) # Start and stop points for the graph [start, stop). self.start_y, self.start_x = util.center_start(console_height, console_width, game_height, game_width) # Stop points are a function based on the start. self.stop_y, self.stop_x = self.start_y + game_height, self.start_x + game_width # Initializes pause window for use in pause(). pause_height, pause_width = 8, 50 pause_y, pause_x = util.center_start(console_height, console_width, pause_height, pause_width) self.pause_window = curses.newwin(pause_height, pause_width, pause_y, pause_x) # Surround Conway graph with a box util.color_box(self.game_pad, 0, 0, game_height, game_width, 0)
def __init__(self, console_height, console_width): """Create a main screen. :param console_height: the height of the console :param console_width: the width of the console :return: null """ # List should be two smaller in each direction because of surrounding border. self._list_height, self._list_width = console_height-2, console_width-2 # Center the window based on the size of the console. display_start_y, display_start_x = util.center_start(console_height, console_width, self._list_height, self._list_width) # Initialize a Recipe to serve as a shopping list. self._shopping_list = Recipe() # Create window that will act as main visual. self._list_display = curses.newwin(self._list_height, self._list_width, display_start_y, display_start_x) # Add visual detail to window. self._list_display.bkgd(' ', curses.color_pair(1)) util.color_box(self._list_display, 0, 0, self._list_height-1, self._list_width-1, 3) # Initializes help window for use in pause(). help_height, help_width = 12, 50 help_y, help_x = util.center_start(console_height, console_width, help_height, help_width) self.help_window = curses.newwin(help_height, help_width, help_y, help_x)
def show_list(self): """Display the shopping list on screen.""" # Format the rows and columns the chart. row = 2 column = 0 # Set up chart header. _, line_x = util.center_start(self._list_height-2, self._list_width-2, 1, 14) self._list_display.addstr(1, line_x, "Shopping list:") for ingredient in sorted(self._shopping_list): # Format where to place each element in chart. start_y = row start_x = column*20+1 # Truncate ingredient if longer than 18 characters. full_ingredient = self._shopping_list.show_ingredient(ingredient) truncated_ingredient = full_ingredient[:18] + (full_ingredient[18:] and '..') # Keep printing until list runs out of space. try: self._list_display.addstr(start_y, start_x, truncated_ingredient) except curses.error: # Window has run out of room, so stop printing. break row += 1 # Every column, restart row number. if row == self._list_height-2: column += 1 row = 2 self._list_display.refresh()
def show_intro(self): """Show welcome text.""" # Calling util.center_start using the length of the string will center the string. # Line length acquired by adding str(len(...)) around text and running program. line_1_y, line_1_x = util.center_start(self._list_height-2, self._list_width-2, 1, 18) self._list_display.addstr(line_1_y, line_1_x, "Welcome to RecAppE") line_2_y, line_2_x = util.center_start(self._list_height-2, self._list_width-2, 1, 42) self._list_display.addstr(line_2_y+1, line_2_x, "To create a new shopping list, press enter") line_3_y, line_3_x = util.center_start(self._list_height-2, self._list_width-2, 1, 52) self._list_display.addstr(line_3_y+2, line_3_x, "To add to a previously made shopping list, press 'l'") line_4_y, line_4_x = util.center_start(self._list_height-2, self._list_width-2, 1, 30) self._list_display.addstr(line_4_y+3, line_4_x, "To quit, press 'q' at any time") self._list_display.refresh()
def __init__(self, console_height, console_width): """Initialize IntroScreen :param console_height: the height of the console :param console_width: the width of the console :return: null """ # If the console window is less wide than the text, it needs to adjust appropriately. self.console_height, self.console_width = console_height, console_width self.win_height, self.win_width = 8, 50 if console_width >= 50 else console_width begin_y, begin_x = util.center_start(console_height, console_width, self.win_height, self.win_width) self.intro_win = curses.newwin(self.win_height, self.win_width, begin_y, begin_x)
def request_element(self, request): """Ask for an element. :param request: the request for the element (requires string) :return: the user's response """ # Clear row in preparation of getting element. self._list_display.addstr(self._list_height-2, 1, ' '*(self._list_width-2)) # Format request, then request. _, line_x = util.center_start(self._list_height-2, self._list_width-2, 1, len(request)) self._list_display.addstr(self._list_height-2, line_x, request) self._list_display.refresh() # Get element. curses.echo() element = self._list_display.getstr().decode(encoding="utf-8") curses.noecho() return element
def start_shopping_list(self): """Start the main screen by getting a command from a key. Pressing 'q' will quit app. :return: a reference to the main screen """ key = self._list_display.getkey() if key == '\n': # Shopping list is already empty so program can continue return self elif key == 'l': # Load a shopping list from saves. self.start_load() return self elif key == 'q': # quit app. exit() else: # Use same method for centering text as show_intro(); add text below show_intro()'s. line_y, line_x = util.center_start(self._list_height-2, self._list_width-2, 1, 28) self._list_display.addstr(line_y+4, line_x, "Command not found, try again") self._list_display.refresh() self.start_shopping_list()
def do_command(self, key=None): """Execute a command based on key input.""" if key is None: key = self._list_display.getkey() # Clear line in case a previous command had written to it. self._list_display.addstr(self._list_height-2, 1, ' '*(self._list_width-2)) self._list_display.refresh() if key == 'l': # Load a recipe. try: filename = self.request_element("Enter name of recipe to load: ") self.add_recipe(filename) except (FileNotFoundError, IsADirectoryError): # Alert user that recipe wasn't loaded. _, line_x = util.center_start(self._list_height-2, self._list_width-2, 1, 18) self._list_display.addstr(self._list_height-2, 1, ' '*(self._list_width-2)) self._list_display.addstr(self._list_height-2, line_x, "File not found") self._list_display.refresh() self.do_command() elif key == 'a': # Add an ingredient. try: # Pull data to add as a new ingredient. item_name = self.request_element("Enter name of item: ") item_quantity = int(self.request_element("Enter quantity of item: ")) item_qualifier = self.request_element("Enter qualifier of item: ") self.add_item(item_name, item_quantity, item_qualifier) except ValueError: self._list_display.addstr(self._list_height-2, 1, ' '*(self._list_width-2)) _, line_x = util.center_start(self._list_height-2, self._list_width-2, 1, 18) self._list_display.addstr(self._list_height-2, line_x, "Could not add item") self._list_display.refresh() self.do_command() elif key == 'q': # Quit app. exit() elif key == 's': # Save shopping list. try: filename = self.request_element("Enter name to save list as: ") self.save_list(filename) except IsADirectoryError: # User didn't enter file, so tell the user and retry. self._list_display.addstr(self._list_height-2, 1, ' '*(self._list_width-2)) _, line_x = util.center_start(self._list_height-2, self._list_width-2, 1, 23) self._list_display.addstr(self._list_height-2, line_x, "File unable to be saved") self._list_display.refresh() self.do_command() elif key == 'w': # Save shopping list as a recipe. try: filename = self.request_element("Enter name to save recipe as: ") self.save_as_recipe(filename) except IsADirectoryError: # User didn't enter file, so tell the user and retry. self._list_display.addstr(self._list_height-2, 1, ' '*(self._list_width-2)) _, line_x = util.center_start(self._list_height-2, self._list_width-2, 1, 23) self._list_display.addstr(self._list_height-2, line_x, "File unable to be saved") self._list_display.refresh() self.do_command() elif key == 'c': # Clear the shopping list. self._shopping_list.clear() elif key == 'r': # Remove item. try: item_name = self.request_element("Enter item to remove: ") self.remove_item(item_name) except ValueError: # Item wasn't in list, so tell the user and retry. self._list_display.addstr(self._list_height-2, 1, ' '*(self._list_width-2)) _, line_x = util.center_start(self._list_height-2, self._list_width-2, 1, 15) self._list_display.addstr(self._list_height-2, line_x, "Item not found") self._list_display.refresh() self.do_command() elif key == 'h': # Show help window self.help() else: # Tell the user that the key was an invalid command. _, line_x = util.center_start(self._list_height-2, self._list_width-2, 1, 18) self._list_display.addstr(self._list_height-2, line_x, "Command not found") self._list_display.refresh() self.do_command()