yet-another-ansi-lib 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 robert-ish
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,40 @@
1
+ Metadata-Version: 2.4
2
+ Name: yet-another-ansi-lib
3
+ Version: 0.1.0
4
+ Summary: Cool ANSI styling!
5
+ Author: robert-ish
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/robert-ish/yet-another-ansi-lib
8
+ Project-URL: Repository, https://github.com/robert-ish/yet-another-ansi-lib
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Operating System :: OS Independent
12
+ Requires-Python: >=3.8
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+ Dynamic: license-file
16
+
17
+ # yet-another-ansi-lib
18
+ Zero-dependency ANSI colors and styles for Python.
19
+
20
+ ## Installation:
21
+ ```bash
22
+ pip install yet-another-ansi-lib
23
+ ```
24
+
25
+ ## Usage:
26
+ ```python
27
+ from yet_another_ansi_lib import ansi
28
+ ansi = ansi()
29
+ print(ansi.red("Hello world!"))
30
+ ```
31
+ you can even do:
32
+ ```python
33
+ from yet_another_ansi_lib import ansi
34
+ ansi = ansi()
35
+ with ansi.styleman('bold', 'red'):
36
+ print('Hello world!')
37
+ ```
38
+
39
+ ## License:
40
+ MIT.
@@ -0,0 +1,27 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.0"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "yet-another-ansi-lib"
7
+ version = "0.1.0"
8
+ description = "Cool ANSI styling!"
9
+ readme = "readme.md"
10
+ authors = [
11
+ {name = "robert-ish"},
12
+ ]
13
+ license = {text = "MIT"}
14
+ classifiers = [
15
+ "Programming Language :: Python :: 3",
16
+ "License :: OSI Approved :: MIT License",
17
+ "Operating System :: OS Independent",
18
+ ]
19
+ requires-python = ">=3.8"
20
+ dependencies = [
21
+ # List your dependencies here, e.g.:
22
+ # "requests>=2.28",
23
+ ]
24
+
25
+ [project.urls]
26
+ Homepage = "https://github.com/robert-ish/yet-another-ansi-lib"
27
+ Repository = "https://github.com/robert-ish/yet-another-ansi-lib"
@@ -0,0 +1,24 @@
1
+ # yet-another-ansi-lib
2
+ Zero-dependency ANSI colors and styles for Python.
3
+
4
+ ## Installation:
5
+ ```bash
6
+ pip install yet-another-ansi-lib
7
+ ```
8
+
9
+ ## Usage:
10
+ ```python
11
+ from yet_another_ansi_lib import ansi
12
+ ansi = ansi()
13
+ print(ansi.red("Hello world!"))
14
+ ```
15
+ you can even do:
16
+ ```python
17
+ from yet_another_ansi_lib import ansi
18
+ ansi = ansi()
19
+ with ansi.styleman('bold', 'red'):
20
+ print('Hello world!')
21
+ ```
22
+
23
+ ## License:
24
+ MIT.
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,303 @@
1
+ from contextlib import contextmanager
2
+ import sys
3
+
4
+ class ansi:
5
+ """
6
+ Provides ANSI escape codes for coloring and styling text in terminal output.
7
+
8
+ All methods return a string with the appropriate escape sequences applied.
9
+ By default, each styled string is automatically terminated with a reset code
10
+ (\\033[0m) unless it already ends with one.
11
+ """
12
+
13
+ ANSI_CODES = {
14
+ # Foreground colors
15
+ 'black': 30,
16
+ 'red': 31,
17
+ 'green': 32,
18
+ 'yellow': 33,
19
+ 'blue': 34,
20
+ 'purple': 35,
21
+ 'cyan': 36,
22
+ 'white': 37,
23
+
24
+ # Text styles
25
+ 'bold': 1,
26
+ 'underline': 4,
27
+ 'italic': 3,
28
+ 'strikethrough': 9,
29
+ 'blink': 5,
30
+ 'reverse': 7,
31
+ 'hidden': 8,
32
+ 'dim': 2,
33
+
34
+ # Background colors
35
+ 'bg_black': 40,
36
+ 'bg_red': 41,
37
+ 'bg_green': 42,
38
+ 'bg_yellow': 43,
39
+ 'bg_blue': 44,
40
+ 'bg_purple': 45,
41
+ 'bg_cyan': 46,
42
+ 'bg_white': 47,
43
+
44
+ # High-intensity foreground
45
+ 'intense_black': 90,
46
+ 'intense_red': 91,
47
+ 'intense_green': 92,
48
+ 'intense_yellow': 93,
49
+ 'intense_blue': 94,
50
+ 'intense_purple': 95,
51
+ 'intense_cyan': 96,
52
+ 'intense_white': 97,
53
+
54
+ # High-intensity background
55
+ 'bg_intense_black': 100,
56
+ 'bg_intense_red': 101,
57
+ 'bg_intense_green': 102,
58
+ 'bg_intense_yellow': 103,
59
+ 'bg_intense_blue': 104,
60
+ 'bg_intense_purple': 105,
61
+ 'bg_intense_cyan': 106,
62
+ 'bg_intense_white': 107,
63
+ }
64
+
65
+ # ---------- Foreground colors ----------
66
+ def black(self, text):
67
+ """Return text colored black (foreground code 30)."""
68
+ return self.apply_codes(text, [30])
69
+
70
+ def red(self, text):
71
+ """Return text colored red (foreground code 31)."""
72
+ return self.apply_codes(text, [31])
73
+
74
+ def green(self, text):
75
+ """Return text colored green (foreground code 32)."""
76
+ return self.apply_codes(text, [32])
77
+
78
+ def yellow(self, text):
79
+ """Return text colored yellow (foreground code 33)."""
80
+ return self.apply_codes(text, [33])
81
+
82
+ def blue(self, text):
83
+ """Return text colored blue (foreground code 34)."""
84
+ return self.apply_codes(text, [34])
85
+
86
+ def purple(self, text):
87
+ """Return text colored purple (foreground code 35)."""
88
+ return self.apply_codes(text, [35])
89
+
90
+ def cyan(self, text):
91
+ """Return text colored cyan (foreground code 36)."""
92
+ return self.apply_codes(text, [36])
93
+
94
+ def white(self, text):
95
+ """Return text colored white (foreground code 37)."""
96
+ return self.apply_codes(text, [37])
97
+
98
+ # ---------- Text styles ----------
99
+ def bold(self, text):
100
+ """Return bold text (code 1)."""
101
+ return self.apply_codes(text, [1])
102
+
103
+ def underline(self, text):
104
+ """Return underlined text (code 4)."""
105
+ return self.apply_codes(text, [4])
106
+
107
+ def italic(self, text):
108
+ """Return italic text (code 3)."""
109
+ return self.apply_codes(text, [3])
110
+
111
+ def strikethrough(self, text):
112
+ """Return text with a strikethrough line (code 9)."""
113
+ return self.apply_codes(text, [9])
114
+
115
+ # ---------- Background colors ----------
116
+ def bg_black(self, text):
117
+ """Return text with black background (code 40)."""
118
+ return self.apply_codes(text, [40])
119
+
120
+ def bg_red(self, text):
121
+ """Return text with red background (code 41)."""
122
+ return self.apply_codes(text, [41])
123
+
124
+ def bg_green(self, text):
125
+ """Return text with green background (code 42)."""
126
+ return self.apply_codes(text, [42])
127
+
128
+ def bg_yellow(self, text):
129
+ """Return text with yellow background (code 43)."""
130
+ return self.apply_codes(text, [43])
131
+
132
+ def bg_blue(self, text):
133
+ """Return text with blue background (code 44)."""
134
+ return self.apply_codes(text, [44])
135
+
136
+ def bg_purple(self, text):
137
+ """Return text with purple background (code 45)."""
138
+ return self.apply_codes(text, [45])
139
+
140
+ def bg_cyan(self, text):
141
+ """Return text with cyan background (code 46)."""
142
+ return self.apply_codes(text, [46])
143
+
144
+ def bg_white(self, text):
145
+ """Return text with white background (code 47)."""
146
+ return self.apply_codes(text, [47])
147
+
148
+ # ---------- High-intensity foreground colors ----------
149
+ def intense_black(self, text):
150
+ """Return text in high‑intensity black (code 90)."""
151
+ return self.apply_codes(text, [90])
152
+
153
+ def intense_red(self, text):
154
+ """Return text in high‑intensity red (code 91)."""
155
+ return self.apply_codes(text, [91])
156
+
157
+ def intense_green(self, text):
158
+ """Return text in high‑intensity green (code 92)."""
159
+ return self.apply_codes(text, [92])
160
+
161
+ def intense_yellow(self, text):
162
+ """Return text in high‑intensity yellow (code 93)."""
163
+ return self.apply_codes(text, [93])
164
+
165
+ def intense_blue(self, text):
166
+ """Return text in high‑intensity blue (code 94)."""
167
+ return self.apply_codes(text, [94])
168
+
169
+ def intense_purple(self, text):
170
+ """Return text in high‑intensity purple (code 95)."""
171
+ return self.apply_codes(text, [95])
172
+
173
+ def intense_cyan(self, text):
174
+ """Return text in high‑intensity cyan (code 96)."""
175
+ return self.apply_codes(text, [96])
176
+
177
+ def intense_white(self, text):
178
+ """Return text in high‑intensity white (code 97)."""
179
+ return self.apply_codes(text, [97])
180
+
181
+ # ---------- High-intensity background colors ----------
182
+ def bg_intense_black(self, text):
183
+ """Return text with high‑intensity black background (code 100)."""
184
+ return self.apply_codes(text, [100])
185
+
186
+ def bg_intense_red(self, text):
187
+ """Return text with high‑intensity red background (code 101)."""
188
+ return self.apply_codes(text, [101])
189
+
190
+ def bg_intense_green(self, text):
191
+ """Return text with high‑intensity green background (code 102)."""
192
+ return self.apply_codes(text, [102])
193
+
194
+ def bg_intense_yellow(self, text):
195
+ """Return text with high‑intensity yellow background (code 103)."""
196
+ return self.apply_codes(text, [103])
197
+
198
+ def bg_intense_blue(self, text):
199
+ """Return text with high‑intensity blue background (code 104)."""
200
+ return self.apply_codes(text, [104])
201
+
202
+ def bg_intense_purple(self, text):
203
+ """Return text with high‑intensity purple background (code 105)."""
204
+ return self.apply_codes(text, [105])
205
+
206
+ def bg_intense_cyan(self, text):
207
+ """Return text with high‑intensity cyan background (code 106)."""
208
+ return self.apply_codes(text, [106])
209
+
210
+ def bg_intense_white(self, text):
211
+ """Return text with high‑intensity white background (code 107)."""
212
+ return self.apply_codes(text, [107])
213
+
214
+ # ---------- Other effects ----------
215
+ def blink(self, text):
216
+ """Return blinking text (code 5)."""
217
+ return self.apply_codes(text, [5])
218
+
219
+ def reverse(self, text):
220
+ """Return text with foreground and background colors swapped (code 7)."""
221
+ return self.apply_codes(text, [7])
222
+
223
+ def hidden(self, text):
224
+ """Return invisible/hidden text (code 8)."""
225
+ return self.apply_codes(text, [8])
226
+
227
+ def dim(self, text):
228
+ """Return text with reduced intensity (low intensity)."""
229
+ return self.apply_codes(text, [2])
230
+
231
+ # ---------- Truecolor ----------
232
+
233
+ def rgb(self, r, g, b, text):
234
+ """Truecolor foreground using 24-bit RGB (semicolon syntax)."""
235
+ return self.apply_codes(text, [38, 2, r, g, b])
236
+
237
+ def bg_rgb(self, r, g, b, text):
238
+ """Truecolor background using 24-bit RGB."""
239
+ return self.apply_codes(text, [48, 2, r, g, b])
240
+
241
+ def hex_color(self, hex_code, text):
242
+ """Convert #RRGGBB to truecolor foreground."""
243
+ hex_code = hex_code.lstrip('#')
244
+ r, g, b = int(hex_code[0:2], 16), int(hex_code[2:4], 16), int(hex_code[4:6], 16)
245
+ return self.rgb(r, g, b, text)
246
+
247
+ # ---------- Core formatting engine ----------
248
+ def apply_codes(self, text: str, codes):
249
+ """
250
+ Apply one or more ANSI escape codes to the given text.
251
+
252
+ Args:
253
+ text (str): The text to style.
254
+ codes (list[int]): List of ANSI codes (e.g., [31, 1] for red and bold).
255
+
256
+ Returns:
257
+ str: The text prefixed with the appropriate escape sequence and,
258
+ unless the original text already ends with a reset code,
259
+ suffixed with \\033[0m to reset all styles.
260
+ """
261
+ if text.endswith('\033[0m'):
262
+ sequence = '\033[' + ';'.join(map(str, codes)) + 'm' + text
263
+ else:
264
+ sequence = '\033[' + ';'.join(map(str, codes)) + 'm' + text + '\033[0m'
265
+ return sequence
266
+
267
+ def style(self, text, *styles):
268
+ """
269
+ Apply multiple styles to the given text by chaining methods.
270
+
271
+ Args:
272
+ text (str): The text to style.
273
+ *styles (str): Styles to apply to text.
274
+ all names of styles are the same as names of functions in this class.
275
+
276
+ Returns:
277
+ str: The text after applying all specified styles sequentially.
278
+
279
+ Example:
280
+ ansi = basic_ansi()
281
+ ansi.style("Hello", "red", "bold", "underline")
282
+ """
283
+ a = []
284
+ for i in styles:
285
+ a.append(self.ANSI_CODES[i])
286
+ text = self.apply_codes(text, a)
287
+ return text
288
+
289
+ @contextmanager
290
+ def styleman(self, *funcs):
291
+ """Context manager for applying styles to a block."""
292
+
293
+ codes = [str(self.ANSI_CODES[f]) for f in funcs if f in self.ANSI_CODES]
294
+ sys.stdout.write('\033[' + ';'.join(codes) + 'm')
295
+ sys.stdout.flush()
296
+
297
+ try:
298
+ yield
299
+ finally:
300
+ sys.stdout.write('\033[0m')
301
+ sys.stdout.flush()
302
+
303
+ __all__ = ['ansi']
@@ -0,0 +1,40 @@
1
+ Metadata-Version: 2.4
2
+ Name: yet-another-ansi-lib
3
+ Version: 0.1.0
4
+ Summary: Cool ANSI styling!
5
+ Author: robert-ish
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/robert-ish/yet-another-ansi-lib
8
+ Project-URL: Repository, https://github.com/robert-ish/yet-another-ansi-lib
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Operating System :: OS Independent
12
+ Requires-Python: >=3.8
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+ Dynamic: license-file
16
+
17
+ # yet-another-ansi-lib
18
+ Zero-dependency ANSI colors and styles for Python.
19
+
20
+ ## Installation:
21
+ ```bash
22
+ pip install yet-another-ansi-lib
23
+ ```
24
+
25
+ ## Usage:
26
+ ```python
27
+ from yet_another_ansi_lib import ansi
28
+ ansi = ansi()
29
+ print(ansi.red("Hello world!"))
30
+ ```
31
+ you can even do:
32
+ ```python
33
+ from yet_another_ansi_lib import ansi
34
+ ansi = ansi()
35
+ with ansi.styleman('bold', 'red'):
36
+ print('Hello world!')
37
+ ```
38
+
39
+ ## License:
40
+ MIT.
@@ -0,0 +1,8 @@
1
+ LICENSE
2
+ pyproject.toml
3
+ readme.md
4
+ yet_another_ansi_lib/__init__.py
5
+ yet_another_ansi_lib.egg-info/PKG-INFO
6
+ yet_another_ansi_lib.egg-info/SOURCES.txt
7
+ yet_another_ansi_lib.egg-info/dependency_links.txt
8
+ yet_another_ansi_lib.egg-info/top_level.txt
@@ -0,0 +1 @@
1
+ yet_another_ansi_lib