Skip to content

Commit 401ab4e

Browse files
committed
bugfix & linting
1 parent dd0a434 commit 401ab4e

3 files changed

Lines changed: 23 additions & 14 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* Removed unnecessary character escaping in the precompiled regex patterns in the `console` module.
2323
* Removed all the runtime type-checks that can also be checked using static type-checking tools, since you're supposed to use type checkers in modern python anyway, and to improve performance.
2424
* Renamed the internal class method `FormatCodes.__config_console()` to `FormatCodes._config_console()` to make it callable, but still indicate that it's internal.
25+
* Fixed a small bug where `Console.log_box_…()` would sometimes raise an exception due to an issue in the internal method `__prepare_log_box()`.
2526

2627
**BREAKING CHANGES:**
2728
* The arguments when calling `Console.get_args()` are no longer specified in a single dictionary, but now each argument is passed as a separate keyword argument.<br>

src/xulbux/console.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -709,11 +709,11 @@ def log_box_filled(
709709
pady = " " * (Console.w if w_full else max_line_len + (2 * w_padding))
710710
pad_w_full = (Console.w - (max_line_len + (2 * w_padding))) if w_full else 0
711711

712-
lines = [
712+
lines = [( \
713713
f"{spaces_l}[bg:{box_bg_color}]{' ' * w_padding}"
714-
+ _FC_PATTERNS.formatting.sub(lambda m: f"{m.group(0)}[bg:{box_bg_color}]", line) +
715-
(" " * ((w_padding + max_line_len - len(unfmt)) + pad_w_full)) + "[*]" for line, unfmt in zip(lines, unfmt_lines)
716-
]
714+
+ _FC_PATTERNS.formatting.sub(lambda m: f"{m.group(0)}[bg:{box_bg_color}]", line)
715+
+ (" " * ((w_padding + max_line_len - len(unfmt)) + pad_w_full)) + "[*]"
716+
) for line, unfmt in zip(lines, unfmt_lines)]
717717

718718
FormatCodes.print(
719719
f"{start}{spaces_l}[bg:{box_bg_color}]{pady}[*]\n" + "\n".join(lines)
@@ -804,13 +804,19 @@ def log_box_bordered(
804804

805805
h_rule = f"{spaces_l}[{border_style}]{border_chars[8]}{border_chars[9] * (Console.w - (len(border_chars[9] * 2)) if w_full else max_line_len + (2 * w_padding))}{border_chars[10]}[_]"
806806

807-
lines = [
808-
h_rule if _PATTERNS.hr.match(line) else f"{spaces_l}{border_l}{' ' * w_padding}{line}[_]" + " " *
809-
((w_padding + max_line_len - len(unfmt)) + pad_w_full) + border_r for line, unfmt in zip(lines, unfmt_lines)
810-
]
807+
lines = [( \
808+
h_rule if _PATTERNS.hr.match(line) else f"{spaces_l}{border_l}{' ' * w_padding}{line}[_]"
809+
+ " " * ((w_padding + max_line_len - len(unfmt)) + pad_w_full)
810+
+ border_r
811+
) for line, unfmt in zip(lines, unfmt_lines)]
811812

812813
FormatCodes.print(
813-
f"{start}{border_t}[_]\n" + "\n".join(lines) + f"\n{border_b}[_]",
814+
( \
815+
f"{start}{border_t}[_]\n"
816+
+ "\n".join(lines)
817+
+ ("\n" if len(lines) > 0 else "")
818+
+ f"{border_b}[_]"
819+
),
814820
default_color=default_color,
815821
sep="\n",
816822
end=end,
@@ -857,7 +863,7 @@ def __prepare_log_box(
857863
lines = [line for val in values for line in str(val).splitlines()]
858864

859865
unfmt_lines = [FormatCodes.remove(line, default_color) for line in lines]
860-
max_line_len = max(len(line) for line in unfmt_lines)
866+
max_line_len = max(len(line) for line in unfmt_lines) if unfmt_lines else 0
861867
return lines, cast(list[tuple[str, tuple[tuple[int, str], ...]]], unfmt_lines), max_line_len
862868

863869
@staticmethod
@@ -1261,8 +1267,10 @@ def show_progress(self, current: int, total: int, label: Optional[str] = None) -
12611267
- `label` -⠀an optional label which is inserted at the `{label}` or `{l}` placeholder"""
12621268
# THROTTLE UPDATES (UNLESS IT'S THE FIRST/FINAL UPDATE)
12631269
current_time = _time.time()
1264-
if not (self._last_update_time == 0.0 or current >= total or current < 0) \
1265-
and (current_time - self._last_update_time) < self._min_update_interval:
1270+
if (
1271+
not (self._last_update_time == 0.0 or current >= total or current < 0) \
1272+
and (current_time - self._last_update_time) < self._min_update_interval
1273+
):
12661274
return
12671275
self._last_update_time = current_time
12681276

src/xulbux/format_codes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ def remove(
455455
--------------------------------------------------------------------------------------------------------
456456
- `string` -⠀the string that contains the formatting codes to remove
457457
- `default_color` -⠀the default text color to use if no other text color was applied
458-
- `get_removals` -⠀if true, additionally to the cleaned string, a list of tuples will be returned,
458+
- `get_removals` -⠀if true, additionally to the cleaned string, a list of tuples will be returned,
459459
where each tuple contains the position of the removed formatting code and the removed formatting code
460460
- `_ignore_linebreaks` -⠀whether to ignore line breaks for the removal positions"""
461461
return FormatCodes.remove_ansi(
@@ -473,7 +473,7 @@ def remove_ansi(
473473
"""Removes all ANSI codes from the string with optional tracking of removed codes.\n
474474
---------------------------------------------------------------------------------------------------
475475
- `ansi_string` -⠀the string that contains the ANSI codes to remove
476-
- `get_removals` -⠀if true, additionally to the cleaned string, a list of tuples will be returned,
476+
- `get_removals` -⠀if true, additionally to the cleaned string, a list of tuples will be returned,
477477
where each tuple contains the position of the removed ansi code and the removed ansi code
478478
- `_ignore_linebreaks` -⠀whether to ignore line breaks for the removal positions"""
479479
if get_removals:

0 commit comments

Comments
 (0)