mirror of
https://github.com/soxoj/maigret.git
synced 2026-09-06 18:07:39 +02:00
Fix get_dict_ascii_tree ignoring new_line=False (#2805)
`get_dict_ascii_tree(items, prepend="", new_line=True)` immediately reassigned
`new_line` to the horizontal box-drawing glyph ("─"), shadowing the boolean
parameter. The trailing `if not new_line: text = text[1:]` — meant to strip the
leading newline — therefore tested a non-empty string (always truthy) and never
ran. Callers passing `new_line=False` (e.g. `maigret.py` printing a user's
identity data) got a stray leading blank line.
Rename the glyph local to `h_line` so the `new_line` parameter is preserved and
its strip takes effect. The tree drawing is unchanged.
Adds a regression test asserting `new_line=False` drops the leading newline
while keeping the rest of the tree identical.
This commit is contained in:
+2
-2
@@ -84,14 +84,14 @@ def ascii_data_display(data: str) -> Any:
|
||||
|
||||
def get_dict_ascii_tree(items, prepend="", new_line=True):
|
||||
new_result = b'\xe2\x94\x9c'.decode()
|
||||
new_line = b'\xe2\x94\x80'.decode()
|
||||
h_line = b'\xe2\x94\x80'.decode()
|
||||
last_result = b'\xe2\x94\x94'.decode()
|
||||
skip_result = b'\xe2\x94\x82'.decode()
|
||||
|
||||
text = ""
|
||||
for num, item in enumerate(items):
|
||||
box_symbol = (
|
||||
new_result + new_line if num != len(items) - 1 else last_result + new_line
|
||||
new_result + h_line if num != len(items) - 1 else last_result + h_line
|
||||
)
|
||||
|
||||
if isinstance(item, tuple):
|
||||
|
||||
@@ -233,3 +233,16 @@ def test_is_plausible_username_rejects_non_strings():
|
||||
assert not is_plausible_username(None)
|
||||
assert not is_plausible_username(42)
|
||||
assert not is_plausible_username(["alice"])
|
||||
|
||||
|
||||
def test_get_dict_ascii_tree_new_line_false_strips_leading_newline():
|
||||
# new_line=False must drop the leading newline. It used to be a no-op
|
||||
# because the parameter was shadowed by the horizontal box-drawing glyph.
|
||||
items = [('a', '1'), ('b', '2')]
|
||||
with_nl = get_dict_ascii_tree(items)
|
||||
without_nl = get_dict_ascii_tree(items, new_line=False)
|
||||
|
||||
assert with_nl.startswith('\n')
|
||||
assert not without_nl.startswith('\n')
|
||||
# Only the leading newline differs; the tree content is identical.
|
||||
assert with_nl[1:] == without_nl
|
||||
|
||||
Reference in New Issue
Block a user