def delete(record): # show selected record skin Utility.stdscr.clear() skins.main() skins.selected_record() # show actual selected record Utility.stdscr.addstr(5, 27, str(record['id'])) Utility.stdscr.addstr(6, 27, str(record['name'])) Utility.stdscr.addstr(7, 27, str(record['phone'])) Utility.stdscr.addstr(10, 20, 'ARE YOU SURE YOU WANT TO DELETE THIS RECORD ?') Utility.stdscr.attron(curses.color_pair(1)) Utility.stdscr.addstr(12, 20, '[Y]') Utility.stdscr.attroff(curses.color_pair(1)) Utility.stdscr.addstr(12, 23, ' - YES') Utility.stdscr.attron(curses.color_pair(1)) Utility.stdscr.addstr(14, 20, '[N]') Utility.stdscr.attroff(curses.color_pair(1)) Utility.stdscr.addstr(14, 23, ' - NO') # '121'=y, '89'='Y' and '110'=n, '78'=N while 1: key = Utility.stdscr.getch() if key == 89 or key == 121: Utility.delete_record(record['id']) Utility.set_selector(1) Utility.set_last_page_phonebook(0) break elif key == 78 or key == 110: break
def selected_record(): # show selected record skin Utility.stdscr.clear() skins.main() skins.selected_record() # show actual selected record selected_record = Utility.records_on_page[Utility.selector - 1] Utility.stdscr.addstr(5, 27, str(selected_record['id'])) Utility.stdscr.addstr(6, 27, str(selected_record['name'])) Utility.stdscr.addstr(7, 27, str(selected_record['phone'])) Utility.stdscr.attron(curses.color_pair(1)) Utility.stdscr.addstr(10, 20, '[E]') Utility.stdscr.attroff(curses.color_pair(1)) Utility.stdscr.addstr(10, 23, ' - EDIT') Utility.stdscr.attron(curses.color_pair(1)) Utility.stdscr.addstr(12, 20, '[D]') Utility.stdscr.attroff(curses.color_pair(1)) Utility.stdscr.addstr(12, 23, ' - DELETE') Utility.stdscr.attron(curses.color_pair(1)) Utility.stdscr.addstr(14, 20, '[ESC]') Utility.stdscr.attroff(curses.color_pair(1)) Utility.stdscr.addstr(14, 25, ' - CANCEL') # selected_record event loop key = None while 1: key = Utility.stdscr.getch() # 'e'=101 and 'E'=69 if key == 69 or key == 101: edit(selected_record) break # 'd'=100 and 'D'=68 elif key == 100 or key == 68: delete(selected_record) break elif key == 27: break # refresh screen show records again Utility.stdscr.clear() load_main_details(Utility.records_per_page, 0) Utility.current_page = 1
def load_main_details(records_per_page, offset, file_to_read=0): # clear screen Utility.stdscr.clear() # load appropriate skin based on file_to_read if file_to_read: skins.main() skins.search() else: skins.main() # check the sort_type if Utility.sort_type == 0: records = Utility.get_records(records_per_page, offset, file_to_read) elif Utility.sort_type == 1: records = Utility.get_records_reverse(records_per_page, offset, file_to_read) # show records if records: Utility.show_records(records) # print total records at the bottom total = Utility.get_total_records(file_to_read) Utility.stdscr.addstr( 23, 17, "{} | PAGE: {}".format(total, Utility.current_page)) return 1 else: Utility.stdscr.addstr(23, 17, '0') Utility.stdscr.addstr(23, 50, '0 records found') # refresh screen Utility.stdscr.refresh() return 0
def edit(record): # show selected record skin Utility.stdscr.clear() skins.main() skins.selected_record() # show actual selected record Utility.stdscr.addstr(5, 27, str(record['id'])) Utility.stdscr.addstr(6, 27, str(record['name'])) Utility.stdscr.addstr(7, 27, str(record['phone'])) # display cursor Utility.cursor_display(1) # display labels Utility.stdscr.addstr(10, 20, 'NEW NAME: ') Utility.stdscr.addstr(11, 20, 'NEW PHONE: ') # accept and validate name Utility.stdscr.addstr(23, 50, 'Please enter a name.') name = Utility.bytes_to_str(Utility.stdscr.getstr(10, 30, 30)) while 1: if name == '': skins.clear_message() Utility.stdscr.addstr(23, 50, 'Name must not be empty.') Utility.stdscr.addstr(10, 30, ' ' * 30) Utility.stdscr.refresh() name = Utility.bytes_to_str(Utility.stdscr.getstr(10, 30, 30)) else: break # accept and validate phone Utility.stdscr.addstr(23, 50, 'Please enter a phone number.') phone = Utility.bytes_to_str(Utility.stdscr.getstr(11, 31, 7)) numbers = set('0123456789') while 1: if phone == '': skins.clear_message() Utility.stdscr.addstr(23, 50, 'Number must not be empty.') Utility.stdscr.addstr(11, 31, ' ' * 7) Utility.stdscr.refresh() phone = Utility.bytes_to_str(Utility.stdscr.getstr(11, 31, 7)) elif set(phone).issubset(numbers) == False or len(phone) < 7: skins.clear_message() Utility.stdscr.addstr(23, 50, 'Incorrect Number.') Utility.stdscr.addstr(11, 31, ' ' * 7) Utility.stdscr.refresh() phone = Utility.bytes_to_str(Utility.stdscr.getstr(11, 31, 7)) else: break # remove cursor Utility.cursor_display(0) # checks if user wants to save or cancel Utility.cursor_display(0) Utility.stdscr.attron(curses.color_pair(1)) Utility.stdscr.addstr(14, 20, '[S]') Utility.stdscr.attroff(curses.color_pair(1)) Utility.stdscr.addstr(14, 23, ' - SAVE') Utility.stdscr.attron(curses.color_pair(1)) Utility.stdscr.addstr(16, 20, '[ESC]') Utility.stdscr.attroff(curses.color_pair(1)) Utility.stdscr.addstr(16, 25, ' - CANCEL') key = None # 's'=115, 'S'=83 and ESC = 27 while (key != 83 and key != 115): key = Utility.stdscr.getch() if key == 115 or key == 83: # edit the record Utility.edit_record(record['id'], name, phone) break elif key == 27: break
def add(): # add skins Utility.stdscr.clear() skins.main() skins.add() # show total records lines = Utility.get_total_records() Utility.stdscr.addstr(23, 17, str(lines)) Utility.stdscr.refresh() # get the current id current_id = Utility.get_id() Utility.stdscr.addstr(5, 26, current_id) # display cursor Utility.cursor_display(1) # accept and validate name Utility.stdscr.addstr(23, 50, 'Please enter a name.') name = Utility.bytes_to_str(Utility.stdscr.getstr(6, 27, 30)) while 1: if name == '': skins.clear_message() Utility.stdscr.addstr(23, 50, 'Name must not be empty.') Utility.stdscr.addstr(6, 27, ' ' * 30) Utility.stdscr.refresh() name = Utility.bytes_to_str(Utility.stdscr.getstr(6, 27, 30)) else: break # clear validation messages skins.clear_message() # accept and validate phone Utility.stdscr.addstr(23, 50, 'Please enter a 7-digit number.') phone = Utility.bytes_to_str(Utility.stdscr.getstr(7, 27, 7)) numbers = set('0123456789') while 1: if phone == '': skins.clear_message() Utility.stdscr.addstr(23, 50, 'Number must not be empty.') Utility.stdscr.addstr(7, 27, ' ' * 7) Utility.stdscr.refresh() phone = Utility.bytes_to_str(Utility.stdscr.getstr(7, 27, 7)) elif set(phone).issubset(numbers) == False or len(phone) < 7: skins.clear_message() Utility.stdscr.addstr(23, 50, 'Incorrect Number.') Utility.stdscr.addstr(7, 27, ' ' * 7) Utility.stdscr.refresh() phone = Utility.bytes_to_str(Utility.stdscr.getstr(7, 27, 7)) else: break # clear validation messages skins.clear_message() # checks if user wants to save or cancel Utility.cursor_display(0) Utility.stdscr.attron(curses.color_pair(1)) Utility.stdscr.addstr(10, 20, '[S]') Utility.stdscr.attroff(curses.color_pair(1)) Utility.stdscr.addstr(10, 23, ' - SAVE') Utility.stdscr.attron(curses.color_pair(1)) Utility.stdscr.addstr(12, 20, '[ESC]') Utility.stdscr.attroff(curses.color_pair(1)) Utility.stdscr.addstr(12, 25, ' - CANCEL') key = None # 's'=115, 'S'=83 and ESC = 27 while (key != 83 and key != 115): key = Utility.stdscr.getch() if key == 115 or key == 83: Utility.save_record(int(current_id), name, phone) Utility.update_current_id(int(current_id)) break elif key == 27: break # refresh screen show records again Utility.stdscr.clear() found_records = load_main_details(Utility.records_per_page, 0) Utility.set_selector(Utility.selector) Utility.current_page = 1 return found_records
def search(): # add skins Utility.stdscr.clear() skins.main() skins.search() y, x = Utility.stdscr.getmaxyx() # display selection Utility.stdscr.attron(curses.color_pair(1)) Utility.stdscr.addstr(5, 20, '[1]') Utility.stdscr.attroff(curses.color_pair(1)) Utility.stdscr.addstr(5, 23, ' - SEARCH BY NAME') Utility.stdscr.attron(curses.color_pair(1)) Utility.stdscr.addstr(7, 20, '[2]') Utility.stdscr.attroff(curses.color_pair(1)) Utility.stdscr.addstr(7, 23, ' - SEARCH BY PHONE NUMBER') Utility.stdscr.attron(curses.color_pair(1)) Utility.stdscr.addstr(9, 20, '[ESC]') Utility.stdscr.attroff(curses.color_pair(1)) Utility.stdscr.addstr(9, 25, ' - CANCEL') # check user selection Utility.stdscr.addstr(23, 50, 'Search, select your option.') key = None # '1'=49 and '2'=50 while (key != 49 and key != 50): key = Utility.stdscr.getch() if key == 49: # 1 skins.clear_message() Utility.stdscr.addstr(23, 50, 'Search, by name') search_by_name() break elif key == 50: # 2 skins.clear_message() Utility.stdscr.addstr(23, 50, 'Search, by phone') search_by_phone() break # checks for window resize event elif curses.is_term_resized(y, x) == True: Utility.stdscr.clear() curses.resizeterm(25, 80) skins.main() skins.search() # display selection Utility.stdscr.attron(curses.color_pair(1)) Utility.stdscr.addstr(5, 20, '[1]') Utility.stdscr.attroff(curses.color_pair(1)) Utility.stdscr.addstr(5, 23, ' - SEARCH BY NAME') Utility.stdscr.attron(curses.color_pair(1)) Utility.stdscr.addstr(7, 20, '[2]') Utility.stdscr.attroff(curses.color_pair(1)) Utility.stdscr.addstr(7, 23, ' - SEARCH BY PHONE NUMBER') Utility.stdscr.attron(curses.color_pair(1)) Utility.stdscr.addstr(9, 20, '[ESC]') Utility.stdscr.attroff(curses.color_pair(1)) Utility.stdscr.addstr(9, 25, ' - CANCEL') elif key == 27: break # refresh screen show records again Utility.stdscr.clear() load_main_details(Utility.records_per_page, 0) Utility.current_page = 1 Utility.set_selector(1)
def search_by_phone(): # add skins Utility.stdscr.clear() skins.main() skins.search() # display cursor Utility.cursor_display(1) y, x = Utility.stdscr.getmaxyx() # accept and validate name search Utility.stdscr.addstr(6, 20, 'Phone: ') Utility.stdscr.addstr(23, 50, 'Please enter a phone number.') phone = Utility.bytes_to_str(Utility.stdscr.getstr(6, 27, 7)) numbers = set('0123456789') while 1: if phone == '': skins.clear_message() Utility.stdscr.addstr(23, 50, 'Number must not be empty.') Utility.stdscr.addstr(6, 27, ' ' * 7) Utility.stdscr.refresh() phone = Utility.bytes_to_str(Utility.stdscr.getstr(6, 27, 7)) elif set(phone).issubset(numbers) == False: skins.clear_message() Utility.stdscr.addstr(23, 50, 'Incorrect Number.') Utility.stdscr.addstr(6, 27, ' ' * 7) Utility.stdscr.refresh() phone = Utility.bytes_to_str(Utility.stdscr.getstr(6, 27, 7)) else: break # add searchs results to temp_search.txt Utility.search_phone(phone) # hide cursor Utility.cursor_display(0) # load records found_records = load_main_details(Utility.records_per_page, 0, 1) # set default selector value if found_records: Utility.set_selector(1) Utility.set_last_page_phonebook(1) # seach name event loop key = Utility.stdscr.getch() while 1: # add new records if key == curses.KEY_F1: # reset current page to 1 Utility.current_page = 1 found_records = add() Utility.set_selector(1) Utility.set_last_page_phonebook(0) break # search records elif key == curses.KEY_F2: # reset current page to 1 Utility.current_page = 1 search() break # sort order elif key == curses.KEY_F3 and found_records: # toggle sort type value if Utility.sort_type == 0: Utility.sort_type = 1 elif Utility.sort_type == 1: Utility.sort_type = 0 # reset current page to 1 Utility.current_page = 1 # reload records load_main_details(Utility.records_per_page, 0, 1) # show currently selected sort type if Utility.sort_type == 0: sort_by = 'Ascending' elif Utility.sort_type == 1: sort_by = 'Descending' # set selector Utility.set_selector(1) Utility.stdscr.addstr(23, 50, "Sort By: {}".format(sort_by)) # show previous records elif key == curses.KEY_F4 and found_records: previous_page(1) Utility.set_selector(1) # show next records elif key == curses.KEY_F5 and found_records: next_page(1) Utility.set_selector(1) # enter key for selecting a record elif key == 10 and found_records: selected_record() Utility.set_selector(1) break # navigating records using arrow up elif key == curses.KEY_UP and found_records: curses.beep() if Utility.selector > 1: # greater than the first page Utility.selector -= 1 Utility.set_selector(Utility.selector) else: if Utility.current_page != 1: previous_page(1) Utility.selector = Utility.total_records_on_page Utility.set_selector(Utility.selector) # navigating records using arrow down elif key == curses.KEY_DOWN and found_records: curses.beep() if Utility.selector < Utility.total_records_on_page: # less than the last page Utility.selector += 1 Utility.set_selector(Utility.selector) else: if Utility.current_page != Utility.phonebook_last_page: next_page(1) Utility.selector = 1 Utility.set_selector(Utility.selector) # checks for window resize event elif curses.is_term_resized(y, x) == True: Utility.stdscr.clear() curses.resizeterm(25, 80) load_main_details(Utility.records_per_page, 0, 1) Utility.set_selector(Utility.selector) # escape to exit elif key == 27: Utility.set_last_page_phonebook(0) break key = Utility.stdscr.getch()