def download_image_coords(self, first_num, second_num): selected_image_num = pure.find_number_map(int(first_num), int(second_num)) if not selected_image_num: print('Invalid number!') else: self.download_image_num(selected_image_num)
def go_artist_gallery_coords(self, first_num, second_num): selected_image_num = pure.find_number_map(int(first_num), int(second_num)) if selected_image_num is False: # 0 is valid! print('Invalid number!') else: self.go_artist_gallery_num(selected_image_num)
def test_find_number_map(): assert pure.find_number_map(1, 1) == 0 assert pure.find_number_map(5, 1) == 4 assert pure.find_number_map(2, 5) == 21 assert pure.find_number_map(5, 6) == 29 assert pure.find_number_map(5, 1) == 4 assert pure.find_number_map(-1, -1) == False assert pure.find_number_map(0, 0) == False
def handle_prompt(self, keyseqs, gallery_command, selected_image_num, first_num, second_num): # Display image (using either coords or image number), the show this prompt if gallery_command == 'b': pass # Stop gallery instance, return to previous state elif gallery_command == 'r': self.reload() elif keyseqs[0] == 'i': self.view_image(selected_image_num) elif keyseqs[0].lower() == 'a': print('Invalid command! Press h to show help') prompt.gallery_like_prompt(self) # Go back to while loop elif len(keyseqs) == 2: selected_image_num = pure.find_number_map(first_num, second_num) if not selected_image_num: print('Invalid number!') prompt.gallery_like_prompt(self) # Go back to while loop else: self.view_image(selected_image_num)
def handle_prompt(self, keyseqs, gallery_command, selected_image_num, first_num, second_num): # "b" must be handled first, because keyseqs might be empty if gallery_command == 'b': print('Invalid command! Press h to show help') prompt.gallery_like_prompt(self) # Go back to while loop elif gallery_command == 'r': self.reload() elif keyseqs[0] == 'i': self.view_image(selected_image_num) elif keyseqs[0] == 'a': self.go_artist_gallery_coords(first_num, second_num) elif keyseqs[0] == 'A': self.go_artist_gallery_num(selected_image_num) elif len(keyseqs) == 2: selected_image_num = pure.find_number_map(first_num, second_num) if not selected_image_num: print('Invalid number!') prompt.gallery_like_prompt(self) # Go back to while loop else: self.view_image(selected_image_num)
def gallery_like_prompt(gallery_like_class): """ Only contains logic for interpreting key presses, and do the correct action Sequence means a combination of more than one key. When a sequenceable key is pressed, wait for the next keys in the sequence If the sequence is valid, execute their corresponding actions Otherwise for keys that do not need a sequence, execute their actions normally """ sequenceable_keys = ('o', 'd', 'i', 'O', 'D', 'a', 'A') with TERM.cbreak(): keyseqs = [] seq_num = 0 selected_image_num, first_num, second_num = None, None, None print('Enter a gallery command:') while True: gallery_command = TERM.inkey() # Wait for the rest of the sequence if gallery_command in sequenceable_keys: keyseqs.append(gallery_command) print(keyseqs) seq_num += 1 elif gallery_command.code == 361: # Escape keyseqs = [] seq_num = 0 print(keyseqs) # Digits continue the sequence elif gallery_command.isdigit(): keyseqs.append(gallery_command) print(keyseqs) # End of the sequence... # Two digit sequence -- view image given coords if seq_num == 1 and keyseqs[0].isdigit() and keyseqs[1].isdigit(): first_num = int(keyseqs[0]) second_num = int(keyseqs[1]) selected_image_num = pure.find_number_map(first_num, second_num) break # leave cbreak(), go to image prompt # One letter two digit sequence elif seq_num == 2 and keyseqs[1].isdigit() and keyseqs[2].isdigit(): first_num = keyseqs[1] second_num = keyseqs[2] # Open or download given coords if keyseqs[0] == 'o': gallery_like_class.open_link_coords(first_num, second_num) elif keyseqs[0] == 'd': gallery_like_class.download_image_coords(first_num, second_num) elif keyseqs[0] == 'a': break # Open, download, or view image, given image number selected_image_num = int(f'{first_num}{second_num}') if keyseqs[0] == 'O': gallery_like_class.open_link_num(selected_image_num) elif keyseqs[0] == 'D': gallery_like_class.download_image_num(selected_image_num) elif keyseqs[0] == 'A': break elif keyseqs[0] == 'i': break # leave cbreak(), go to image prompt # Reset sequence info after running everything keyseqs = [] seq_num = 0 # Not the end of the sequence yet, continue while block else: seq_num += 1 # No sequence, execute their functions immediately elif gallery_command == 'n': gallery_like_class.next_page() elif gallery_command == 'p': gallery_like_class.previous_page() elif gallery_command == 'q': print('Are you sure you want to exit?') ask_quit() # If exit cancelled print('Enter a gallery command:') elif gallery_command == 'b': break elif gallery_command == 'r': break elif gallery_command == 'm': print(gallery_like_class.__doc__) elif gallery_command == 'h': gallery_like_class.help() elif gallery_command.code == 343: # Enter pass elif gallery_command: print('Invalid command! Press h to show help') keyseqs = [] seq_num = 0 # End if # End while # End cbreak() gallery_like_class.handle_prompt(keyseqs, gallery_command, selected_image_num, first_num, second_num)