chapter 2.1 calculator project setup
This commit is contained in:
23
calculator/pkg/render.py
Normal file
23
calculator/pkg/render.py
Normal file
@@ -0,0 +1,23 @@
|
||||
# render.py
|
||||
|
||||
def render(expression, result):
|
||||
if isinstance(result, float) and result.is_integer():
|
||||
result_str = str(int(result))
|
||||
else:
|
||||
result_str = str(result)
|
||||
|
||||
box_width = max(len(expression), len(result_str)) + 4
|
||||
|
||||
box = []
|
||||
box.append("┌" + "─" * box_width + "┐")
|
||||
box.append(
|
||||
"│" + " " * 2 + expression + " " * (box_width - len(expression) - 2) + "│"
|
||||
)
|
||||
box.append("│" + " " * box_width + "│")
|
||||
box.append("│" + " " * 2 + "=" + " " * (box_width - 3) + "│")
|
||||
box.append("│" + " " * box_width + "│")
|
||||
box.append(
|
||||
"│" + " " * 2 + result_str + " " * (box_width - len(result_str) - 2) + "│"
|
||||
)
|
||||
box.append("└" + "─" * box_width + "┘")
|
||||
return "\n".join(box)
|
||||
Reference in New Issue
Block a user