Skip to content

Commit

Permalink
[PATCH] Added py.typed
Browse files Browse the repository at this point in the history
  • Loading branch information
Diapolo10 committed Jun 1, 2023
1 parent ac364b6 commit fbe7d28
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 424 deletions.
33 changes: 0 additions & 33 deletions .github/workflows/flake8.yml

This file was deleted.

30 changes: 0 additions & 30 deletions .github/workflows/pylint.yml

This file was deleted.

44 changes: 44 additions & 0 deletions .github/workflows/ruff.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# This workflow runs the Ruff linter

name: Ruff

on: [ push, pull_request ]

jobs:
ruff:
runs-on: ubuntu-latest
steps:

- name: Checkout repository
uses: actions/checkout@v3

- name: Set up Python 3.10
uses: actions/setup-python@v4
with:
python-version: '3.10'

- name: Install Poetry
uses: Gr1N/setup-poetry@v8

- name: Install library and dependencies
run: |
poetry run pip install --upgrade pip setuptools
poetry install --only linters
- name: Lint with Ruff
run: |
poetry run ruff check --fix .
echo "RUFF_SUCCESS=$([ $? -eq 0 ])" >> $GITHUB_ENV
- name: Create Pull Request
uses: peter-evans/create-pull-request@v5
if: ${{ !env.RUFF_SUCCESS }}
with:
token: ${{ secrets.GITHUB_TOKEN }}
title: "Format Python code with Ruff push"
commit-message: "[PATCH] Ruff auto-format"
body: |
There appear to be some python formatting errors in ${{ github.sha }}.
This pull request uses the Ruff formatter to fix some of these issues.
base: ${{ github.head_ref }} # Creates pull request onto pull request or commit branch
branch: actions/ruff
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Yet another ANSI escape sequence library for Python - now modernised!

## Installation

The package is readily available on PyPI. There are no dependencies, but Python 3.6 or newer is required.
The package is readily available on PyPI. There are no dependencies, but Python 3.9 or newer is required.

On Windows:

Expand Down
4 changes: 2 additions & 2 deletions escapyde/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""A library for simplifying ANSI escape sequences in Python"""

from .ansi import *
from .colours import *
from escapyde.ansi import *
from escapyde.colours import *
29 changes: 18 additions & 11 deletions escapyde/ansi.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,52 @@
"""Main ANSI Escape sequence class"""

from typing import Any, Dict, Iterable, Optional
from __future__ import annotations

__all__ = ('AnsiEscape', 'escape_format',)
from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
from collections.abc import Iterable

__all__ = ('AnsiEscape', 'escape_format')

_CLEAR: str = '\033[0m'


class AnsiEscape:
"""Wrapper for ANSI escape sequences that makes use of operators as syntactic sugar"""

def __init__(self, colour: Optional[Iterable[int]] = None):
def __init__(self: AnsiEscape, colour: Iterable[int] | None = None) -> None:
self.sequence: str = f'\033[{";".join(str(rgb) for rgb in colour)}m' if colour is not None else ''
self.string: Optional[str] = None
self.string: str | None = None

def __str__(self) -> str:
def __str__(self: AnsiEscape) -> str:
if self.string:
return self.sequence + self.string + _CLEAR

return ''

def __or__(self, other: Any) -> 'AnsiEscape':
def __or__(self: AnsiEscape, other: Any) -> AnsiEscape:
if isinstance(other, AnsiEscape):
self.sequence += other.sequence
return self

self.string = str(other)
return self

def __ror__(self, other: Any) -> 'AnsiEscape':
def __ror__(self: AnsiEscape, other: Any) -> AnsiEscape:
return self.__or__(other)


def escape_format(string: str, escape_map: Dict[str, AnsiEscape], case_sensitive: bool=False) -> str:
def escape_format(string: str, escape_map: dict[str, AnsiEscape], case_sensitive: bool = False) -> str:
"""
Maps a dictionary of substrings => escape sequences to the given string,
returning a new string with the sequences applied to all
found substrings.
Example:
import escapyde as esc
COLOURS = {
'red': esc.FRED,
'green': esc.FGREEN,
Expand All @@ -54,7 +61,7 @@ def escape_format(string: str, escape_map: Dict[str, AnsiEscape], case_sensitive
text = \"\"\"Hello, red world! The sun is bright yellow, and the sky cyan blue.
Green, lush fields are all around us.\"\"\"
print(escape_format(text, COLOURS)) # Would print all mapped words in their respective colours
print(esc.escape_format(text, COLOURS)) # Would print all mapped words in their respective colours
Inspired by: https://www.reddit.com/r/learnpython/comments/rvcg0l/print_colour_in_terminal/hr73v3f/
"""
Expand All @@ -68,8 +75,8 @@ def escape_format(string: str, escape_map: Dict[str, AnsiEscape], case_sensitive
for idx, word in enumerate(words):

if not case_sensitive:
substring = substring.lower()
word = word.lower()
substring = substring.lower() # noqa: PLW2901
word = word.lower() # noqa: PLW2901

if word.startswith(substring):
words[idx] = f'{escape | word[:len(substring)]}{word[len(substring):]}'
Expand Down
6 changes: 3 additions & 3 deletions escapyde/colours.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Sequences for colours and text formatting"""

from typing import Dict, Iterable
from collections.abc import Iterable

from .ansi import AnsiEscape
from escapyde.ansi import AnsiEscape

__all__ = (
'FBLACK', 'FRED', 'FGREEN', 'FYELLOW',
Expand All @@ -12,7 +12,7 @@
'CLEAR',
)

sequence_table: Dict[str, Iterable[int]] = {
sequence_table: dict[str, Iterable[int]] = {
# Dark colours

# Foreground
Expand Down
Loading

0 comments on commit fbe7d28

Please sign in to comment.