Skip to content

Commit

Permalink
Fix CLI spinner on PyPy on Windows (#396)
Browse files Browse the repository at this point in the history
Closes: #274
Amends: #393

The CLI spinner should show ⢎⡡ but PyPy has problems with unicode
characters on Windows (printed as ÔóÄÔí░). Therefore, using the old
["|", "/", "-", "\\"] spinner when on PyPy+Windows.
  • Loading branch information
fohrloop authored Sep 20, 2024
1 parent c9ae918 commit 55cce46
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/source/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

### ✨ Features
- Add support for BSD and other non-Linux Unix-like FOSS desktop systems. All systems running a supported Desktop Environment (currently: KDE, Gnome + other freedesktop compliant DEs) should be supported. ([#379](https://github.com/fohrloop/wakepy/pull/379), [#385](https://github.com/fohrloop/wakepy/pull/385))
- Add support for PyPy ([#393](https://github.com/fohrloop/wakepy/pull/393) and [#396](https://github.com/fohrloop/wakepy/pull/396))
- When running on an *unknown* platform, do not fail any Methods in the platform check phase anymore, but try to use each Method. This means for example that any system running GNOME that is not Linux (or BSD) could still use wakepy with the [org.gnome.SessionManager](https://wakepy.readthedocs.io/stable/methods-reference.html#org-gnome-sessionmanager) ([#379](https://github.com/fohrloop/wakepy/pull/379))
- 🚨 CLI arguments: Change `-k, --keep-running` to be `-r, --keep-running` and `-p, --presentation` to be `-p, --keep-presenting`; Be more consistent with the naming of the [Modes](#wakepy-modes). The old alternatives are deprecated and will be removed in a future release. ([#356](https://github.com/fohrloop/wakepy/pull/356))
- 🚨 Renamed [`PlatformName`](https://wakepy.readthedocs.io/v0.9.0.post1/api-reference.html#wakepy.core.constants.PlatformName) to [`PlatformType`](https://wakepy.readthedocs.io/v0.10.0/api-reference.html#wakepy.core.constants.PlatformType) and added new types: `ANY`, which means "any platform", `BSD`, meaning "any BSD system" in the future, but currently just FreeBSD / GhostBSD, and `UNIX_LIKE_FOSS`, which means "Unix-like desktop environment, but FOSS". Includes: Linux and BSD. Excludes: Android (mobile), MacOS (non-FOSS), ChromeOS (non-FOSS). Only affects you if you have created custom [Method](https://wakepy.readthedocs.io/v0.9.0.post1/api-reference.html#wakepy.Method) subclasses. ([#379](https://github.com/fohrloop/wakepy/pull/379))
Expand Down
21 changes: 18 additions & 3 deletions src/wakepy/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import argparse
import itertools
import platform
import sys
import time
import typing
Expand All @@ -21,7 +22,7 @@
from wakepy import ModeExit
from wakepy.core.constants import ModeName
from wakepy.core.mode import Mode
from wakepy.core.platform import get_platform_debug_info
from wakepy.core.platform import CURRENT_PLATFORM, get_platform_debug_info, is_windows

if typing.TYPE_CHECKING:
from typing import List, Tuple
Expand Down Expand Up @@ -190,7 +191,7 @@ def get_startup_text(mode: ModeName) -> str:
from wakepy import __version__

wakepy_text = WAKEPY_TEXT_TEMPLATE.format(
VERSION_STRING=f"{' v.'+__version__: <28}"
VERSION_STRING=f"{' v.'+__version__[:24]: <24}"
)
options_txt = WAKEPY_TICKBOXES_TEMPLATE.strip("\n").format(
no_auto_suspend="x",
Expand All @@ -200,7 +201,7 @@ def get_startup_text(mode: ModeName) -> str:


def wait_until_keyboardinterrupt() -> None:
spinner_symbols = ["⢎⡰", "⢎⡡", "⢎⡑", "⢎⠱", "⠎⡱", "⢊⡱", "⢌⡱", "⢆⡱"]
spinner_symbols = get_spinner_symbols()
try:
for spinner_symbol in itertools.cycle(spinner_symbols): # pragma: no branch
print("\r " + spinner_symbol + r" [Press Ctrl+C to exit] ", end="")
Expand All @@ -209,5 +210,19 @@ def wait_until_keyboardinterrupt() -> None:
pass


def get_spinner_symbols() -> list[str]:

if (
is_windows(CURRENT_PLATFORM)
and platform.python_implementation().lower() == "pypy"
):
# Windows + PyPy combination does not support unicode well, at least
# yet at version 7.3.17. See:
# https://github.com/pypy/pypy/issues/3890
# https://github.com/fohrloop/wakepy/issues/274#issuecomment-2363293422
return ["|", "/", "-", "\\"]
return ["⢎⡰", "⢎⡡", "⢎⡑", "⢎⠱", "⠎⡱", "⢊⡱", "⢌⡱", "⢆⡱"]


if __name__ == "__main__":
main() # pragma: no cover
14 changes: 13 additions & 1 deletion tests/unit/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
from wakepy import ActivationResult, Method
from wakepy.__main__ import (
_get_activation_error_text,
get_spinner_symbols,
get_startup_text,
handle_activation_error,
main,
parse_arguments,
wait_until_keyboardinterrupt,
)
from wakepy.core import PlatformType
from wakepy.core.constants import ModeName
from wakepy.core.constants import IdentifiedPlatformType, ModeName


@pytest.fixture
Expand Down Expand Up @@ -214,3 +215,14 @@ def sys_argv(self):
# The patched value for sys.argv. Does not matter here otherwise, but
# should be a list of at least two items.
return ["", ""]


class TestGetSpinnerSymbols:
@patch("wakepy.__main__.CURRENT_PLATFORM", IdentifiedPlatformType.LINUX)
def test_on_linux(self):
assert get_spinner_symbols() == ["⢎⡰", "⢎⡡", "⢎⡑", "⢎⠱", "⠎⡱", "⢊⡱", "⢌⡱", "⢆⡱"]

@patch("wakepy.__main__.CURRENT_PLATFORM", IdentifiedPlatformType.WINDOWS)
@patch("wakepy.__main__.platform.python_implementation", lambda: "PyPy")
def test_on_windows_pypy(self):
assert get_spinner_symbols() == ["|", "/", "-", "\\"]

0 comments on commit 55cce46

Please sign in to comment.