diff --git a/src/fastapi_cli/cli.py b/src/fastapi_cli/cli.py index d000bef..a29c895 100644 --- a/src/fastapi_cli/cli.py +++ b/src/fastapi_cli/cli.py @@ -1,4 +1,5 @@ import logging +import sys from pathlib import Path from typing import Annotated, Any, Union @@ -79,21 +80,37 @@ def callback( def _get_module_tree(module_paths: list[Path]) -> Tree: root = module_paths[0] - name = f"🐍 {root.name}" if root.is_file() else f"📁 {root.name}" + if sys.platform == "win32": + name = f"Python {root.name}" if root.is_file() else f"Folder {root.name}" + else: + name = f"🐍 {root.name}" if root.is_file() else f"📁 {root.name}" root_tree = Tree(name) if root.is_dir(): - root_tree.add("[dim]🐍 __init__.py[/dim]") + if sys.platform == "win32": + root_tree.add("[dim]Python __init__.py[/dim]") + else: + root_tree.add("[dim]🐍 __init__.py[/dim]") tree = root_tree for sub_path in module_paths[1:]: - sub_name = ( - f"🐍 {sub_path.name}" if sub_path.is_file() else f"📁 {sub_path.name}" - ) + if sys.platform == "win32": + sub_name = ( + f"Python {sub_path.name}" + if sub_path.is_file() + else f"Folder {sub_path.name}" + ) + else: + sub_name = ( + f"🐍 {sub_path.name}" if sub_path.is_file() else f"📁 {sub_path.name}" + ) tree = tree.add(sub_name) if sub_path.is_dir(): - tree.add("[dim]🐍 __init__.py[/dim]") + if sys.platform == "win32": + tree.add("[dim]Python __init__.py[/dim]") + else: + tree.add("[dim]🐍 __init__.py[/dim]") return root_tree @@ -116,7 +133,12 @@ def _run( with get_rich_toolkit() as toolkit: server_type = "development" if command == "dev" else "production" - toolkit.print_title(f"Starting {server_type} server 🚀", tag="FastAPI") + if sys.platform == "win32": + title = f"Starting {server_type} server" + else: + title = f"Starting {server_type} server 🚀" + + toolkit.print_title(title, tag="FastAPI") toolkit.print_line() toolkit.print( diff --git a/src/fastapi_cli/logging.py b/src/fastapi_cli/logging.py index 67f116c..c08b339 100644 --- a/src/fastapi_cli/logging.py +++ b/src/fastapi_cli/logging.py @@ -1,4 +1,5 @@ import logging +import sys from typing import Union from rich.console import Console @@ -9,7 +10,10 @@ def setup_logging( terminal_width: Union[int, None] = None, level: int = logging.INFO ) -> None: logger = logging.getLogger("fastapi_cli") - console = Console(width=terminal_width) if terminal_width else None + if sys.platform == "win32": + console = Console(width=terminal_width or 80, emoji=False, legacy_windows=True) + else: + console = Console(width=terminal_width or 80) rich_handler = RichHandler( show_time=False, rich_tracebacks=True, diff --git a/tests/test_cli.py b/tests/test_cli.py index ddfb808..dd5d524 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -35,7 +35,10 @@ def test_dev() -> None: "log_config": get_uvicorn_log_config(), } assert "Using import string: single_file_app:app" in result.output - assert "Starting development server 🚀" in result.output + if sys.platform == "win32": + assert "Starting development server" in result.output + else: + assert "Starting development server 🚀" in result.output assert "Server started at http://127.0.0.1:8000" in result.output assert "Documentation at http://127.0.0.1:8000/docs" in result.output assert ( @@ -43,7 +46,10 @@ def test_dev() -> None: in result.output ) - assert "🐍 single_file_app.py" in result.output + if sys.platform == "win32": + assert "Python single_file_app.py" in result.output + else: + assert "🐍 single_file_app.py" in result.output def test_dev_no_args_auto_discovery() -> None: @@ -81,7 +87,10 @@ def test_dev_package() -> None: "log_config": get_uvicorn_log_config(), } assert "Using import string: nested_package.package:app" in result.output - assert "Starting development server 🚀" in result.output + if sys.platform == "win32": + assert "Starting development server" in result.output + else: + assert "Starting development server 🚀" in result.output assert "Server started at http://127.0.0.1:8000" in result.output assert "Documentation at http://127.0.0.1:8000/docs" in result.output assert ( @@ -89,10 +98,16 @@ def test_dev_package() -> None: in result.output ) - assert "📁 package" in result.output - assert "└── 🐍 __init__.py" in result.output - assert "└── 📁 package" in result.output - assert " └── 🐍 __init__.py" in result.output + if sys.platform == "win32": + assert "Folder package" in result.output + assert "└── Python __init__.py" in result.output + assert "└── Folder package" in result.output + assert " └── Python __init__.py" in result.output + else: + assert "📁 package" in result.output + assert "└── 🐍 __init__.py" in result.output + assert "└── 📁 package" in result.output + assert " └── 🐍 __init__.py" in result.output def test_dev_args() -> None: @@ -131,7 +146,10 @@ def test_dev_args() -> None: "log_config": get_uvicorn_log_config(), } assert "Using import string: single_file_app:api" in result.output - assert "Starting development server 🚀" in result.output + if sys.platform == "win32": + assert "Starting development server" in result.output + else: + assert "Starting development server 🚀" in result.output assert "Server started at http://192.168.0.2:8080" in result.output assert "Documentation at http://192.168.0.2:8080/docs" in result.output assert ( @@ -162,7 +180,10 @@ def test_dev_env_vars() -> None: "log_config": get_uvicorn_log_config(), } assert "Using import string: single_file_app:app" in result.output - assert "Starting development server 🚀" in result.output + if sys.platform == "win32": + assert "Starting development server" in result.output + else: + assert "Starting development server 🚀" in result.output assert "Server started at http://127.0.0.1:8111" in result.output assert "Documentation at http://127.0.0.1:8111/docs" in result.output assert ( @@ -200,7 +221,10 @@ def test_dev_env_vars_and_args() -> None: "log_config": get_uvicorn_log_config(), } assert "Using import string: single_file_app:app" in result.output - assert "Starting development server 🚀" in result.output + if sys.platform == "win32": + assert "Starting development server" in result.output + else: + assert "Starting development server 🚀" in result.output assert "Server started at http://127.0.0.1:8080" in result.output assert "Documentation at http://127.0.0.1:8080/docs" in result.output assert ( @@ -246,7 +270,10 @@ def test_run() -> None: "log_config": get_uvicorn_log_config(), } assert "Using import string: single_file_app:app" in result.output - assert "Starting production server 🚀" in result.output + if sys.platform == "win32": + assert "Starting production server" in result.output + else: + assert "Starting production server 🚀" in result.output assert "Server started at http://0.0.0.0:8000" in result.output assert "Documentation at http://0.0.0.0:8000/docs" in result.output @@ -273,7 +300,10 @@ def test_run_trust_proxy() -> None: "log_config": get_uvicorn_log_config(), } assert "Using import string: single_file_app:app" in result.output - assert "Starting production server 🚀" in result.output + if sys.platform == "win32": + assert "Starting production server" in result.output + else: + assert "Starting production server 🚀" in result.output assert "Server started at http://0.0.0.0:8000" in result.output assert "Documentation at http://0.0.0.0:8000/docs" in result.output assert ( @@ -321,7 +351,10 @@ def test_run_args() -> None: } assert "Using import string: single_file_app:api" in result.output - assert "Starting production server 🚀" in result.output + if sys.platform == "win32": + assert "Starting production server" in result.output + else: + assert "Starting production server 🚀" in result.output assert "Server started at http://192.168.0.2:8080" in result.output assert "Documentation at http://192.168.0.2:8080/docs" in result.output assert ( @@ -352,7 +385,10 @@ def test_run_env_vars() -> None: "log_config": get_uvicorn_log_config(), } assert "Using import string: single_file_app:app" in result.output - assert "Starting production server 🚀" in result.output + if sys.platform == "win32": + assert "Starting production server" in result.output + else: + assert "Starting production server 🚀" in result.output assert "Server started at http://0.0.0.0:8111" in result.output assert "Documentation at http://0.0.0.0:8111/docs" in result.output @@ -386,7 +422,10 @@ def test_run_env_vars_and_args() -> None: "log_config": get_uvicorn_log_config(), } assert "Using import string: single_file_app:app" in result.output - assert "Starting production server 🚀" in result.output + if sys.platform == "win32": + assert "Starting production server" in result.output + else: + assert "Starting production server 🚀" in result.output assert "Server started at http://0.0.0.0:8080" in result.output assert "Documentation at http://0.0.0.0:8080/docs" in result.output