Exemplo n.º 1
0
def scroll_prompt(tracker, data, max_images):
    show = True
    terminal_page = 0
    images = []

    if tracker is lscat.TrackDownloadsUsers:
        max_scrolls = utils.max_terminal_scrolls(data, False)
    else:
        max_scrolls = utils.max_terminal_scrolls(data, True)

    with TERM.cbreak():
        while True:
            if show:
                lscat.api.hide_all(images)
                myslice = utils.slice_images(max_images, terminal_page)
                images = lscat.handle_scroll(tracker, data, myslice)

            ans = TERM.inkey()
            utils.quit_on_q(ans)

            if ans.name == 'KEY_DOWN' and terminal_page + 1 < max_scrolls:
                terminal_page += 1
                show = True

            elif ans.name == 'KEY_UP' and terminal_page > 0:
                terminal_page -= 1
                show = True

            else:
                print('Out of bounds!')
                show = False
Exemplo n.º 2
0
def ask_quit() -> 'IO':
    """Ask for quit confirmation, no need to press enter"""
    printer.print_bottom('\nAre you sure you want to exit?', offset=1)
    with TERM.cbreak():
        ans = TERM.inkey()
        if ans == 'y' or ans == 'q' or ans.name == 'KEY_ENTER':
            sys.exit(0)
Exemplo n.º 3
0
    def start(self) -> (int, int):
        """Main loop"""
        self.maybe_move_down()

        self.spaces = self.start_spaces
        self.valid = True

        with TERM.cbreak():
            while True:
                if self.is_input_valid():
                    self.hide_show_print()

                ans = TERM.inkey()
                utils.quit_on_q(ans)

                if ans.name == 'KEY_ENTER' and self.image:
                    self.maybe_erase()
                    lscat.api.hide(self.image)
                    if self.static:
                        lscat.api.hide(self.static)
                    return self.return_tup()

                if ans in PLUS:
                    self.spaces += 1
                    self.valid = True

                elif ans in MINUS and self.spaces > self.start_spaces:
                    self.spaces -= 1
                    self.valid = True

                else:
                    self.valid = False
Exemplo n.º 4
0
def center_spaces_assistant():
    """=== Ueberzug center image ===
    Use +/= to move the image right, and -/_ to move it left
    Adjust the position to the center of your terminal

    Use q to exit the program, and press enter to confirm the current position
    """
    if not config.api.use_ueberzug():
        print('Center images assistant is only for ueberzug!')
        return -1

    # Setup
    image = WELCOME_IMAGE.parent / '79494300_p0.png'
    valid = True
    spacing = 0
    i = 0

    # Start
    printer.print_doc(center_spaces_assistant.__doc__)
    print('\n' * (TERM.height - 9), f'Current position: {spacing:01}')

    with TERM.cbreak():
        while True:
            if valid:
                printer.move_cursor_up(1)
                print(f'Current position: {spacing:02}')
                placement = lscat.api.show(image, spacing, 0, 500)

            ans = TERM.inkey()
            utils.quit_on_q(ans)

            if ans in PLUS:
                spacing += 1
                valid = True

            elif ans in MINUS and spacing > 0:
                spacing -= 1
                valid = True

            elif ans.name == 'KEY_ENTER':
                os.system('clear')
                lscat.api.hide(placement)
                return spacing

            else:
                valid = False

            if valid:
                lscat.api.hide(placement)
                i += 1
Exemplo n.º 5
0
def gallery_print_spacing_assistant(size, xpadding, image_width,
                                    image_height: int) -> 'list[int]':
    """=== Gallery print spacing ===
    Use +/= to increase the spacing, and -/_ to decrease it
    Use q to exit the program, and press enter to go to the next assistant
    Use left and right arrow keys to change the current space selection

    Do you want to preview an existing cache dir? [y/N]
    To keep your chosen thumbnail size, image width and x spacing, enter 'n'.
    """
    printer.print_doc(
        gallery_print_spacing_assistant.__doc__)  # Action before start
    ans = input()

    # Setup variables
    ncols, images = _display_inital_row(ans, size, xpadding, image_width,
                                        image_height)

    # Just the default settings; len(first_list) == 5
    spacings = [9, 17, 17, 17, 17] + [17] * (ncols - 5)
    current_selection = 0

    # Start
    print('\n')
    with TERM.cbreak():
        while True:
            printer.update_gallery_info(spacings, ncols, current_selection)

            ans = TERM.inkey()
            utils.quit_on_q(ans)

            if ans in PLUS and pure.line_width(spacings, ncols) < TERM.width:
                spacings[current_selection] += 1

            elif ans in MINUS and spacings[current_selection] > 0:
                spacings[current_selection] -= 1

            # right arrow
            elif (ans.name == 'KEY_RIGHT' or ans in {'d', 'l'}
                  and current_selection < len(spacings) - 1):
                current_selection += 1

            # left arrow
            elif ans.name == 'KEY_LEFT' or ans in {'a', 'h'
                                                   } and current_selection > 0:
                current_selection -= 1

            elif ans.name == 'KEY_ENTER':
                lscat.api.hide_all(images)
                return spacings
Exemplo n.º 6
0
    def start(self) -> 'IO':
        show_images = True
        self.terminal_page = 0

        with TERM.cbreak():
            while True:
                if show_images:
                    self.show_func()
                    self.maybe_show_preview()
                    self.report()

                ans = TERM.inkey()
                print(ans)
                utils.quit_on_q(ans)

                if ans == 'n' and self.current_page == self.max_pages:
                    print('This is the last cached page!')
                    show_images = False

                elif ans == 'p' and self.current_page == self.condition:
                    print('This is the last page!')
                    show_images = False

                elif ans == 'n':
                    os.system('clear')
                    self.current_page += 1
                    show_images = True

                elif ans == 'p':
                    os.system('clear')
                    self.current_page -= 1
                    show_images = True

                elif (ans.name == 'KEY_DOWN' and self.scrollable
                      and self.terminal_page + 1 < self.max_scrolls):
                    self.terminal_page += 1
                    show_images = True

                elif (ans.name == 'KEY_UP' and self.scrollable
                      and self.terminal_page > 0):
                    self.terminal_page -= 1
                    show_images = True

                else:
                    print('Invalid input!')
                    show_images = False

                if show_images:
                    self.end_func()
Exemplo n.º 7
0
def print_doc(doc: str) -> 'IO':
    """Prints a given string in the bottom of the terminal"""
    os.system('clear')
    number_of_newlines = doc.count('\n')
    bottom = TERM.height - (number_of_newlines + 2)
    with TERM.location(0, bottom):
        print(doc)
Exemplo n.º 8
0
def user_prompt(user):
    """Handles key presses for user views (following users and user search)
    The only difference between image and user prompts is the `case`,
    action of two-digit-sequence, and single character return
    """
    keyseqs = []
    case = {
        'n': user.next_page,
        'p': user.previous_page,
        'h': printer.user_help,
        'q': ask_quit,
        'm': lambda: printer.print_bottom(user.__class__.__bases__[0].__doc__),
        'KEY_DOWN': user.scroll_down,
        'KEY_UP': user.scroll_up,
    }

    with TERM.cbreak():
        while True:
            two_digit_seq = len(keyseqs) == 2 and all_isdigit(keyseqs)

            # 1. Two digit sequence -- view artist given number
            if two_digit_seq:
                return user.go_artist_mode(pure.concat_seqs_to_int(keyseqs))

            # 2. Ask and wait for user input
            user_prompt_command = ask_wait_user_input(keyseqs, 'a user')

            # 3. Single char input with action that leaves prompt
            if user_prompt_command == 'r':
                return user.reload()

            # 4. Common
            keyseqs = common(case, user_prompt_command, keyseqs)
Exemplo n.º 9
0
 def maybe_show_preview(self) -> 'IO':
     if len(self.all_images) > 1:
         tracker = self._update_tracker()
         loc = TERM.get_location()
         for image in self.all_images[self.current_page + 1:][:4]:
             tracker.update(image)
         printer.move_cursor_xy(loc[0], loc[1])
         self.preview_images = tracker.images
Exemplo n.º 10
0
def gallery_like_prompt(gallery):
    """
    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
    """
    keyseqs = []
    sequenceable_keys = ('o', 'd', 'i', 'O', 'D', 'a', 'A')
    case = {
        'n': gallery.next_page,
        'p': gallery.previous_page,
        'h': gallery.help,
        'q': ask_quit,
        'm': lambda: printer.print_bottom('', gallery.__doc__),
        'KEY_DOWN': gallery.scroll_down,
        'KEY_UP': gallery.scroll_up,
    }

    with TERM.cbreak():
        while True:
            two_digit_seq = len(keyseqs) == 2 and all_isdigit(keyseqs)
            one_letter_two_digit_seq = (len(keyseqs) == 3
                                        and all_isdigit(keyseqs[1:])
                                        and keyseqs[0] in sequenceable_keys)

            # 1. Two digit sequence
            if two_digit_seq:
                image_num = utils.seq_coords_to_int(keyseqs)
                if image_num is not False:
                    return goto_image(gallery, image_num)
                printer.print_bottom('\nInvalid command! Press h to show help')
                keyseqs = []

            # 1. One letter two digit sequence
            elif one_letter_two_digit_seq:
                open_or_download(gallery, keyseqs)

                if keyseqs[0] == 'i':
                    return goto_image(gallery,
                                      pure.concat_seqs_to_int(keyseqs, 1))
                elif keyseqs[0].lower() == 'a':
                    return gallery.handle_prompt(keyseqs)
                keyseqs = []

            # 2. Wait for user input
            gallery_command = ask_wait_user_input(keyseqs, 'a gallery')

            # 3. Single char input with action that leaves prompt
            if gallery_command == 'b':
                return gallery.handle_prompt(['b'])

            elif gallery_command == 'r':
                return gallery.handle_prompt(['r'])

            # 4. Common
            keyseqs = common(case, gallery_command, keyseqs, sequenceable_keys)
Exemplo n.º 11
0
def thumbnail_size_assistant() -> 'IO[int]':
    """=== Thumbnail size ===
    This will display an image whose thumbnail size can be varied
    Use +/= to increase the size, and -/_ to decrease it
    Use q to exit the program, and press enter to confirm the size

    Keep in mind this size will be used for a grid of images
    """
    # Setup
    images = []
    size = 300
    previous_size = 300

    # Start
    printer.print_doc(thumbnail_size_assistant.__doc__)

    with TERM.cbreak():
        while True:
            images.append(lscat.api.show(WELCOME_IMAGE, 0, 0, size))
            with TERM.location(0, (TERM.height - 10)):
                print(f'size = {size}')

            ans = TERM.inkey()
            utils.quit_on_q(ans)

            if ans in PLUS:
                previous_size = size
                size += 20

            # +, +, +, -, +
            # |     |  ^  |> Do nothing here as well
            # |     |  |
            # |     |  |
            # |     |  This is where all images should be hidden
            # |     |
            # No images hidden in this range
            elif ans in MINUS:
                previous_size = size
                size -= 20
                if previous_size > size:
                    lscat.api.hide_all(images)

            elif ans.name == 'KEY_ENTER':
                lscat.api.hide_all(images)
                return size
Exemplo n.º 12
0
    def hide_show_print(self) -> 'IO':
        """Hide image if shown, show another image, and report"""
        if self.image:
            lscat.api.hide(self.image)

        self.image = self.show_func_args()

        self.maybe_move_up()
        printer.write('\r' + ' ' * 20 + '\r')
        with TERM.location(0, TERM.height - 13):
            self.report()
Exemplo n.º 13
0
def user_info_assistant(thumbnail_size, xpadding, image_width: int) -> int:
    """=== User print name xcoord ===
    Use +/= to move the text right, and -/_ to move it left
    Adjust the position as you see fit

    Use q to exit the program, and press enter to confirm the current position
    """
    # Setup variables
    spacing, _ = config.api.gen_users_settings()  # Default
    preview_xcoords = pure.xcoords(TERM.width, image_width, xpadding, 1)[-3:]

    # Start
    printer.print_doc(user_info_assistant.__doc__)

    images = lscat.api.show_user_row(WELCOME_IMAGE, preview_xcoords, xpadding,
                                     thumbnail_size)

    if not config.api.use_ueberzug():
        printer.move_cursor_up(5)
    else:
        printer.move_cursor_down(3)

    with TERM.cbreak():
        while True:
            printer.update_user_info(spacing)

            ans = TERM.inkey()
            utils.quit_on_q(ans)

            if ans in PLUS:
                spacing += 1

            elif ans in MINUS and spacing > 0:
                spacing -= 1

            elif ans.name == 'KEY_ENTER':
                print('\n' * 3)
                lscat.api.hide_all(images)
                return spacing
Exemplo n.º 14
0
def maybe_print_bottom(use_ueberzug: 'Optional[bool]' = None, offset=0):
    if use_ueberzug is None:
        from koneko import config

        use_ueberzug = config.api.use_ueberzug()

    if use_ueberzug:
        cursor = TERM.location(0, TERM.height - 5 + offset)
        cursor.__enter__()

    try:
        yield
    finally:
        if use_ueberzug:
            cursor.__exit__(None, None, None)
Exemplo n.º 15
0
def image_prompt(image):
    """if-else statements to intercept key presses and do the correct action"""
    keyseqs = []
    case = {
        'o': image.open_image,
        'd': image.download_image,
        'n': image.next_image,
        'p': image.previous_image,
        'f': image.show_full_res,
        'r': image.view_related_images,
        'h': printer.image_help,
        'q': ask_quit,
        'm': lambda: printer.print_bottom(image.__doc__)
    }

    with TERM.cbreak():
        while True:
            two_digit_seq = len(keyseqs) == 2 and all_isdigit(keyseqs)

            # 1. Two digit sequence -- jump to post number
            if two_digit_seq:
                image.jump_to_image(pure.concat_seqs_to_int(keyseqs))
                keyseqs = []

            # 2. Ask and wait for user input
            image_prompt_command = ask_wait_user_input(keyseqs, 'an image')

            # 3. Single char input with action that leaves prompt
            if image_prompt_command == 'b':
                return image.leave(False)

            elif image_prompt_command == 'a':
                return image.leave(True)

            # 4. Common
            keyseqs = common(case, image_prompt_command, keyseqs)
Exemplo n.º 16
0
 def start_preview(self):
     self.loc = TERM.get_location()
     if config.check_image_preview() and self.number_of_pages > 1:
         self.event = threading.Event()  # Reset event, in case if it's set
         threading.Thread(target=self.preview).start()
Exemplo n.º 17
0
def ask_wait_user_input(keyseqs: 'list[str]', view_name: str) -> str:
    if not keyseqs:
        printer.print_bottom(f'Enter {view_name} view command:', offset=-1)
    command = TERM.inkey()
    printer.print_bottom(command, offset=2, end='', flush=True)
    return command