def compose_in_editor(two_way_card): if two_way_card: print_info('Two-way mode is ON.') else: print_info('Two-way mode is OFF.') print_instruction('Hit Enter to continue in editor mode, or enter one of these options:') print_instruction('q: Quit.') print_instruction('t: Toggle two-way card and continue in editor mode.') print_instruction('m: Continue in normal multi-line mode.') print_instruction('s: Continue in single-line mode.') while True: selection = colored_prompt('[q/t/m/s]: ') if selection in ['q', 't', 'm', 's', '']: break if selection == 'q': return None, None, None, None if selection == 't': two_way_card = not two_way_card if selection == 'm': return new_card(two_way_card, single_line_mode=False, editor_mode=False) if selection == 's': return new_card(two_way_card, single_line_mode=True, editor_mode=False) bogus_timestamp = '1991-08-25 20:57:08' empty_card = Card('', '', 0, bogus_timestamp) card = edit(empty_card, two_way_card) return card, two_way_card, False, True
def main(): # Initialise colorama colorama.init() valid_args = ['-n', '-n2', '-s', '-s2', '-e', '-e2', '-b', '-bf', '-bb', '-bs', '-bl', '-br'] if len(sys.argv) > 1 and sys.argv[1] not in valid_args: print_usage_info(sys.argv) if sys.argv[1] not in ['-h', '--help']: sys.exit(1) sys.exit() db_file = pick_db_file() conn, cursor = db_connection.connect(db_file) card_repository.create_table_if_not_exists(conn, cursor) if len(sys.argv) == 1: table_is_empty = card_repository.check_if_empty(cursor) if table_is_empty: print_error("You don't have any cards yet.") print_instruction( 'Create some cards by launching garrick with one of the following options first:' ) print_instruction('\t-n\tCreate cards starting in one-way mode.') print_instruction('\t-n2\tCreate cards starting in two-way mode.') print_instruction('\t-s\tCreate cards starting in single-line and one-way mode.') print_instruction('\t-s2\tCreate cards starting in single-line and two-way mode.') print_instruction('\t-e\tCreate cards starting in editor mode and in one-way mode.') print_instruction('\t-s2\tCreate cards starting in editor mode and in two-way mode.') else: review.review(conn, cursor) elif sys.argv[1] == '-n': new_cards(conn, cursor, two_way_card=False, single_line_mode=False, editor_mode=False) elif sys.argv[1] == '-n2': new_cards(conn, cursor, two_way_card=True, single_line_mode=False, editor_mode=False) elif sys.argv[1] == '-s': new_cards(conn, cursor, two_way_card=False, single_line_mode=True, editor_mode=False) elif sys.argv[1] == '-s2': new_cards(conn, cursor, two_way_card=True, single_line_mode=True, editor_mode=False) elif sys.argv[1] == '-e': new_cards(conn, cursor, two_way_card=False, single_line_mode=False, editor_mode=True) elif sys.argv[1] == '-e2': new_cards(conn, cursor, two_way_card=True, single_line_mode=False, editor_mode=True) elif sys.argv[1] == '-b': review.browse_by_regex(conn, cursor) elif sys.argv[1] == '-bf': review.browse_by_regex_front(conn, cursor) elif sys.argv[1] == '-bb': review.browse_by_regex_back(conn, cursor) elif sys.argv[1] == '-bs': review.browse_by_score(conn, cursor) elif sys.argv[1] == '-bl': review.browse_by_last_viewed(conn, cursor) elif sys.argv[1] == '-br': review.browse_by_last_viewed_reverse(conn, cursor) print_info('Kbai') db_connection.disconnect(conn, cursor)
def include_flipped_card(): print_info('This is a two-way card.') print_info('Should your changes also affect the flipped version of this card?') while True: answer = colored_prompt('[y/n]: ') if answer in ['y', 'n']: break if answer == 'y': return True else: return False
def parse_edits(lines, tags): if not all([x in lines for x in tags]): print_error('Error: The lines "{}", "{}", and/or "{}" are messed up.'.format(*tags)) colored_getpass('Hit Enter to start over.') return None, None front_index = lines.index(tags[0]) back_index = lines.index(tags[1]) score_index = lines.index(tags[2]) front_lines = lines[front_index + 1:back_index] back_lines = lines[back_index + 1:score_index] front = '\n'.join(front_lines) back = '\n'.join(back_lines) last_viewed = get_timestamp() if len(lines) < score_index + 2: print_error('Error: No score.') colored_getpass('Hit Enter to go fix it.') faulty_card = Card(front, back, 0, last_viewed) faulty = True return faulty_card, faulty score_string = lines[score_index + 1] if (not score_string.isdigit()) or (not int(score_string) in range(6)): print_error('Error: {} is not a valid score.'.format(score_string)) print_info('Valid scores are 0, 1, 2, 3, 4, 5.') colored_getpass('Hit Enter to go fix it.') faulty_card = Card(front, back, 0, last_viewed) faulty = True return faulty_card, faulty score = int(score_string) edited_card = Card(front, back, score, last_viewed) faulty = False return edited_card, faulty
def new_cards(conn, cursor, two_way_card, single_line_mode, editor_mode): while True: card, two_way_card, single_line_mode, editor_mode = \ new_card(two_way_card, single_line_mode, editor_mode) if card == None: break card_repository.insert(conn, cursor, card) if two_way_card: flipped_card = Card( card.back, card.front, card.score, card.last_viewed ) card_repository.insert(conn, cursor, flipped_card) print_info('Saved.')
def iterate(conn, cursor, iterator, count): if count == 0: print_error('No results.') elif count == 1: print_info('[1 CARD.]') else: print_info('[{} CARDS.]'.format(count)) try: for card in iterator: print_instruction('[Ctrl+C to quit.]') updated_card, contents_changed = review_card(card) is_two_way_card = card_repository.is_two_way_card(cursor, card) if is_two_way_card and contents_changed: flipped_card_too = include_flipped_card() else: flipped_card_too = False if updated_card == None: card_repository.delete(conn, cursor, card) if flipped_card_too: card_repository.delete_flipped_card(conn, cursor, card) count -= 1 # Not an error, but I feel this should be red. print_error('DELETED.') table_is_empty = card_repository.check_if_empty(cursor) if table_is_empty: print_info('You have no more cards!') break else: if contents_changed: while True: card_repository.insert(conn, cursor, updated_card) if flipped_card_too: card_repository.insert_flipped_card(conn, cursor, updated_card) else: bump_flipped_card_timestamp(conn, cursor, card) print_instruction('Create another version based on this same card?') print_info('Protip: This is how you split one card into several cards.') while True: answer = colored_prompt('[y/n]: ') if answer in ['y', 'n']: break if answer == 'n': break else: updated_card = edit(card, flipped_card_too) card_repository.delete(conn, cursor, card) if flipped_card_too: card_repository.delete_flipped_card(conn, cursor, card) else: card_repository.insert(conn, cursor, updated_card) card_repository.delete(conn, cursor, card) bump_flipped_card_timestamp(conn, cursor, card) count -= 1 if count > 0: print_info('[{} MORE:]'.format(count)) except KeyboardInterrupt: print()