Skip to content

Commit

Permalink
tap miniscript indentation change
Browse files Browse the repository at this point in the history
  • Loading branch information
odudex committed Feb 7, 2025
1 parent 8626c05 commit ed8adb9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/krux/pages/home_pages/miniscript_indenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,16 @@ def indent(self, expression, max_line_width=25):
"""
Indent a MiniScript expression, breaking lines as needed to fit within max_line_width.
"""
multiple_tap_scripts = "{" in expression
tree = self._parse_expression(expression)
indented_lines = self._node_to_indented_string(tree, max_line_width)
final_lines = []
for line in indented_lines:
final_lines.extend(self._break_lines(line, max_line_width))
if multiple_tap_scripts:
# replace penultimate ")" of the last line by "}"
line = final_lines[-1]
final_lines[-1] = line[:-2] + "}" + line[-1]
return final_lines

def _split_top_level_args(self, s, delimiter=","):
Expand Down
9 changes: 6 additions & 3 deletions tests/pages/home_pages/test_miniscript_indenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,13 @@ def test_intender(mocker, amigo):
""",
),
(
"tr(A, and_v(v:pk(B,older(65535)))",
"tr(A,and_v(v:pk(B),older(65535)))",
"""
tr(
A,
and_v(
v:pk(B,older(6553))))
v:pk(B),
older(65535)))
""",
),
(
Expand All @@ -234,7 +235,7 @@ def test_intender(mocker, amigo):
{and_v(
v:multi_a(2,B,C,D),
older(6)),
multi_a(2,F,G)))
multi_a(2,F,G)})
""",
),
]
Expand All @@ -244,6 +245,7 @@ def test_intender(mocker, amigo):
indented = MiniScriptIndenter().indent(case[0], max_line_width=25)
indented = "\n".join(indented)
assert indented == case[1].strip()
assert indented.replace("\n", "").replace(" ", "") == case[0]

# Case for Yahboom/WonderMV
indented = MiniScriptIndenter().indent(case[0], max_line_width=27)
Expand All @@ -254,3 +256,4 @@ def test_intender(mocker, amigo):
else:
# Results are the same as for Amigo
assert indented == case[1].strip()
assert indented.replace("\n", "").replace(" ", "") == case[0]

0 comments on commit ed8adb9

Please sign in to comment.