Skip to content

Commit 64e7fa9

Browse files
matteiusoz123
authored andcommitted
PR Feedback
1 parent fbee93c commit 64e7fa9

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ dependencies = [
3939
]
4040

4141
[project.optional-dependencies]
42-
cli = ["click"]
42+
cli = ["click", "colorama"]
4343
tests = [
4444
"pytest",
4545
"pytest-timeout",

src/pythonfinder/cli.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,35 @@
11
from __future__ import annotations
22

33
import argparse
4+
import os
5+
import platform
46
import sys
57

68
from . import __version__
79
from .pythonfinder import Finder
810

11+
# Use colorama for cross-platform color support if available
12+
try:
13+
import colorama
14+
colorama.init()
15+
HAS_COLORAMA = True
16+
except ImportError:
17+
HAS_COLORAMA = False
18+
919

1020
def colorize(text: str, color: str | None = None, bold: bool = False) -> str:
1121
"""
1222
Simple function to colorize text for terminal output.
23+
24+
Uses colorama for cross-platform support if available.
25+
Falls back to ANSI escape codes on Unix/Linux systems.
26+
On Windows without colorama, returns plain text.
1327
"""
28+
# Check if colors should be disabled
29+
if "ANSI_COLORS_DISABLED" in os.environ:
30+
return text
31+
32+
# Define ANSI color codes
1433
colors = {
1534
"red": "\033[31m",
1635
"green": "\033[32m",
@@ -25,9 +44,14 @@ def colorize(text: str, color: str | None = None, bold: bool = False) -> str:
2544
bold_code = "\033[1m" if bold else ""
2645
color_code = colors.get(color, "")
2746

47+
# If no color or bold requested, return plain text
2848
if not color_code and not bold:
2949
return text
3050

51+
# On Windows without colorama, return plain text
52+
if platform.system() == "Windows" and not HAS_COLORAMA:
53+
return text
54+
3155
return f"{bold_code}{color_code}{text}{reset}"
3256

3357

src/pythonfinder/models/python_info.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class PythonInfo:
2020

2121
path: Path
2222
version_str: str
23-
major: int
23+
major: int | None
2424
minor: int | None = None
2525
patch: int | None = None
2626
is_prerelease: bool = False
@@ -49,7 +49,7 @@ def as_python(self) -> PythonInfo:
4949
return self
5050

5151
@property
52-
def version_tuple(self) -> tuple[int, int | None, int | None, bool, bool, bool]:
52+
def version_tuple(self) -> tuple[int | None, int | None, int | None, bool, bool, bool]:
5353
"""
5454
Provides a version tuple for using as a dictionary key.
5555
"""
@@ -79,7 +79,7 @@ def version_sort(self) -> tuple[int, int, int, int, int]:
7979
release_sort = 1
8080
return (
8181
company_sort,
82-
self.major,
82+
self.major or 0, # Handle None case by defaulting to 0
8383
self.minor or 0,
8484
self.patch or 0,
8585
release_sort,
@@ -143,7 +143,7 @@ def as_dict(self) -> dict[str, Any]:
143143
Convert this PythonInfo to a dictionary.
144144
"""
145145
return {
146-
"major": self.major,
146+
"major": self.major, # Can be None
147147
"minor": self.minor,
148148
"patch": self.patch,
149149
"is_prerelease": self.is_prerelease,

0 commit comments

Comments
 (0)