Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[notabug][helpwanted] Not sure how popup is meant to work ? #183

Open
stuaxo opened this issue Apr 14, 2023 · 1 comment
Open

[notabug][helpwanted] Not sure how popup is meant to work ? #183

stuaxo opened this issue Apr 14, 2023 · 1 comment
Labels
Bug Something isn't working

Comments

@stuaxo
Copy link

stuaxo commented Apr 14, 2023

I'm making a menu that, ideally would display on the existing terminal output, without the "root" window, it also has key shortcuts for each item.

I tried using add_popup, but on my first try:

  • The cursor keys didn't work for navigation (is that expected) ?
  • I couldn't work out a sane way to get a reference to it, so I could attach hotkeys to each item.

In the end, I've used a normal menu, it's not ideal, as the background is there and it's possible to focus the root element.

(My use-case for the hotkeys is a scenario where the screen is sometimes blank).

Here's the for a menu of options code so far - are there some really obvious things I'm missing ?

import re
import sys

import py_cui
from py_cui.keys import get_ascii_from_char


class SimplishMenu:
    # Match any keys in square brackets:
    HOTKEY_REGEX = r'\[(.*?)\]'

    def __init__(self, title, items, row=0, column=0, root=None):
        if root is None:
            root = py_cui.PyCUI(30, 30)

        items = list(items)
        row_span = len(items)
        column_span = max(len(item) for item in items)

        self.menu = root.add_scroll_menu(title, row, column, row_span + 1, column_span)
        self.menu.add_key_command(py_cui.keys.KEY_ENTER, lambda: self.choose_item(self.menu.get()))

        # Hotkeys
        self.menu.add_text_color_rule(self.HOTKEY_REGEX, py_cui.RED_ON_BLACK, 'contains', match_type='regex')
        for item in items:
            self.add_item(item)
        self.root = root
        self.root.move_focus(self.menu)

    def choose_item(self, item):
        if self.menu.get() != item:
            # If item was chosen via hotkey, select it
            self.menu.set_selected_item(item)

        self.root.stop()

    def add_item(self, item):
        hotkeys = ''.join(re.findall(self.HOTKEY_REGEX, item))
        if hotkeys:
            for hotkey in hotkeys.lower():
                self.menu.add_key_command(get_ascii_from_char(hotkey), lambda: self.choose_item(item))
        self.menu.add_item(item)

    def get(self):
        return self.menu.get()

    def show(self):
        self.root.move_focus(self.menu)
        self.root.start()


def choose_option(title, items):
    root = py_cui.PyCUI(10, 10, auto_focus_buttons=True, exit_key=None)
    menu = SimplishMenu(title, items, root=root)
    root.start()
    return menu.get()


if __name__ == '__main__':
    option = choose_option('Main Menu', ['Option [1]', 'Option [2]'])
    print("Chose: ", option)
@stuaxo stuaxo added the Bug Something isn't working label Apr 14, 2023
@jwlodek
Copy link
Owner

jwlodek commented Apr 17, 2023

Could you post the code where you tried to use the add_popup mechanism?

You can get a reference to the active popup using the root element:

root._popup

When there isn't an active one, this will be set to None. The current logic doesn't really support adding hotkeys to popups, although that shouldn't be too challenging to add if it is a worthwhile feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants