Skip to content

Commit 3ee04d9

Browse files
committed
[MINOR] Added the escape_format function
1 parent 254811f commit 3ee04d9

File tree

3 files changed

+75
-5
lines changed

3 files changed

+75
-5
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: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""Main ANSI Escape sequence class"""
22

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

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

77
_CLEAR: str = '\033[0m'
88

@@ -30,3 +30,50 @@ def __or__(self, other: Any) -> 'AnsiEscape':
3030

3131
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)

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)