Пример #1
0
def test_interpolation():
    value = Template(" {} ").format(HTML("<b>hello</b>"))

    assert to_formatted_text(value) == [
        ("", " "),
        ("class:b", "hello"),
        ("", " "),
    ]

    value = Template("a{}b{}c").format(HTML("<b>hello</b>"), "world")

    assert to_formatted_text(value) == [
        ("", "a"),
        ("class:b", "hello"),
        ("", "b"),
        ("", "world"),
        ("", "c"),
    ]
Пример #2
0
def test_interpolation():
    value = Template(' {} ').format(HTML('<b>hello</b>'))

    assert to_formatted_text(value) == [
        ('', ' '),
        ('class:b', 'hello'),
        ('', ' '),
    ]

    value = Template('a{}b{}c').format(HTML('<b>hello</b>'), 'world')

    assert to_formatted_text(value) == [
        ('', 'a'),
        ('class:b', 'hello'),
        ('', 'b'),
        ('', 'world'),
        ('', 'c'),
    ]
Пример #3
0
    def __init__(self, text: AnyFormattedText = '') -> None:
        self.checked = True

        kb = KeyBindings()

        @kb.add(' ')
        @kb.add('enter')
        def _(event: E) -> None:
            self.checked = not self.checked

        self.control = FormattedTextControl(self._get_text_fragments,
                                            key_bindings=kb,
                                            focusable=True)

        self.window = Window(width=3, content=self.control, height=1)

        self.container = VSplit(
            [self.window,
             Label(text=Template(' {}').format(text))],
            style='class:checkbox')
Пример #4
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="├"),
                fill(char=Border.HORIZONTAL),
                # 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(char=Border.HORIZONTAL),
                fill(width=1, height=1, char="┤"),
            ],
            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,
                ),
            ],
            width=width,
            height=height,
            style=style,
            key_bindings=key_bindings,
            modal=modal,
        )
Пример #5
0
    def __init__(self,
                 body,
                 title='',
                 style='',
                 width=None,
                 height=None,
                 key_bindings=None,
                 modal=False):
        assert is_container(body)
        assert is_formatted_text(title)
        assert isinstance(style, six.text_type)
        assert is_dimension(width)
        assert is_dimension(height)
        assert key_bindings is None or isinstance(key_bindings, KeyBindings)
        assert isinstance(modal, bool)

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

        if title:
            top_row = VSplit([
                fill(width=1, height=1, char=Border.TOP_LEFT),
                fill(char=Border.HORIZONTAL),
                fill(width=1, height=1, char='|'),
                Label(Template(' {} ').format(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)
        else:
            top_row = 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)

        self.container = HSplit(
            [
                top_row,
                VSplit(
                    [
                        fill(width=1, char=Border.VERTICAL),
                        body,
                        fill(width=1, char=Border.VERTICAL),
                        # Padding is required to make sure that if the content is
                        # too small, that 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)
Пример #6
0
    def __init__(self,
                 body,
                 title='',
                 style='',
                 width=None,
                 height=None,
                 key_bindings=None,
                 modal=False):
        assert is_container(body)
        assert is_formatted_text(title)
        assert isinstance(style, six.text_type)
        assert is_dimension(width)
        assert is_dimension(height)
        assert key_bindings is None or isinstance(key_bindings, KeyBindings)
        assert isinstance(modal, bool)

        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():
            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, that 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)