Exemple #1
0
    def __init__(self) -> None:
        self._percentage = 60

        self.label = Label('60%')
        self.container = FloatContainer(
            content=Window(height=1),
            floats=[
                # We first draw the label, then the actual progress bar.  Right
                # now, this is the only way to have the colors of the progress
                # bar appear on top of the label. The problem is that our label
                # can't be part of any `Window` below.
                Float(content=self.label, top=0, bottom=0),
                Float(left=0,
                      top=0,
                      right=0,
                      bottom=0,
                      content=VSplit([
                          Window(
                              style='class:progress-bar.used',
                              width=lambda: D(weight=int(self._percentage))),
                          Window(style='class:progress-bar',
                                 width=lambda: D(weight=int(100 - self.
                                                            _percentage))),
                      ])),
            ])
Exemple #2
0
    def __init__(self,
                 body: AnyContainer,
                 title: AnyFormattedText = '',
                 buttons: Optional[Sequence[Button]] = None,
                 modal: bool = True,
                 width: AnyDimension = None,
                 with_background: bool = False) -> None:

        self.body = body
        self.title = title

        buttons = buttons or []

        # When a button is selected, handle left/right key bindings.
        buttons_kb = KeyBindings()
        if len(buttons) > 1:
            first_selected = has_focus(buttons[0])
            last_selected = has_focus(buttons[-1])

            buttons_kb.add('left', filter=~first_selected)(focus_previous)
            buttons_kb.add('right', filter=~last_selected)(focus_next)

        frame_body: AnyContainer
        if buttons:
            frame_body = HSplit([
                # Add optional padding around the body.
                Box(body=DynamicContainer(lambda: self.body),
                    padding=D(preferred=1, max=1),
                    padding_bottom=0),
                # The buttons.
                Box(body=VSplit(buttons, padding=1, key_bindings=buttons_kb),
                    height=D(min=1, max=3, preferred=3))
            ])
        else:
            frame_body = body

        # Key bindings for whole dialog.
        kb = KeyBindings()
        kb.add('tab', filter=~has_completions)(focus_next)
        kb.add('s-tab', filter=~has_completions)(focus_previous)

        frame = Shadow(body=Frame(
            title=lambda: self.title,
            body=frame_body,
            style='class:dialog.body',
            width=(None if with_background is None else width),
            key_bindings=kb,
            modal=modal,
        ))

        self.container: Union[Box, Shadow]
        if with_background:
            self.container = Box(body=frame, style='class:dialog', width=width)
        else:
            self.container = frame
Exemple #3
0
    def __init__(self,
                 body: AnyContainer,
                 padding: AnyDimension = None,
                 padding_left: AnyDimension = None,
                 padding_right: AnyDimension = None,
                 padding_top: AnyDimension = None,
                 padding_bottom: AnyDimension = None,
                 width: AnyDimension = None,
                 height: AnyDimension = None,
                 style: str = '',
                 char: Union[None, str, Callable[[], str]] = None,
                 modal: bool = False,
                 key_bindings: Optional[KeyBindings] = None) -> None:

        if padding is None:
            padding = D(preferred=0)

        def get(value: AnyDimension) -> D:
            if value is None:
                value = padding
            return to_dimension(value)

        self.padding_left = get(padding_left)
        self.padding_right = get(padding_right)
        self.padding_top = get(padding_top)
        self.padding_bottom = get(padding_bottom)
        self.body = body

        self.container = HSplit([
            Window(height=self.padding_top, char=char),
            VSplit([
                Window(width=self.padding_left, char=char),
                body,
                Window(width=self.padding_right, char=char),
            ]),
            Window(height=self.padding_bottom, char=char),
        ],
                                width=width,
                                height=height,
                                style=style,
                                modal=modal,
                                key_bindings=None)
Exemple #4
0
def test_layout_class():
    c1 = BufferControl()
    c2 = BufferControl()
    c3 = BufferControl()
    win1 = Window(content=c1)
    win2 = Window(content=c2)
    win3 = Window(content=c3)

    layout = Layout(container=VSplit([HSplit([win1, win2]), win3]))

    # Listing of windows/controls.
    assert list(layout.find_all_windows()) == [win1, win2, win3]
    assert list(layout.find_all_controls()) == [c1, c2, c3]

    # Focusing something.
    layout.focus(c1)
    assert layout.has_focus(c1)
    assert layout.has_focus(win1)
    assert layout.current_control == c1
    assert layout.previous_control == c1

    layout.focus(c2)
    assert layout.has_focus(c2)
    assert layout.has_focus(win2)
    assert layout.current_control == c2
    assert layout.previous_control == c1

    layout.focus(win3)
    assert layout.has_focus(c3)
    assert layout.has_focus(win3)
    assert layout.current_control == c3
    assert layout.previous_control == c2

    # Pop focus. This should focus the previous control again.
    layout.focus_last()
    assert layout.has_focus(c2)
    assert layout.has_focus(win2)
    assert layout.current_control == c2
    assert layout.previous_control == c1
Exemple #5
0
    def __init__(self,
                 body: AnyContainer,
                 title: AnyFormattedText = '',
                 style: str = '',
                 width: AnyDimension = None,
                 height: AnyDimension = None,
                 key_bindings: Optional[KeyBindings] = None,
                 modal: bool = False) -> None:

        self.title = title
        self.body = body

        fill = partial(Window, style='class:frame.border')
        style = 'class:frame ' + style

        top_row_with_title = VSplit(
            [
                fill(width=1, height=1, char=Border.TOP_LEFT),
                fill(char=Border.HORIZONTAL),
                fill(width=1, height=1, char='|'),
                # Notice: we use `Template` here, because `self.title` can be an
                # `HTML` object for instance.
                Label(lambda: Template(' {} ').format(self.title),
                      style='class:frame.label',
                      dont_extend_width=True),
                fill(width=1, height=1, char='|'),
                fill(char=Border.HORIZONTAL),
                fill(width=1, height=1, char=Border.TOP_RIGHT),
            ],
            height=1)

        top_row_without_title = VSplit([
            fill(width=1, height=1, char=Border.TOP_LEFT),
            fill(char=Border.HORIZONTAL),
            fill(width=1, height=1, char=Border.TOP_RIGHT),
        ],
                                       height=1)

        @Condition
        def has_title() -> bool:
            return bool(self.title)

        self.container = HSplit(
            [
                ConditionalContainer(content=top_row_with_title,
                                     filter=has_title),
                ConditionalContainer(content=top_row_without_title,
                                     filter=~has_title),
                VSplit(
                    [
                        fill(width=1, char=Border.VERTICAL),
                        DynamicContainer(lambda: self.body),
                        fill(width=1, char=Border.VERTICAL),
                        # Padding is required to make sure that if the content is
                        # too small, the right frame border is still aligned.
                    ],
                    padding=0),
                VSplit([
                    fill(width=1, height=1, char=Border.BOTTOM_LEFT),
                    fill(char=Border.HORIZONTAL),
                    fill(width=1, height=1, char=Border.BOTTOM_RIGHT),
                ]),
            ],
            width=width,
            height=height,
            style=style,
            key_bindings=key_bindings,
            modal=modal)
Exemple #6
0
tempus vehicula augue non venenatis. Mauris aliquam velit turpis, nec congue
risus aliquam sit amet. Pellentesque blandit scelerisque felis, faucibus
consequat ante. Curabitur tempor tortor a imperdiet tincidunt. Nam sed justo
sit amet odio bibendum congue. Quisque varius ligula nec ligula gravida, sed
convallis augue faucibus. Nunc ornare pharetra bibendum. Praesent blandit ex
quis sodales maximus. """

left_top = Window(BufferControl(Buffer(document=Document(LIPSUM))))
left_bottom = Window(BufferControl(Buffer(document=Document(LIPSUM))))
right_top = Window(BufferControl(Buffer(document=Document(LIPSUM))))
right_bottom = Window(BufferControl(Buffer(document=Document(LIPSUM))))

body = HSplit([
    Window(FormattedTextControl(top_text), height=2, style='reverse'),
    Window(height=1, char='-'),  # Horizontal line in the middle.
    VSplit([left_top, Window(width=1, char='|'), right_top]),
    Window(height=1, char='-'),  # Horizontal line in the middle.
    VSplit([left_bottom, Window(width=1, char='|'), right_bottom]),
])

# 2. Key bindings
kb = KeyBindings()


@kb.add('q')
def _(event):
    " Quit application. "
    event.app.exit()


@kb.add('a')
Exemple #7
0
adipiscing elit.
Maecenas quis
interdum enim."""


# 1. The layout
body = HSplit([
    Frame(
        Window(FormattedTextControl(TITLE), height=2), style='bg:#88ff88 #000000'),
    HSplit([
        # Left alignment.
        VSplit([
            Window(FormattedTextControl(HTML('<u>LEFT</u>')), width=10,
                   ignore_content_width=True, style='bg:#ff3333 ansiblack', align=WindowAlign.CENTER),
            VSplit([
                Window(FormattedTextControl(LIPSUM), height=4, style='bg:#444488'),
                Window(FormattedTextControl(LIPSUM), height=4, style='bg:#444488'),
                Window(FormattedTextControl(LIPSUM), height=4, style='bg:#444488'),
            ], padding=1, padding_style='bg:#888888', align=HorizontalAlign.LEFT, height=5, padding_char='|'),
        ]),
        # Center alignment.
        VSplit([
            Window(FormattedTextControl(HTML('<u>CENTER</u>')), width=10,
                   ignore_content_width=True, style='bg:#ff3333 ansiblack', align=WindowAlign.CENTER),
            VSplit([
                Window(FormattedTextControl(LIPSUM), height=4, style='bg:#444488'),
                Window(FormattedTextControl(LIPSUM), height=4, style='bg:#444488'),
                Window(FormattedTextControl(LIPSUM), height=4, style='bg:#444488'),
            ], padding=1, padding_style='bg:#888888', align=HorizontalAlign.CENTER, height=5, padding_char='|'),
        ]),
        # Right alignment.
"""
Vertical split example.
"""
from prompt_toolkit_dev.application import Application
from prompt_toolkit_dev.key_binding import KeyBindings
from prompt_toolkit_dev.layout.containers import VSplit, Window
from prompt_toolkit_dev.layout.controls import FormattedTextControl
from prompt_toolkit_dev.layout.layout import Layout

# 1. The layout
left_text = "\nVertical-split example. Press 'q' to quit.\n\n(left pane.)"
right_text = "\n(right pane.)"

body = VSplit([
    Window(FormattedTextControl(left_text)),
    Window(width=1, char='|'),  # Vertical line in the middle.
    Window(FormattedTextControl(right_text)),
])

# 2. Key bindings
kb = KeyBindings()


@kb.add('q')
def _(event):
    " Quit application. "
    event.app.exit()


# 3. The `Application`
application = Application(layout=Layout(body),
Exemple #9
0
left_buffer = Buffer()
right_buffer = Buffer()

# 1. First we create the layout
#    --------------------------

left_window = Window(BufferControl(buffer=left_buffer))
right_window = Window(BufferControl(buffer=right_buffer))


body = VSplit([
    left_window,

    # A vertical line in the middle. We explicitly specify the width, to make
    # sure that the layout engine will not try to divide the whole width by
    # three for all these windows.
    Window(width=1, char='|', style='class:line'),

    # Display the Result buffer on the right.
    right_window,
])

# As a demonstration. Let's add a title bar to the top, displaying "Hello world".

# somewhere, because usually the default key bindings include searching. (Press
# Ctrl-R.) It would be really annoying if the search key bindings are handled,
# but the user doesn't see any feedback. We will add the search toolbar to the
# bottom by using an HSplit.


def get_titlebar_text():
Exemple #10
0
body = HSplit([
    Frame(Window(FormattedTextControl(TITLE), height=2),
          style='bg:#88ff88 #000000'),
    VSplit([
        Window(FormattedTextControl(HTML('  <u>VerticalAlign.TOP</u>')),
               height=4,
               ignore_content_width=True,
               style='bg:#ff3333 #000000 bold',
               align=WindowAlign.CENTER),
        Window(FormattedTextControl(HTML('  <u>VerticalAlign.CENTER</u>')),
               height=4,
               ignore_content_width=True,
               style='bg:#ff3333 #000000 bold',
               align=WindowAlign.CENTER),
        Window(FormattedTextControl(HTML('  <u>VerticalAlign.BOTTOM</u>')),
               height=4,
               ignore_content_width=True,
               style='bg:#ff3333 #000000 bold',
               align=WindowAlign.CENTER),
        Window(FormattedTextControl(HTML('  <u>VerticalAlign.JUSTIFY</u>')),
               height=4,
               ignore_content_width=True,
               style='bg:#ff3333 #000000 bold',
               align=WindowAlign.CENTER),
    ],
           height=1,
           padding=1,
           padding_style='bg:#ff3333'),
    VSplit(
        [
            # Top alignment.
    ('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,
Exemple #12
0
                             width=D(preferred=80),
                             modal=True)

    def __pt_container__(self):
        return self.dialog


body = HSplit([
    text_field,
    search_toolbar,
    ConditionalContainer(
        content=VSplit([
            Window(FormattedTextControl(get_statusbar_text),
                   style='class:status'),
            Window(FormattedTextControl(get_statusbar_right_text),
                   style='class:status.right',
                   width=9,
                   align=WindowAlign.RIGHT),
        ],
                       height=1),
        filter=Condition(lambda: ApplicationState.show_status_bar)),
])

# Global key bindings.
bindings = KeyBindings()


@bindings.add('c-c')
def _(event):
    " Focus menu. "
    event.app.layout.focus(root_container.window)