Ejemplo n.º 1
0
def checkboxlist_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[List[_T]]:
    """
    Display a simple list of element the user can choose multiple values amongst.

    Several elements 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=cb_list.current_values)

    cb_list = CheckboxList(values)

    dialog = Dialog(title=title,
                    body=HSplit([
                        Label(text=text, dont_extend_height=True),
                        cb_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)
Ejemplo n.º 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)
Ejemplo n.º 3
0
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)
Ejemplo n.º 4
0
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)
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
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)
Ejemplo n.º 7
0
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)
Ejemplo n.º 8
0

def button2_clicked():
    text_area.text = 'Button 2 clicked'


def button3_clicked():
    text_area.text = 'Button 3 clicked'


def exit_clicked():
    get_app().exit()


# All the widgets for the UI.
button1 = Button('Button 1', handler=button1_clicked)
button2 = Button('Button 2', handler=button2_clicked)
button3 = Button('Button 3', handler=button3_clicked)
button4 = Button('Exit', handler=exit_clicked)
text_area = TextArea(focusable=True)

# Combine all the widgets in a UI.
# The `Box` object ensures that padding will be inserted around the containing
# widget. It adapts automatically, unless an explicit `padding` amount is given.
root_container = Box(
    HSplit([
        Label(text='Press `Tab` to move the focus.'),
        VSplit([
            Box(body=HSplit([button1, button2, button3, button4], padding=1),
                padding=1,
                style='class:left-pane'),
)


def accept_yes():
    get_app().exit(result=True)


def accept_no():
    get_app().exit(result=False)


def do_exit():
    get_app().exit(result=False)


yes_button = Button(text='Yes', handler=accept_yes)
no_button = Button(text='No', handler=accept_no)
textfield  = TextArea(lexer=PygmentsLexer(HtmlLexer))
checkbox1 = Checkbox(text='Checkbox')
checkbox2 = Checkbox(text='Checkbox')

radios = RadioList(values=[
    ('Red', 'red'),
    ('Green', 'green'),
    ('Blue', 'blue'),
    ('Orange', 'orange'),
    ('Yellow', 'yellow'),
    ('Purple', 'Purple'),
    ('Brown', 'Brown'),
])