Пример #1
0
def test_consider_percent(
    considerations: List[int],
    expect: Optional[int],
) -> None:
    layout = Layout()
    for consideration in considerations:
        layout.consider_percent(consideration)
    assert layout.percent_length == expect
Пример #2
0
def test_consider_right_fraction(
    considerations: List[int],
    expect: Optional[int],
) -> None:
    layout = Layout()
    for consideration in considerations:
        layout.consider_right_fraction(consideration)
    assert layout.right_fraction_length == expect
Пример #3
0
    def calculate_layout(self, style: Style) -> Layout:
        """ Calculates a layout to align all rows. """

        layout = Layout()

        for row in self.rows:
            name_len = row.render_name(color=False,
                                       suffix=style.name_suffix)[1]
            layout.consider_name(name_len)

            if style.show_fraction:
                left_len = row.render_left_fraction(color=False)[1]
                layout.consider_left_fraction(left_len)
                right_len = row.render_right_fraction(color=False)[1]
                layout.consider_right_fraction(right_len)

            if style.show_percent:
                pc_len = row.render_percent(color=False,
                                            prefix=style.percent_prefix)[1]
                layout.consider_percent(pc_len)

        return layout
Пример #4
0
    def render(
        self,
        layout: Optional[Layout] = None,
        style: Optional[Style] = None,
    ) -> str:
        """ Renders the row. """

        style = style or Style()
        layout = layout or Layout()

        name, name_len = self.render_name(
            color=style.color,
            suffix=style.name_suffix,
            length=layout.name_length,
        )

        if style.show_fraction:
            fraction, fraction_len = self.render_fraction(
                color=style.color,
                prefix=style.fraction_prefix,
                separator=style.fraction_separator,
                left_length=layout.left_fraction_length,
                right_length=layout.right_fraction_length,
            )
        else:
            fraction = ""
            fraction_len = 0

        if style.show_percent:
            percent, percent_len = self.render_percent(
                color=style.color,
                prefix=style.percent_prefix,
                length=layout.percent_length,
            )
        else:
            percent = ""
            percent_len = 0

        bar = self.render_bar(
            color=style.color,
            length=style.width - name_len - fraction_len - percent_len,
        )

        return (name + bar + fraction + percent).rstrip()
Пример #5
0
def test_init__left_fraction_length_empty() -> None:
    assert Layout().left_fraction_length is None
Пример #6
0
def test_init__right_fraction_length_set() -> None:
    assert Layout(right_fraction_length=3).right_fraction_length == 3
Пример #7
0
def test_init__percent_length_set() -> None:
    assert Layout(percent_length=3).percent_length == 3
Пример #8
0
def test_init__percent_length_empty() -> None:
    assert Layout().percent_length is None
Пример #9
0
def test_init__name_length_set() -> None:
    assert Layout(name_length=3).name_length == 3
Пример #10
0
def test_init__name_length_empty() -> None:
    assert Layout().name_length is None
Пример #11
0
def test_init__left_fraction_length_set() -> None:
    assert Layout(left_fraction_length=3).left_fraction_length == 3
Пример #12
0
fraction_part_test_cases = [
    (0, False, None, ("0", 1)),
    (1, False, None, ("1", 1)),
    (1.2, False, None, ("1.2", 3)),
    (-3.4, False, None, ("-3.4", 4)),
]


@mark.parametrize(
    "row, style, layout, expect",
    [
        (
            Row(name="foo", maximum=9, current=1),
            Style(color=False, width=40),
            Layout(),
            "foo ████",
        ),
        (
            Row(name="foo", maximum=9, current=1),
            Style(color=False, show_fraction=True, width=40),
            Layout(),
            "foo ███▍                           1 / 9",
        ),
        (
            Row(name="foo", maximum=9, current=1),
            Style(color=False, show_percent=True, width=40),
            Layout(),
            "foo ███▌                             11%",
        ),
        (
Пример #13
0
def test_append() -> None:
    rows = Rows([Row(name="foo", current=1, maximum=9)])
    rows.append(name="bar", current=2, maximum=9)
    style = Style(color=False, width=40)
    assert rows.render(style) == "foo ████\nbar ███████▉"


@mark.parametrize(
    "rows, style, expect",
    [
        (
            Rows([
                Row(name="red", current=1, maximum=9),
            ]),
            Style(width=40, name_suffix=""),
            Layout(name_length=3),
        ),
        (
            Rows([
                Row(name="red", current=1, maximum=9),
            ]),
            Style(width=40, name_suffix="x"),
            Layout(name_length=4),
        ),
        (
            Rows([
                Row(name="red", current=1, maximum=9),
            ]),
            Style(width=40, name_suffix="", show_fraction=True),
            Layout(name_length=3,
                   left_fraction_length=1,