diff --git a/.Jules/palette.md b/.Jules/palette.md new file mode 100644 index 00000000..7286458a --- /dev/null +++ b/.Jules/palette.md @@ -0,0 +1,3 @@ +## 2024-03-02 - Inline Terminal Loading Bars +**Learning:** Terminal output can get cluttered with successive `print()` statements for loading steps. Using `sys.stdout.write` in combination with the carriage return character (`\r`) allows for updating the same line in place. +**Action:** Use `sys.stdout.write` and `\r` for terminal progress/loading indicators to improve CLI UX. diff --git a/simulasyon_11.py b/simulasyon_11.py index 63f8da57..6ff5dea5 100644 --- a/simulasyon_11.py +++ b/simulasyon_11.py @@ -35,9 +35,11 @@ class Colors: # ============================================================================== def loading_bar(desc): - print(f"{Colors.CYAN}{desc}...{Colors.ENDC}") + sys.stdout.write(f"{Colors.CYAN}{desc}...{Colors.ENDC}") + sys.stdout.flush() time.sleep(0.01) - print(f"{Colors.GREEN}[OK]{Colors.ENDC}") + sys.stdout.write(f"\r{Colors.CYAN}{desc}...{Colors.ENDC} {Colors.GREEN}[OK]{Colors.ENDC}\n") + sys.stdout.flush() # ------------------------------------------------------------------------------