Skip to content

Commit 2db109b

Browse files
authored
Merge pull request #4 from Diapolo10/nightly
[0.2.0] Release
2 parents 52b4b08 + 3ee04d9 commit 2db109b

File tree

4 files changed

+95
-25
lines changed

4 files changed

+95
-25
lines changed

CHANGELOG.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,29 @@ First official documentation.
6868

6969
_______________________________________________________________________________
7070

71+
## [0.2.0] - 2022-01-16
72+
73+
This release adds a new function, `escapyde.escape_format`, which can be used
74+
to format known substrings in a string with ANSI escape sequences. In addition
75+
the codebase now uses type hints thorought.
76+
77+
The new feature was inspired by [this Reddit thread][Reddit escape format].
78+
79+
### Added
80+
81+
- Added `escapyde.escape_format`
82+
- Added more type hints for better typing coverage
83+
84+
### Changed
85+
86+
- Updated the localisation files
87+
88+
_______________________________________________________________________________
89+
7190
## [0.1.2] - 2021-12-01
7291

73-
This release adds support for arbitrary types; previously `AnsiEscape` only supported strings.
92+
This release adds support for arbitrary types; previously `AnsiEscape` only
93+
supported strings.
7494

7595
### Changed
7696

@@ -81,7 +101,8 @@ _______________________________________________________________________________
81101

82102
## [0.1.1] - 2021-12-01
83103

84-
A hotfix release that fixes a problem in the README example code, and adds a screenshot of the code running.
104+
A hotfix release that fixes a problem in the README example code, and adds a
105+
screenshot of the code running.
85106

86107
### Added
87108

@@ -127,6 +148,8 @@ the initial commit.
127148
- Fixed an oversight related to chaining ANSI escape sequences
128149
- Fixed linter issues
129150

151+
[Reddit escape format]: https://www.reddit.com/r/learnpython/comments/rvcg0l/print_colour_in_terminal/hr73v3f/
152+
130153
<!-- markdownlint-configure-file {
131154
"MD022": false,
132155
"MD024": false,

escapyde/ansi.py

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"""Main ANSI Escape sequence class"""
22

3-
from typing import Optional, Iterable
3+
from typing import Any, Dict, Iterable, Optional
44

5-
__all__ = ('AnsiEscape',)
5+
__all__ = ('AnsiEscape', 'escape_format',)
66

7-
_CLEAR = '\033[0m'
7+
_CLEAR: str = '\033[0m'
88

99

1010
class AnsiEscape:
@@ -20,13 +20,60 @@ def __str__(self) -> str:
2020

2121
return ''
2222

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

2828
self.string = str(other)
2929
return self
3030

31-
def __ror__(self, other):
31+
def __ror__(self, other: Any) -> 'AnsiEscape':
3232
return self.__or__(other)
33+
34+
35+
def escape_format(string: str, escape_map: Dict[str, AnsiEscape], case_sensitive: bool=False) -> str:
36+
"""
37+
Maps a dictionary of substrings => escape sequences to the given string,
38+
returning a new string with the sequences applied to all
39+
found substrings.
40+
41+
Example:
42+
43+
COLOURS = {
44+
'red': esc.FRED,
45+
'green': esc.FGREEN,
46+
'yellow': esc.FYELLOW,
47+
'blue': esc.FBLUE,
48+
'magenta': esc.FMAGENTA,
49+
'cyan': esc.FCYAN,
50+
'white': esc.FWHITE,
51+
'black': esc.FBLACK,
52+
}
53+
54+
text = \"\"\"Hello, red world! The sun is bright yellow, and the sky cyan blue.
55+
Green, lush fields are all around us.\"\"\"
56+
57+
print(escape_format(text, COLOURS)) # Would print all mapped words in their respective colours
58+
59+
Inspired by: https://www.reddit.com/r/learnpython/comments/rvcg0l/print_colour_in_terminal/hr73v3f/
60+
"""
61+
62+
lines = string.splitlines()
63+
for line_idx, line in enumerate(lines):
64+
65+
words = line.split(' ')
66+
for substring, escape in escape_map.items():
67+
68+
for idx, word in enumerate(words):
69+
70+
if not case_sensitive:
71+
substring = substring.lower()
72+
word = word.lower()
73+
74+
if word.startswith(substring):
75+
words[idx] = f'{escape | word[:len(substring)]}{word[len(substring):]}'
76+
77+
lines[line_idx] = ' '.join(words)
78+
79+
return '\n'.join(lines)

escapyde/colours.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,22 @@
6161
'clear': (0,),
6262
}
6363

64-
FBLACK = AnsiEscape(sequence_table['fg_black'])
65-
FRED = AnsiEscape(sequence_table['fg_red'])
66-
FGREEN = AnsiEscape(sequence_table['fg_green'])
67-
FYELLOW = AnsiEscape(sequence_table['fg_yellow'])
68-
FBLUE = AnsiEscape(sequence_table['fg_blue'])
69-
FMAGENTA = AnsiEscape(sequence_table['fg_magenta'])
70-
FCYAN = AnsiEscape(sequence_table['fg_cyan'])
71-
FWHITE = AnsiEscape(sequence_table['fg_white'])
64+
FBLACK: AnsiEscape = AnsiEscape(sequence_table['fg_black'])
65+
FRED: AnsiEscape = AnsiEscape(sequence_table['fg_red'])
66+
FGREEN: AnsiEscape = AnsiEscape(sequence_table['fg_green'])
67+
FYELLOW: AnsiEscape = AnsiEscape(sequence_table['fg_yellow'])
68+
FBLUE: AnsiEscape = AnsiEscape(sequence_table['fg_blue'])
69+
FMAGENTA: AnsiEscape = AnsiEscape(sequence_table['fg_magenta'])
70+
FCYAN: AnsiEscape = AnsiEscape(sequence_table['fg_cyan'])
71+
FWHITE: AnsiEscape = AnsiEscape(sequence_table['fg_white'])
7272

73-
BBLACK = AnsiEscape(sequence_table['bg_black'])
74-
BRED = AnsiEscape(sequence_table['bg_red'])
75-
BGREEN = AnsiEscape(sequence_table['bg_green'])
76-
BYELLOW = AnsiEscape(sequence_table['bg_yellow'])
77-
BBLUE = AnsiEscape(sequence_table['bg_blue'])
78-
BMAGENTA = AnsiEscape(sequence_table['bg_magenta'])
79-
BCYAN = AnsiEscape(sequence_table['bg_cyan'])
80-
BWHITE = AnsiEscape(sequence_table['bg_white'])
73+
BBLACK: AnsiEscape = AnsiEscape(sequence_table['bg_black'])
74+
BRED: AnsiEscape = AnsiEscape(sequence_table['bg_red'])
75+
BGREEN: AnsiEscape = AnsiEscape(sequence_table['bg_green'])
76+
BYELLOW: AnsiEscape = AnsiEscape(sequence_table['bg_yellow'])
77+
BBLUE: AnsiEscape = AnsiEscape(sequence_table['bg_blue'])
78+
BMAGENTA: AnsiEscape = AnsiEscape(sequence_table['bg_magenta'])
79+
BCYAN: AnsiEscape = AnsiEscape(sequence_table['bg_cyan'])
80+
BWHITE: AnsiEscape = AnsiEscape(sequence_table['bg_white'])
8181

82-
CLEAR = AnsiEscape(sequence_table['clear'])
82+
CLEAR: AnsiEscape = AnsiEscape(sequence_table['clear'])

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ __init__.py:F401,F403,F405\
1717

1818
[tool.poetry]
1919
name = 'escapyde'
20-
version = '0.1.2'
20+
version = '0.2.0'
2121
description = "Yet another ANSI escape sequence library for Python - now modernised!"
2222

2323
authors = ["Lari Liuhamo <lari.liuhamo+pypi@gmail.com>",]

0 commit comments

Comments
 (0)