Skip to content

Commit

Permalink
rich console output
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben Horn committed Aug 15, 2024
1 parent 3d586f8 commit 27fa12b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
8 changes: 4 additions & 4 deletions examples/config-generator/src/generators/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ def apply(partial_xml, get_config_value) -> None:
simulation_type = get_config_value(key)
domain_size = get_domain_size(get_config_value)
if simulation_type == "test":
partial_xml.substitute("coupling-cycles", 50)
partial_xml.substitute("coupling-cycles", 50)
elif simulation_type == "short":
partial_xml.substitute("coupling-cycles", 250 * domain_size**2)
partial_xml.substitute("coupling-cycles", 250 * domain_size**2)
elif simulation_type == "normal":
partial_xml.substitute("coupling-cycles", 500 * domain_size**2)
partial_xml.substitute("coupling-cycles", 500 * domain_size**2)
else:
raise ValueError(key + " = " + simulation_type)
raise ValueError(key + " = " + simulation_type)

print("Substituted coupling cycles")
14 changes: 11 additions & 3 deletions examples/config-generator/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def main() -> None:

while True:
# 2. Create the application menu
title = "MaMiCo couette.xml Generator\n"
title = term.fmt_bold("MaMiCo couette.xml Generator\n")
main_menu = []
for config in configs:
# Apply overrides from command line
Expand All @@ -265,10 +265,18 @@ def main() -> None:
else:
if is_valid:
main_menu[-1] += "\n"
main_menu.append("Generate")
main_menu.append("Generate config file(s)")
else:
title += "\n" + validation_errors + "\n"
validation_errors = "\n".join(
[term.fmt_red(s) for s in validation_errors.splitlines()]
)
title += (
term.fmt_red(term.fmt_bold("\nValidation errors:\n"))
+ validation_errors
+ "\n"
)
# 4. Show and the menu
title += "\n(Press Ctrl+C to quit without generating anything)\n"
selected = term.select(
main_menu,
title=title,
Expand Down
24 changes: 24 additions & 0 deletions examples/config-generator/src/term.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,27 @@ def select(
def clear() -> None:
"""Clears all output from the terminal."""
os.system("cls" if os.name == "nt" else "clear")


def fmt_bold(text: str) -> None:
"""Returns text formatted to be printed to the terminal in bold font.
On windows this has no effect.
Keyword arguments:
text -- The text to be formatted
"""
if os.name == "nt":
return text
return f"\033[1m{text}\033[0m"


def fmt_red(text: str) -> None:
"""Returns text formatted to be printed to the terminal in red font.
On windows this has no effect.
Keyword arguments:
text -- The text to be formatted
"""
if os.name == "nt":
return text
return f"\033[0;31m{text}\033[0m"

0 comments on commit 27fa12b

Please sign in to comment.