From 855b49f236e61e887b4b4ddfe3459872842c8efd Mon Sep 17 00:00:00 2001 From: beucismis Date: Sun, 14 Apr 2024 10:55:26 +0300 Subject: [PATCH] Bump version --- src/tinyfetch/__init__.py | 4 +++- src/tinyfetch/cli.py | 16 +++++++++++++--- src/tinyfetch/core.py | 35 ++++++++++++++++++----------------- src/tinyfetch/module.py | 36 +++++++++++++++++++++++++++--------- 4 files changed, 61 insertions(+), 30 deletions(-) diff --git a/src/tinyfetch/__init__.py b/src/tinyfetch/__init__.py index 27fdca4..0c61219 100644 --- a/src/tinyfetch/__init__.py +++ b/src/tinyfetch/__init__.py @@ -1 +1,3 @@ -__version__ = "0.0.3" +from tinyfetch.module import Color, Module + +__version__ = "0.0.4" diff --git a/src/tinyfetch/cli.py b/src/tinyfetch/cli.py index e0970fa..1bd16a1 100644 --- a/src/tinyfetch/cli.py +++ b/src/tinyfetch/cli.py @@ -1,7 +1,7 @@ import argparse import tinyfetch -from tinyfetch import core +from tinyfetch import core, module def main() -> None: @@ -9,6 +9,17 @@ def main() -> None: prog="tinyfetch", description="Python and system information command-line fetch tool", ) + parser.add_argument( + "--title-color", + default="blue", + choices=list(c.name for c in module.Color), + help="set default the title color", + ) + parser.add_argument( + "--no-color", + action="store_true", + help="turn off all colors and disables", + ) parser.add_argument( "--version", action="version", @@ -16,5 +27,4 @@ def main() -> None: ) args = parser.parse_args() - if args: - core.render() + core.render(title_color=args.title_color, no_color=args.no_color) diff --git a/src/tinyfetch/core.py b/src/tinyfetch/core.py index 565990d..50b456b 100644 --- a/src/tinyfetch/core.py +++ b/src/tinyfetch/core.py @@ -1,21 +1,22 @@ from tinyfetch import module +from tinyfetch.module import Color +modules_list = [ + module.Space, + module.UserHost, + module.SplitLine, + module.PythonVersion, + module.PIPVersion, + module.PIPPackages, + module.Implementation, + module.Compiler, + module.Space, + module.Kernel, + module.OperationSystem, + module.Space, +] -def render() -> None: - modules = [ - module.Space(), - module.UserHost(), - module.SplitLine(), - module.PythonVersion(), - module.PIPVersion(), - module.PIPPackages(), - module.Implementation(), - module.Compiler(), - module.Space(), - module.Kernel(), - module.OperationSystem(), - module.Space(), - ] - for m in modules: - print(m.output()) +def render(title_color: str, no_color: bool = False) -> None: + for m in modules_list: + print(m.__call__(title_color=Color[title_color], no_color=no_color).output()) diff --git a/src/tinyfetch/module.py b/src/tinyfetch/module.py index d3db88b..5c5c690 100644 --- a/src/tinyfetch/module.py +++ b/src/tinyfetch/module.py @@ -3,28 +3,42 @@ import os import platform from dataclasses import dataclass, field +from enum import Enum from pathlib import Path +from typing import Union -RESET = "\u001b[0m" BOLD = "\u001b[1m" -MAIN = BOLD + "\u001b[034m" +RESET = "\u001b[0m" + + +class Color(Enum): + red = "\u001b[31m" + green = "\u001b[32m" + yellow = "\u001b[33m" + blue = "\u001b[34m" + magenta = "\u001b[35m" + cyan = "\u001b[36m" @dataclass class Module: - title: str = field(init=False, default=None) + title: Union[str, None] = field(init=False, default=None) value: str = field(init=False) + title_color: str = field(default=Color["blue"]) + no_color: bool = field(default=False) - def output(self): + def output(self) -> str: if self.title is None: return self.value - return f"{MAIN}{self.title}:{RESET} {self.value}" + if self.no_color: + return f"{self.title}: {self.value}" + return f"{BOLD}{self.title_color.value}{self.title}:{RESET} {self.value}" @dataclass class Space(Module): def __post_init__(self): - self.value = " " + self.value = "" @dataclass @@ -33,9 +47,13 @@ def __post_init__(self): user = getpass.getuser() host = os.uname().nodename self.userhost = f"{user}@{host}" - self.value = MAIN + self.userhost + RESET - def __len__(self): + if self.no_color: + self.value = self.userhost + else: + self.value = f"{BOLD}{self.title_color.value}{self.userhost}{RESET}" + + def __len__(self) -> int: return len(self.userhost) @@ -102,7 +120,7 @@ def __post_init__(self): if os.name == "posix": self.value = self.posix_os_name() - def posix_os_name(self): + def posix_os_name(self) -> str: path = Path("/etc/os-release") with open(path) as file: reader = csv.reader(file, delimiter="=")