Skip to content

Commit

Permalink
test: maybe fix windows cli tests (#3146)
Browse files Browse the repository at this point in the history
Fix windows testing hanging from colors issue
  • Loading branch information
mscolnick authored Dec 12, 2024
1 parent 2367332 commit f7ab973
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test_cli.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ jobs:
- name: Test CLI
shell: bash
run: |
pytest -v tests/_cli/test_cli*
pytest -v tests/_cli/test_cli* --maxfail=2
test_examples:
needs: [changes, build_wheel]
Expand Down
46 changes: 42 additions & 4 deletions marimo/_cli/print.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,77 @@

from __future__ import annotations

from typing import Any
import os
import sys
from typing import Any, cast

from marimo._config.settings import GLOBAL_SETTINGS

# Print helpers

# Check if we're on Windows and if ANSI colors are supported
def _supports_color() -> bool:
if os.environ.get("NO_COLOR") == "1":
return False

# Windows 10 build 14931+ supports ANSI color codes
if os.name == "nt":
try:
import ctypes

kernel32 = ctypes.windll.kernel32 # type: ignore[attr-defined]
# Get Windows version
major = sys.getwindowsversion().major # type: ignore[attr-defined]
build = sys.getwindowsversion().build # type: ignore[attr-defined]

# Check if Windows 10+ and if VIRTUAL_TERMINAL_PROCESSING is enabled
return cast(
bool,
major >= 10
and build >= 14931
and kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7), # type: ignore[attr-defined]
)
except Exception:
return False
return hasattr(sys.stdout, "isatty") and sys.stdout.isatty()


_USE_COLOR = _supports_color()


def bold(text: str) -> str:
return "\033[1m" + text + "\033[0m"
return "\033[1m" + text + "\033[0m" if _USE_COLOR else text


def green(text: str, bold: bool = False) -> str:
if not _USE_COLOR:
return text
prefix = "\033[32m" if not bold else "\033[1;32m"
return prefix + text + "\033[0m"


def yellow(text: str, bold: bool = False) -> str:
if not _USE_COLOR:
return text
prefix = "\033[33m" if not bold else "\033[1;33m"
return prefix + text + "\033[0m"


def orange(text: str, bold: bool = False) -> str:
if not _USE_COLOR:
return text
prefix = "\033[33m" if not bold else "\033[1;33m"
return prefix + text + "\033[0m"


def red(text: str, bold: bool = False) -> str:
if not _USE_COLOR:
return text
prefix = "\033[31m" if not bold else "\033[1;31m"
return prefix + text + "\033[0m"


def muted(text: str) -> str:
return "\033[90m" + text + "\033[0m"
return "\033[90m" + text + "\033[0m" if _USE_COLOR else text


def echo(*args: Any, **kwargs: Any) -> None:
Expand Down
4 changes: 2 additions & 2 deletions marimo/_server/print.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ def print_startup(
f"{green('Create or edit notebooks in your browser', bold=True)} {_utf8('📝')}"
)
print_()
print_tabbed(f" {green('URL')}: {_colorized_url(url)}")
print_tabbed(f"{_utf8('➜')} {green('URL')}: {_colorized_url(url)}")
if network:
print_tabbed(
f" {green('Network')}: {_colorized_url(_get_network_url(url))}"
f"{_utf8('➜')} {green('Network')}: {_colorized_url(_get_network_url(url))}"
)
print_()

Expand Down

0 comments on commit f7ab973

Please sign in to comment.