escansi 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 @@
1
+ **/__pycache__
escansi-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ricardo JRSM Vieira
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.
escansi-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,22 @@
1
+ Metadata-Version: 2.4
2
+ Name: escansi
3
+ Version: 0.1.0
4
+ Summary: Python library to easily create ANSI escape codes and add them to your text.
5
+ Project-URL: Homepage, https://github.com/ricardojrsmvieira/escansi
6
+ Project-URL: Issues, https://github.com/ricardojrsmvieira/escansi/issues
7
+ Author-email: Ricardo JRSM Vieira <ricardojrsmvieira@gmail.com>
8
+ Maintainer-email: Ricardo JRSM Vieira <ricardojrsmvieira@gmail.com>
9
+ License-Expression: MIT
10
+ License-File: LICENSE
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Operating System :: OS Independent
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Topic :: Printing
16
+ Classifier: Topic :: Terminals
17
+ Classifier: Typing :: Typed
18
+ Requires-Python: >=3.0
19
+ Description-Content-Type: text/markdown
20
+
21
+ # escansi
22
+ Python library to easily create ANSI escape codes and add them to your text.
@@ -0,0 +1,2 @@
1
+ # escansi
2
+ Python library to easily create ANSI escape codes and add them to your text.
@@ -0,0 +1,31 @@
1
+ [build-system]
2
+ requires = ["hatchling >= 1.26"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "escansi"
7
+ version = "0.1.0"
8
+ authors = [
9
+ { name = "Ricardo JRSM Vieira", email = "ricardojrsmvieira@gmail.com" }
10
+ ]
11
+ maintainers = [
12
+ { name = "Ricardo JRSM Vieira", email = "ricardojrsmvieira@gmail.com" }
13
+ ]
14
+ description = "Python library to easily create ANSI escape codes and add them to your text."
15
+ readme = "README.md"
16
+ requires-python = ">=3.0"
17
+ classifiers = [
18
+ "Development Status :: 3 - Alpha",
19
+ "Intended Audience :: Developers",
20
+ "Operating System :: OS Independent",
21
+ "Programming Language :: Python :: 3",
22
+ "Topic :: Printing",
23
+ "Topic :: Terminals",
24
+ "Typing :: Typed"
25
+ ]
26
+ license = "MIT"
27
+ license-files = ["LICEN[CS]E*"]
28
+
29
+ [project.urls]
30
+ Homepage = "https://github.com/ricardojrsmvieira/escansi"
31
+ Issues = "https://github.com/ricardojrsmvieira/escansi/issues"
@@ -0,0 +1,14 @@
1
+ from .utils import (
2
+ BackColor as BackColor,
3
+ Clear as Clear,
4
+ Cursor as Cursor,
5
+ ForeColor as ForeColor,
6
+ Format as Format,
7
+ ResetFormat as ResetFormat
8
+ )
9
+ from .types import (
10
+ ColorNameType as ColorNameType,
11
+ ClearActionNameType as ClearActionNameType,
12
+ CursorActionNameType as CursorActionNameType,
13
+ FormatNameType as FormatNameType
14
+ )
@@ -0,0 +1,116 @@
1
+ from typing import Literal, get_args
2
+
3
+ from .types import (
4
+ ClearActionNameType,
5
+ ColorNameType,
6
+ CursorActionNameType,
7
+ CursorActionsThatHaveNoNumberParametersNameType,
8
+ CursorActionsThatHaveOneNumberParameterNameType,
9
+ CursorActionsThatHaveTwoNumberParametersNameType,
10
+ FormatNameType
11
+ )
12
+
13
+
14
+ COLOR_CODES: dict[Literal["foreground", "background"], dict[ColorNameType, int]] = {
15
+ "foreground": {
16
+ "black": 30,
17
+ "red": 31,
18
+ "green": 32,
19
+ "yellow": 33,
20
+ "blue": 34,
21
+ "magenta": 35,
22
+ "cyan": 36,
23
+ "white": 37,
24
+ "default": 39,
25
+ "bright_black": 90,
26
+ "bright_red": 91,
27
+ "bright_green": 92,
28
+ "bright_yellow": 93,
29
+ "bright_blue": 94,
30
+ "bright_magenta": 95,
31
+ "bright_cyan": 96,
32
+ "bright_white": 97
33
+ },
34
+ "background": {
35
+ "black": 40,
36
+ "red": 41,
37
+ "green": 42,
38
+ "yellow": 43,
39
+ "blue": 44,
40
+ "magenta": 45,
41
+ "cyan": 46,
42
+ "white": 47,
43
+ "default": 49,
44
+ "bright_black": 100,
45
+ "bright_red": 101,
46
+ "bright_green": 102,
47
+ "bright_yellow": 103,
48
+ "bright_blue": 104,
49
+ "bright_magenta": 105,
50
+ "bright_cyan": 106,
51
+ "bright_white": 107
52
+ }
53
+ }
54
+
55
+
56
+ FORMAT_CODES: dict[Literal["apply", "reset"], dict[FormatNameType, int]] = {
57
+ "apply": {
58
+ "bold": 1,
59
+ "dim": 2,
60
+ "italic": 3,
61
+ "underline": 4,
62
+ "blink": 5,
63
+ "swap_colors": 7,
64
+ "hidden": 8,
65
+ "strikethrough": 9
66
+ },
67
+ "reset": {
68
+ "bold": 21,
69
+ "dim": 22,
70
+ "italic": 23,
71
+ "underline": 24,
72
+ "blink": 25,
73
+ "swap_colors": 27,
74
+ "hidden": 28,
75
+ "strikethrough": 29
76
+ }
77
+ }
78
+
79
+
80
+ CURSOR_CODES: dict[CursorActionNameType, str] = {
81
+ "up": "A", "u": "A",
82
+ "down": "B", "d": "B",
83
+ "forward": "C", "f": "C",
84
+ "backward": "D", "b": "D",
85
+ "start_of_line_down": "E", "sold": "E",
86
+ "start_of_line_up": "F", "solu": "F",
87
+ "move_to_column": "G", "mtc": "G",
88
+ "move_to_position": "H", "mtp": "H",
89
+ "save_position": "s", "sp": "s",
90
+ "restore_position": "u", "rp": "u",
91
+ "hide": "?25l", "h": "?25l",
92
+ "show": "?25h", "s": "?25h"
93
+ }
94
+
95
+
96
+ CLEAR_CODES: dict[ClearActionNameType, str] = {
97
+ "all": "2J", "a": "2J",
98
+ "begin_to_cursor": "1J", "btc": "1J",
99
+ "cursor_to_end": "0J", "cte": "0J",
100
+ "line": "2K", "l": "2K",
101
+ "line_start_to_cursor": "1K", "lstc": "1K",
102
+ "cursor_to_line_end": "0K", "ctle": "0K"
103
+ }
104
+
105
+
106
+ COLOR_NAMES: tuple[ColorNameType, ...] = get_args(ColorNameType.__value__)
107
+
108
+ CURSOR_ACTIONS_THAT_HAVE_ONE_NUMBER_PARAMETER: tuple[CursorActionsThatHaveOneNumberParameterNameType, ...] = (
109
+ get_args(CursorActionsThatHaveOneNumberParameterNameType.__value__)
110
+ )
111
+ CURSOR_ACTIONS_THAT_HAVE_TWO_NUMBER_PARAMETERS: tuple[CursorActionsThatHaveTwoNumberParametersNameType, ...] = (
112
+ get_args(CursorActionsThatHaveTwoNumberParametersNameType.__value__)
113
+ )
114
+ CURSOR_ACTIONS_THAT_HAVE_NO_NUMBER_PARAMETERS: tuple[CursorActionsThatHaveNoNumberParametersNameType, ...] = (
115
+ get_args(CursorActionsThatHaveNoNumberParametersNameType.__value__)
116
+ )
@@ -0,0 +1,88 @@
1
+ from typing import Literal
2
+
3
+
4
+ type ColorNameType = Literal[
5
+ "black",
6
+ "red",
7
+ "green",
8
+ "yellow",
9
+ "blue",
10
+ "magenta",
11
+ "cyan",
12
+ "white",
13
+ "default",
14
+ "bright_black",
15
+ "bright_red",
16
+ "bright_green",
17
+ "bright_yellow",
18
+ "bright_blue",
19
+ "bright_magenta",
20
+ "bright_cyan",
21
+ "bright_white"
22
+ ]
23
+
24
+
25
+ type FormatNameType = Literal[
26
+ "bold",
27
+ "dim",
28
+ "italic",
29
+ "underline",
30
+ "blink",
31
+ "swap_colors",
32
+ "hidden",
33
+ "strikethrough"
34
+ ]
35
+
36
+
37
+ # THIS TYPES ARE RELATED ->
38
+ # If need to add/remove to the first one (main), consider where else it should be added/removed to keep the consistency.
39
+ type CursorActionNameType = Literal[
40
+ # CursorActionsThatHaveOneNumberParameterNameType
41
+ "up", "u",
42
+ "down", "d",
43
+ "forward", "f",
44
+ "backward", "b",
45
+ "start_of_line_down", "sold",
46
+ "start_of_line_up", "solu",
47
+ "move_to_column", "mtc",
48
+ # CursorActionsThatHaveTwoNumberParametersNameType
49
+ "move_to_position", "mtp",
50
+ # CursorActionsThatHaveNoNumberParametersNameType
51
+ "save_position", "sp",
52
+ "restore_position", "rp",
53
+ "hide", "h",
54
+ "show", "s"
55
+ ]
56
+
57
+ type CursorActionsThatHaveOneNumberParameterNameType = Literal[
58
+ "up", "u",
59
+ "down", "d",
60
+ "forward", "f",
61
+ "backward", "b",
62
+ "start_of_line_down", "sold",
63
+ "start_of_line_up", "solu",
64
+ "move_to_column", "mtc"
65
+ ]
66
+
67
+ type CursorActionsThatHaveTwoNumberParametersNameType = Literal[
68
+ "move_to_position", "mtp"
69
+ ]
70
+
71
+ type CursorActionsThatHaveNoNumberParametersNameType = Literal[
72
+ "save_position", "sp",
73
+ "restore_position", "rp",
74
+ "hide", "h",
75
+ "show", "s"
76
+ ]
77
+ # If need to add/remove to the first one (main), consider where else it should be added/removed to keep the consistency.
78
+ # <- THIS TYPES ARE RELATED
79
+
80
+
81
+ type ClearActionNameType = Literal[
82
+ "all", "a",
83
+ "begin_to_cursor", "btc",
84
+ "cursor_to_end", "cte",
85
+ "line", "l",
86
+ "line_start_to_cursor", "lstc",
87
+ "cursor_to_line_end", "ctle"
88
+ ]
@@ -0,0 +1,170 @@
1
+ from typing import Literal, overload
2
+
3
+ from .types import (
4
+ ClearActionNameType,
5
+ CursorActionNameType,
6
+ CursorActionsThatHaveNoNumberParametersNameType,
7
+ CursorActionsThatHaveOneNumberParameterNameType,
8
+ CursorActionsThatHaveTwoNumberParametersNameType,
9
+ FormatNameType,
10
+ )
11
+ from .constants import (
12
+ CLEAR_CODES,
13
+ COLOR_CODES,
14
+ COLOR_NAMES,
15
+ CURSOR_ACTIONS_THAT_HAVE_NO_NUMBER_PARAMETERS,
16
+ CURSOR_ACTIONS_THAT_HAVE_ONE_NUMBER_PARAMETER,
17
+ CURSOR_ACTIONS_THAT_HAVE_TWO_NUMBER_PARAMETERS,
18
+ CURSOR_CODES,
19
+ FORMAT_CODES,
20
+ )
21
+
22
+
23
+ #region overloads
24
+ @overload
25
+ def ForeColor(
26
+ color_or_grey_or_red: str | int | tuple[int, int, int],
27
+ green: None = None,
28
+ blue: None = None,
29
+ /
30
+ ) -> str: ...
31
+ @overload
32
+ def ForeColor(
33
+ color_or_grey_or_red: int,
34
+ green: int,
35
+ blue: int,
36
+ /
37
+ ) -> str: ...
38
+ #endregion overloads
39
+ def ForeColor(
40
+ color_or_grey_or_red: str | int | tuple[int, int, int],
41
+ green: int | None = None,
42
+ blue: int | None = None,
43
+ /
44
+ ) -> str:
45
+ if isinstance(color_or_grey_or_red, str):
46
+ if color_or_grey_or_red in COLOR_NAMES:
47
+ return f"\033[{COLOR_CODES['foreground'][color_or_grey_or_red]}m"
48
+ else:
49
+ if color_or_grey_or_red.startswith("#") and len(color_or_grey_or_red) == 7:
50
+ try:
51
+ r = int(color_or_grey_or_red[1:3], 16)
52
+ g = int(color_or_grey_or_red[3:5], 16)
53
+ b = int(color_or_grey_or_red[5:7], 16)
54
+ return f"\033[38;2;{r};{g};{b}m"
55
+ except ValueError:
56
+ raise ValueError(f"Invalid color code or name: '{color_or_grey_or_red}'")
57
+ else:
58
+ raise ValueError(f"Invalid color code or name: '{color_or_grey_or_red}'")
59
+ elif green is None or blue is None:
60
+ if isinstance(color_or_grey_or_red, tuple):
61
+ r, g, b = color_or_grey_or_red
62
+ return f"\033[38;2;{r};{g};{b}m"
63
+ else:
64
+ return f"\033[38;5;{color_or_grey_or_red}m"
65
+ else:
66
+ return f"\033[38;2;{color_or_grey_or_red};{green};{blue}m"
67
+
68
+
69
+ #region overloads
70
+ @overload
71
+ def BackColor(
72
+ color_or_grey_or_red: str | int | tuple[int, int, int],
73
+ green: None = None,
74
+ blue: None = None,
75
+ /
76
+ ) -> str: ...
77
+ @overload
78
+ def BackColor(
79
+ color_or_grey_or_red: int,
80
+ green: int,
81
+ blue: int,
82
+ /
83
+ ) -> str: ...
84
+ #endregion overloads
85
+ def BackColor(
86
+ color_or_grey_or_red: str | int | tuple[int, int, int],
87
+ green: int | None = None,
88
+ blue: int | None = None,
89
+ /
90
+ ) -> str:
91
+ if isinstance(color_or_grey_or_red, str):
92
+ if color_or_grey_or_red in COLOR_NAMES:
93
+ return f"\033[{COLOR_CODES['background'][color_or_grey_or_red]}m"
94
+ else:
95
+ if color_or_grey_or_red.startswith("#") and len(color_or_grey_or_red) == 7:
96
+ try:
97
+ r = int(color_or_grey_or_red[1:3], 16)
98
+ g = int(color_or_grey_or_red[3:5], 16)
99
+ b = int(color_or_grey_or_red[5:7], 16)
100
+ return f"\033[48;2;{r};{g};{b}m"
101
+ except ValueError:
102
+ raise ValueError(f"Invalid color code or name: '{color_or_grey_or_red}'")
103
+ else:
104
+ raise ValueError(f"Invalid color code or name: '{color_or_grey_or_red}'")
105
+ elif green is None or blue is None:
106
+ if isinstance(color_or_grey_or_red, tuple):
107
+ r, g, b = color_or_grey_or_red
108
+ return f"\033[48;2;{r};{g};{b}m"
109
+ else:
110
+ return f"\033[48;5;{color_or_grey_or_red}m"
111
+ else:
112
+ return f"\033[48;2;{color_or_grey_or_red};{green};{blue}m"
113
+
114
+
115
+ def Format(format: FormatNameType | tuple[FormatNameType, ...]) -> str:
116
+ if isinstance(format, tuple):
117
+ return "".join(f"\033[{FORMAT_CODES['apply'][f]}m" for f in format)
118
+ return f"\033[{FORMAT_CODES['apply'][format]}m"
119
+
120
+
121
+ def ResetFormat(format: FormatNameType | tuple[FormatNameType, ...] | Literal["all"] = "all") -> str:
122
+ if format == "all":
123
+ return "\033[0m"
124
+ elif isinstance(format, tuple):
125
+ return "".join(f"\033[{FORMAT_CODES['reset'][f]}m" for f in format)
126
+ else:
127
+ return f"\033[{FORMAT_CODES['reset'][format]}m"
128
+
129
+
130
+ #region overloads
131
+ @overload
132
+ def Cursor(
133
+ action: CursorActionsThatHaveNoNumberParametersNameType,
134
+ n: None = None,
135
+ m: None = None,
136
+ /
137
+ ) -> str: ...
138
+ @overload
139
+ def Cursor(
140
+ action: CursorActionsThatHaveOneNumberParameterNameType,
141
+ n: int,
142
+ m: None = None,
143
+ /
144
+ ) -> str: ...
145
+ @overload
146
+ def Cursor(
147
+ action: CursorActionsThatHaveTwoNumberParametersNameType,
148
+ n: int,
149
+ m: int,
150
+ /
151
+ ) -> str: ...
152
+ #endregion overloads
153
+ def Cursor(
154
+ action: CursorActionNameType,
155
+ n: int | None = None,
156
+ m: int | None = None,
157
+ /
158
+ ) -> str:
159
+ if action in CURSOR_ACTIONS_THAT_HAVE_ONE_NUMBER_PARAMETER:
160
+ return f"\033[{n}{CURSOR_CODES[action]}"
161
+ elif action in CURSOR_ACTIONS_THAT_HAVE_TWO_NUMBER_PARAMETERS:
162
+ return f"\033[{n};{m}{CURSOR_CODES[action]}"
163
+ elif action in CURSOR_ACTIONS_THAT_HAVE_NO_NUMBER_PARAMETERS:
164
+ return f"\033[{CURSOR_CODES[action]}"
165
+ else:
166
+ raise ValueError(f"Invalid cursor action: '{action}'")
167
+
168
+
169
+ def Clear(action: ClearActionNameType = "all") -> str:
170
+ return f"\033[{CLEAR_CODES[action]}"
File without changes