def radiolist_dialog(title: AnyFormattedText = '',
                     text: AnyFormattedText = '',
                     ok_text: str = 'Ok',
                     cancel_text: str = 'Cancel',
                     values: Optional[List[Tuple[_T,
                                                 AnyFormattedText]]] = None,
                     style: Optional[BaseStyle] = None) -> Application[_T]:
    """
    Display a simple list of element the user can choose amongst.

    Only one element can be selected at a time using Arrow keys and Enter.
    The focus can be moved between the list and the Ok/Cancel button with tab.
    """
    if values is None:
        values = []

    def ok_handler() -> None:
        get_app().exit(result=radio_list.current_value)

    radio_list = RadioList(values)

    dialog = Dialog(title=title,
                    body=HSplit([
                        Label(text=text, dont_extend_height=True),
                        radio_list,
                    ],
                                padding=1),
                    buttons=[
                        Button(text=ok_text, handler=ok_handler),
                        Button(text=cancel_text, handler=_return_none),
                    ],
                    with_background=True)

    return _create_app(dialog, style)
Beispiel #2
0
    def __init__(self, title='', label_text='', completer=None):
        self.future = Future()

        def accept_text(buf):
            get_app().layout.focus(ok_button)
            buf.complete_state = None
            return True

        def accept():
            self.future.set_result(self.text_area.text)

        def cancel():
            self.future.set_result(None)

        self.text_area = TextArea(completer=completer,
                                  multiline=False,
                                  width=D(preferred=40),
                                  accept_handler=accept_text)

        ok_button = Button(text='OK', handler=accept)
        cancel_button = Button(text='Cancel', handler=cancel)

        self.dialog = Dialog(title=title,
                             body=HSplit(
                                 [Label(text=label_text), self.text_area]),
                             buttons=[ok_button, cancel_button],
                             width=D(preferred=80),
                             modal=True)
def progress_dialog(title: AnyFormattedText = '',
                    text: AnyFormattedText = '',
                    run_callback: Callable[
                        [Callable[[int], None], Callable[[str], None]],
                        None] = (lambda *a: None),
                    style: Optional[BaseStyle] = None) -> Application[None]:
    """
    :param run_callback: A function that receives as input a `set_percentage`
        function and it does the work.
    """
    loop = get_event_loop()
    progressbar = ProgressBar()
    text_area = TextArea(
        focusable=False,

        # Prefer this text area as big as possible, to avoid having a window
        # that keeps resizing when we add text to it.
        height=D(preferred=10**10))

    dialog = Dialog(body=HSplit([
        Box(Label(text=text)),
        Box(text_area, padding=D.exact(1)),
        progressbar,
    ]),
                    title=title,
                    with_background=True)
    app = _create_app(dialog, style)

    def set_percentage(value: int) -> None:
        progressbar.percentage = int(value)
        app.invalidate()

    def log_text(text: str) -> None:
        loop.call_soon_threadsafe(text_area.buffer.insert_text, text)
        app.invalidate()

    # Run the callback in the executor. When done, set a return value for the
    # UI, so that it quits.
    def start() -> None:
        try:
            run_callback(set_percentage, log_text)
        finally:
            app.exit()

    def pre_run() -> None:
        run_in_executor_with_context(start)

    app.pre_run_callables.append(pre_run)

    return app
def message_dialog(title: AnyFormattedText = '',
                   text: AnyFormattedText = '',
                   ok_text: str = 'Ok',
                   style: Optional[BaseStyle] = None) -> Application[None]:
    """
    Display a simple message box and wait until the user presses enter.
    """
    dialog = Dialog(title=title,
                    body=Label(text=text, dont_extend_height=True),
                    buttons=[
                        Button(text=ok_text, handler=_return_none),
                    ],
                    with_background=True)

    return _create_app(dialog, style)
Beispiel #5
0
    def __init__(self, title, text):
        self.future = Future()

        def set_done():
            self.future.set_result(None)

        ok_button = Button(text='OK', handler=(lambda: set_done()))

        self.dialog = Dialog(title=title,
                             body=HSplit([
                                 Label(text=text),
                             ]),
                             buttons=[ok_button],
                             width=D(preferred=80),
                             modal=True)
def button_dialog(title: AnyFormattedText = '',
                  text: AnyFormattedText = '',
                  buttons: List[Tuple[str, _T]] = [],
                  style: Optional[BaseStyle] = None) -> Application[_T]:
    """
    Display a dialog with button choices (given as a list of tuples).
    Return the value associated with button.
    """
    def button_handler(v: _T) -> None:
        get_app().exit(result=v)

    dialog = Dialog(title=title,
                    body=Label(text=text, dont_extend_height=True),
                    buttons=[
                        Button(text=t,
                               handler=functools.partial(button_handler, v))
                        for t, v in buttons
                    ],
                    with_background=True)

    return _create_app(dialog, style)
def input_dialog(title: AnyFormattedText = '',
                 text: AnyFormattedText = '',
                 ok_text: str = 'OK',
                 cancel_text: str = 'Cancel',
                 completer: Optional[Completer] = None,
                 password: FilterOrBool = False,
                 style: Optional[BaseStyle] = None) -> Application[str]:
    """
    Display a text input box.
    Return the given text, or None when cancelled.
    """
    def accept(buf: Buffer) -> bool:
        get_app().layout.focus(ok_button)
        return True  # Keep text.

    def ok_handler() -> None:
        get_app().exit(result=textfield.text)

    ok_button = Button(text=ok_text, handler=ok_handler)
    cancel_button = Button(text=cancel_text, handler=_return_none)

    textfield = TextArea(multiline=False,
                         password=password,
                         completer=completer,
                         accept_handler=accept)

    dialog = Dialog(title=title,
                    body=HSplit([
                        Label(text=text, dont_extend_height=True),
                        textfield,
                    ],
                                padding=D(preferred=1, max=1)),
                    buttons=[ok_button, cancel_button],
                    with_background=True)

    return _create_app(dialog, style)
def yes_no_dialog(title: AnyFormattedText = '',
                  text: AnyFormattedText = '',
                  yes_text: str = 'Yes',
                  no_text: str = 'No',
                  style: Optional[BaseStyle] = None) -> Application[bool]:
    """
    Display a Yes/No dialog.
    Return a boolean.
    """
    def yes_handler() -> None:
        get_app().exit(result=True)

    def no_handler() -> None:
        get_app().exit(result=False)

    dialog = Dialog(title=title,
                    body=Label(text=text, dont_extend_height=True),
                    buttons=[
                        Button(text=yes_text, handler=yes_handler),
                        Button(text=no_text, handler=no_handler),
                    ],
                    with_background=True)

    return _create_app(dialog, style)
    ('Yellow', 'yellow'),
    ('Purple', 'Purple'),
    ('Brown', 'Brown'),
])

animal_completer = WordCompleter([
    'alligator', 'ant', 'ape', 'bat', 'bear', 'beaver', 'bee', 'bison',
    'butterfly', 'cat', 'chicken', 'crocodile', 'dinosaur', 'dog', 'dolphin',
    'dove', 'duck', 'eagle', 'elephant', 'fish', 'goat', 'gorilla', 'kangaroo',
    'leopard', 'lion', 'mouse', 'rabbit', 'rat', 'snake', 'spider', 'turkey',
    'turtle', ], ignore_case=True)

root_container = HSplit([
    VSplit([
        Frame(body=Label(text='Left frame\ncontent')),
        Dialog(title='The custom window',
               body=Label('hello\ntest')),
        textfield,
    ], height=D()),
    VSplit([
        Frame(body=ProgressBar(),
              title='Progress bar'),
        Frame(title='Checkbox list',
              body=HSplit([
                  checkbox1,
                  checkbox2,
              ])),
        Frame(title='Radio list', body=radios),
    ], padding=1),
    Box(
        body=VSplit([
            yes_button,