Skip to content

Commit

Permalink
Merge pull request #2 from mdlockyer/dev
Browse files Browse the repository at this point in the history
v1.5.0 - Full type annotations, refactoring, and better test coverage
  • Loading branch information
mdlockyer authored Aug 25, 2020
2 parents ce01e34 + eb6edae commit 4ae68ad
Show file tree
Hide file tree
Showing 7 changed files with 349 additions and 325 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
language: python
python:
- "3.4"
- "3.5"
- "3.6"
- "3.7"
- "3.8"
install:
- pip install pytest
- pip install pytest-cov
Expand Down
2 changes: 1 addition & 1 deletion PrintTags/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

__version__ = '1.4.0'
__version__ = '1.5.0'

from .colors import Colors

Expand Down
86 changes: 45 additions & 41 deletions PrintTags/colors.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
# -*- coding: utf-8 -*-

# Verify that using ANSI color is supported
from os import getenv
_color_supported = getenv('ANSI_COLORS_DISABLED') is None

# ANSI color codes
_black_color = 30
_red_color = 31
_green_color = 32
_yellow_color = 33
_blue_color = 34
_magenta_color = 35
_cyan_color = 36
_white_color = 37
from enum import Enum

# Verify that using ANSI color is supported
_color_is_supported: bool = getenv('ANSI_COLORS_DISABLED') is None


class _ANSIColor(Enum):
COLOR_BLACK = 30
COLOR_RED = 31
COLOR_GREEN = 32
COLOR_YELLOW = 33
COLOR_BLUE = 34
COLOR_MAGENTA = 35
COLOR_CYAN = 36
COLOR_WHITE = 37


# ANSI string format
_base_string = '\033[0;{}m{}\033[0m'
_base_string: str = '\033[0;{}m{}\033[0m'


def _colorize_string(string, color=_black_color) -> str:
if _color_supported:
string = _base_string.format(color, string)
return string
def _colorize_string(value: str, color: _ANSIColor) -> str:
if _color_is_supported:
value = f'\033[0;{color.value}m{value}\033[0m'
return value


class Colors(object):
Expand All @@ -32,100 +36,100 @@ class Colors(object):
"""

@staticmethod
def black(string) -> str:
def black(value: str) -> str:
"""
Colorizes a string to black
Args:
string (str): The string to colorize
value (str): The string to colorize
Returns:
str: The colorized string
"""
return _colorize_string(string, color=_black_color)
return _colorize_string(value, color=_ANSIColor.COLOR_BLACK)

@staticmethod
def red(string) -> str:
def red(value: str) -> str:
"""
Colorizes a string to red
Args:
string (str): The string to colorize
value (str): The string to colorize
Returns:
str: The colorized string
"""
return _colorize_string(string, color=_red_color)
return _colorize_string(value, color=_ANSIColor.COLOR_RED)

@staticmethod
def green(string) -> str:
def green(value: str) -> str:
"""
Colorizes a string to green
Args:
string (str): The string to colorize
value (str): The string to colorize
Returns:
str: The colorized string
"""
return _colorize_string(string, color=_green_color)
return _colorize_string(value, color=_ANSIColor.COLOR_GREEN)

@staticmethod
def yellow(string) -> str:
def yellow(value: str) -> str:
"""
Colorizes a string to yellow
Args:
string (str): The string to colorize
value (str): The string to colorize
Returns:
str: The colorized string
"""
return _colorize_string(string, color=_yellow_color)
return _colorize_string(value, color=_ANSIColor.COLOR_YELLOW)

@staticmethod
def blue(string) -> str:
def blue(value: str) -> str:
"""
Colorizes a string to blue
Args:
string (str): The string to colorize
value (str): The string to colorize
Returns:
str: The colorized string
"""
return _colorize_string(string, color=_blue_color)
return _colorize_string(value, color=_ANSIColor.COLOR_BLUE)

@staticmethod
def magenta(string) -> str:
def magenta(value: str) -> str:
"""
Colorizes a string to magenta
Args:
string (str): The string to colorize
value (str): The string to colorize
Returns:
str: The colorized string
"""
return _colorize_string(string, color=_magenta_color)
return _colorize_string(value, color=_ANSIColor.COLOR_MAGENTA)

@staticmethod
def cyan(string) -> str:
def cyan(value: str) -> str:
"""
Colorizes a string to cyan
Args:
string (str): The string to colorize
value (str): The string to colorize
Returns:
str: The colorized string
"""
return _colorize_string(string, color=_cyan_color)
return _colorize_string(value, color=_ANSIColor.COLOR_CYAN)

@staticmethod
def white(string) -> str:
def white(value: str) -> str:
"""
Colorizes a string to white
Args:
string (str): The string to colorize
value (str): The string to colorize
Returns:
str: The colorized string
"""
return _colorize_string(string, color=_white_color)
return _colorize_string(value, color=_ANSIColor.COLOR_WHITE)


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit 4ae68ad

Please sign in to comment.