Files
008-caesar-cipher/src/caesar_cipher/main.py
T
Tirsvad 9d8beb3aef Refactor to unified caesar() function with encode/decode support
- Replace separate encrypt/decrypt with shared caesar() function
- Add direction constants (DIRECTION_ENCODE, DIRECTION_DECODE) and
  prompts
- Update main.py to support interactive encode/decode loop
- Keep encrypt/decrypt as wrappers for backward compatibility
- Add tests for caesar() with both directions and error handling
- Update tests for main.py with interactive scenario tests
2026-10-04 13:23:47 +08:00

44 lines
1.3 KiB
Python

## @file main.py
# @brief Interactive command line interface.
import os
import sys
from caesar_cipher import constants as c
from caesar_cipher.cipher import caesar
def print_title() -> None:
## @brief Print the ASCII title, in colour when the terminal supports it.
if sys.stdout.isatty() and "NO_COLOR" not in os.environ:
os.system("") # enables ANSI escape sequences on older Windows consoles
print(f"{c.COLOR_TITLE}{c.TITLE}{c.COLOR_RESET}")
else:
print(c.TITLE)
def main() -> None:
## @brief Run the interactive encode/decode loop until the user stops.
print_title()
while True:
direction = input(c.PROMPT_DIRECTION).strip().lower()
if direction not in (c.DIRECTION_ENCODE, c.DIRECTION_DECODE):
print("Please type 'encode' or 'decode'.")
continue
text = input(c.PROMPT_TEXT)
try:
shift = int(input(c.PROMPT_SHIFT))
except ValueError:
print("The shift must be a whole number.")
continue
print(f"Here's the {direction}d result: {caesar(text, shift, direction)}")
if input(c.PROMPT_AGAIN).strip().lower() != "yes":
print("Goodbye")
break
if __name__ == "__main__":
main()