Luci-Code-Tools 1.2.3__tar.gz → 2.0.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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: Luci-Code-Tools
3
- Version: 1.2.3
3
+ Version: 2.0.0
4
4
  Summary: 安全输入工具库,支持彩色输出,KeyboardInterrupt保护等
5
5
  Home-page: https://github.com/Luci-Codeing/code_tools
6
6
  Author: Luci_Code
@@ -52,7 +52,6 @@ import Luci_code_tools
52
52
  | color_rgb | 输出RGB彩色字符 | data, r, g, b, mode, newline |
53
53
  | clear_screen | 可跨平台的清屏 | None (没有参数) |
54
54
  | safe_* | 安全的输入, 防止Ctrl + C和输入流关闭等报错 | prompt, text_color, bg_color, mode |
55
- | cloudflare_play | 输出 CloudFlare 人机验证 | None (没有参数) |
56
55
 
57
56
  **提示**: ***参数需要按照顺序填***
58
57
 
@@ -125,12 +124,15 @@ clear_screen()
125
124
  **clear_screen源码**
126
125
 
127
126
  ```python
128
- def clear_screen() -> None:
127
+ def clear_screen() -> None: # 清屏: 兼容版
129
128
  """ 跨平台兼容清屏 """
130
- if platform == "win32":
131
- run(['cmd', '/c', 'cls'])
132
- else:
133
- run(['clear'])
129
+ try: # 检测环境
130
+ if platform == "win32": # 环境为 Windows
131
+ run(['cmd', '/c', 'cls']) # 安全: 使用列表命令, 防注入
132
+ else: # 包括 MacOS, Linux (Android 也是 Linux 列)
133
+ run(['clear'], check=False) # check = 遇到错误我自己来, 别报错
134
+ except Exception: # 非交互式环境 (.py 打包.exe类环境等没有cmd命令的环境)
135
+ print("\n" * 200) # 手动清屏
134
136
  ```
135
137
 
136
138
  **clear_screen** 可以直接使用, 无需参数, 自动适配平台并运行相应的 *清屏命令*
@@ -167,16 +169,6 @@ safe_input, safe_int, safe_float
167
169
 
168
170
  ---
169
171
 
170
- - cloudflare_play
171
-
172
- 这个函数用着玩的, 无参数, 运行输出:
173
- ┏━━━━━━━━━━━━━━┓
174
- ┃ ┌┐ 请验证您是真人 ☁️ ┃
175
- ┃ └┘ cloudflare ┃
176
- ┗━━━━━━━━━━━━━━┛
177
-
178
- ---
179
-
180
172
  # 其他
181
173
 
182
174
  **ANSI常用序列**
@@ -0,0 +1,249 @@
1
+ from sys import platform, exit
2
+ from subprocess import run
3
+ from typing import Optional, Union
4
+
5
+ __version__ = "2.0.0" # 明确版本号
6
+ __all__ = [
7
+ 'color', 'color_rgb', 'clear_screen',
8
+ 'safe_input', 'safe_int', 'safe_float'
9
+ ] # 确定导出后的代码可用的函数
10
+
11
+ class _AnsiCode: # AnsiCode 类
12
+ ESC = "\033"
13
+ RESET = f"{ESC}[0m"
14
+ @staticmethod # 我忘了这什么意思🤪 (并非忘了😂)
15
+ def build(mode: int = 0, fg: Optional[int] = None, bg: Optional[int] = None) -> str:
16
+ parts = []
17
+ if mode != 0:
18
+ parts.append(str(mode))
19
+ if fg is not None:
20
+ parts.append(str(fg))
21
+ if bg is not None:
22
+ parts.append(str(bg))
23
+ if parts:
24
+ return f"{_AnsiCode.ESC}[{';'.join(parts)}m"
25
+ return ""
26
+
27
+ def _ansi_return(data: str, text_color: Optional[int] = None, bg_color: Optional[int] = None, mode: int = 0) -> str: # 6767676767 !
28
+ """ 生成ANSI彩色文本(不自动输出)
29
+
30
+ 参数:
31
+ data: 要着色的文本内容
32
+ text_color: 前景色代码 (30-37普通色, 90-97亮色)
33
+ bg_color: 背景色代码 (40-47普通色, 100-107亮色)
34
+ mode: 显示方式 (0=默认,1=粗体,2=暗淡,3=斜体,4=下划线,5=闪烁,7=反色,8=隐藏,9=删除线)
35
+
36
+ 返回:
37
+ 带ANSI转义序列的字符串
38
+ """
39
+
40
+ if text_color is None and bg_color is None: # 如果 text_color 和 bg_color 不是 None
41
+ return data
42
+
43
+ ansi = _AnsiCode.build(mode, text_color, bg_color)
44
+ return f"{ansi}{data}{_AnsiCode.RESET}"
45
+
46
+ def _ansi_rgb(data: str, r: int, g: int, b: int, mode: int = 0) -> str: # 这啥意思
47
+ """ 内部函数 """
48
+ if mode == 0:
49
+ return f"{_AnsiCode.ESC}[38;2;{r};{g};{b}m{data}{_AnsiCode.RESET}"
50
+ else:
51
+ return f"{_AnsiCode.ESC}[{mode};38;2;{r};{g};{b}m{data}{_AnsiCode.RESET}"
52
+
53
+ def color_rgb(data: str, r: int, g: int, b: int, mode: int = 0, newline: bool = True) -> None: # 用户输出 RGB 彩色字符
54
+ """ 输出 rgb 字符 """
55
+ if newline:
56
+ end="\n\n"
57
+ else:
58
+ end="\n"
59
+ print(_ansi_rgb(data, r, g, b, mode), end=end)
60
+
61
+ def color(data: str, text_color: Optional[int] = None, bg_color: Optional[int] = None, mode: int = 0, newline: bool =True) -> None: # 🤔
62
+ """ 输出彩色字符到控制台
63
+
64
+ 参数:
65
+ data: 要输出的文本内容
66
+ text_color: 前景色代码 (30-37普通色, 90-97亮色)
67
+ bg_color: 背景色代码 (40-47普通色, 100-107亮色)
68
+ mode: 显示方式 (0=默认,1=粗体,2=暗淡,3=斜体,4=下划线,5=闪烁,7=反色,8=隐藏,9=删除线)
69
+ newline: 是否在输出后添加空行,默认True
70
+
71
+ 示例:
72
+ color("错误信息", text_color=31, mode=1) # 红色加粗
73
+ color("警告", text_color=33, bg_color=41) # 黄字红底
74
+
75
+ newline 参数说明
76
+ 普通输出:
77
+ 1
78
+ 2
79
+ 3
80
+ 1-1
81
+ 2-1
82
+ Input: Luci_Code
83
+ newline 输出:
84
+ 1
85
+ 2
86
+ 3
87
+
88
+ 1-1
89
+ 2-1
90
+
91
+ Input: Luci_Code
92
+
93
+ newline=True:
94
+ a
95
+
96
+ a
97
+
98
+ newline=False:
99
+ a
100
+ a
101
+ """
102
+ if newline:
103
+ end="\n\n"
104
+ else:
105
+ end="\n"
106
+ print(_ansi_return(data, text_color, bg_color, mode), end=end)
107
+
108
+ def clear_screen() -> None: # 清屏: 兼容版
109
+ """ 跨平台兼容清屏 """
110
+ try: # 检测环境
111
+ if platform == "win32": # 环境为 Windows
112
+ run(['cmd', '/c', 'cls']) # 安全: 使用列表命令, 防注入
113
+ else: # 包括 MacOS, Linux (Android 也是 Linux 列)
114
+ run(['clear'], check=False) # check = 遇到错误我自己来, 别报错
115
+ except Exception: # 非交互式环境 (.py 打包.exe类环境等没有cmd命令的环境)
116
+ print("\n" * 200) # 手动清屏
117
+
118
+ def safe_try_except(prompt, input_type = True, convert_func=None, text_color=None, bg_color=None, mode=0) -> Union[str, int, float]: # True = input 模式 : False = int / float 模式
119
+ if text_color is not None or bg_color is not None:
120
+ prompt = _ansi_return(prompt, text_color, bg_color, mode)
121
+ while True:
122
+ try:
123
+ if input_type:
124
+ return input(prompt)
125
+ else:
126
+ return convert_func(input(prompt))
127
+ except MemoryError:
128
+ color("OUT OF MEMORY !\n\nInsufficient memory to complete the operation.\n\nTry:\n\n· Close other apps and retry\n· Increase RAM or swap space\n· Restart the system if problem persists\nOperation terminated. (Error 0x0000005E)\n\n内存不足!\n\n内存不足,无法完成操作。\n\n请尝试:\n\n· 关闭其他应用后重试\n· 增加内存或交换空间\n· 如果问题仍然存在,请重启系统\n操作已终止 (错误代码 0x0000005E)", 37, 44, 1)
129
+ exit(1)
130
+ except ValueError:
131
+ print("Invalid input value type\n无效的输入值类型")
132
+ continue
133
+ except KeyboardInterrupt:
134
+ clear_screen()
135
+ color("Program interrupted\n程序被中断", 33, None, 1)
136
+ exit(0)
137
+ except EOFError:
138
+ clear_screen()
139
+ color("Input stream terminated\n输入流已终止", 33, None, 1)
140
+ exit(0)
141
+ except TimeoutError:
142
+ color("Input timeout\n输入超时", 33, None, 1)
143
+ exit(0)
144
+ except UnicodeDecodeError:
145
+ color("Unicode encoding error\nUnicode 编码错误", 33, None, 1)
146
+ continue
147
+ except OSError:
148
+ color("OSError: System operation failed. Please check file path, permissions, or disk space.\nOSError:系统操作失败。请检查文件路径、权限或磁盘空间。", 37, 44, 1)
149
+ exit(1)
150
+ except OverflowError:
151
+ color("Value overflow\n数值溢出", 33, None, 1)
152
+ continue
153
+ except TypeError:
154
+ color("Type conversion failed\n类型转换失败", 33, None, 1)
155
+ continue
156
+ except RuntimeError as e:
157
+ color(f"Serious runtime error\nError message:\n{e}\n严重的运行时错误\n错误信息,\n{e}", 33, None, 1)
158
+ exit(1)
159
+ except SystemExit:
160
+ color("System exit\n系统退出", 33, None, 1)
161
+ exit(0)
162
+ except SystemError:
163
+ color("System error, please check Python interpreter\n系统错误,请检查 Python 解释器", 33, None, 1)
164
+ exit(0)
165
+ except AttributeError:
166
+ color("Object has no such attribute\n对象没有此属性", 33, None, 1)
167
+ continue
168
+ except NameError:
169
+ color("Variable or function not defined\n变量或函数未定义", 33, None, 1)
170
+ exit(1)
171
+ except SyntaxError:
172
+ color("Syntax error in code\n代码中的语法错误", 33, None, 1)
173
+ continue
174
+ except RecursionError:
175
+ color("Recursion depth exceeded\n超出递归深度", 33, None, 1)
176
+ exit(1)
177
+ except BrokenPipeError:
178
+ color("Input pipe closed by other end\n输入管道被另一端关闭", 33, None, 1)
179
+ exit(1)
180
+ except ConnectionError:
181
+ color("Input from network stream and connection lost\n来自网络流的输入,连接已丢失", 33, None, 1)
182
+ exit(1)
183
+ except FileNotFoundError:
184
+ color("Input source file not found\n未找到输入源文件", 33, None, 1)
185
+ exit(1)
186
+ except IsADirectoryError:
187
+ color("Input source is a directory, not a file\n输入源是一个目录,而不是文件", 33, None, 1)
188
+ exit(1)
189
+ except PermissionError:
190
+ color("Permission denied to read input source\n权限被拒绝,无法读取输入源", 33, None, 1)
191
+ exit(1)
192
+ except BlockingIOError:
193
+ color("No data to read in non-blocking mode\n非阻塞模式下没有数据可读", 33, None, 1)
194
+ continue
195
+ except ChildProcessError:
196
+ color("Child process operation failed\n子进程操作失败", 33, None, 1)
197
+ exit(1)
198
+ except InterruptedError:
199
+ color("System call interrupted by signal\n系统调用被信号中断", 33, None, 1)
200
+ continue
201
+ except StopIteration:
202
+ color("Custom iterator input exhausted\n自定义迭代器输入已耗尽", 33, None, 1)
203
+ continue
204
+ except GeneratorExit:
205
+ color("Generator closed\n生成器已关闭", 33, None, 1)
206
+ continue
207
+ except Exception as e:
208
+ color(f"Code error\nError message:\n{e}\n代码错误\n错误信息:\n{e}", 33, None, 1)
209
+ continue
210
+
211
+ def safe_input(prompt, text_color=None, bg_color=None, mode=0) -> str:
212
+ return safe_try_except(prompt, True, None, text_color, bg_color, mode)
213
+
214
+ def _safe_number(prompt, convert_func, text_color=None, bg_color=None, mode=0):
215
+ return safe_try_except(prompt, False, convert_func, text_color, bg_color, mode)
216
+
217
+ def safe_int(prompt: str, text_color: Optional[int] = None, bg_color: Optional[int] = None, mode: int = 0) -> int:
218
+ """ 安全输出整数 """
219
+ return _safe_number(prompt, int, text_color, bg_color, mode)
220
+
221
+ def safe_float(prompt: str, text_color: Optional[int] = None, bg_color: Optional[int] = None, mode: int = 0) -> float:
222
+ """ 安全输出浮点数 """
223
+ return _safe_number(prompt, float, text_color, bg_color, mode)
224
+
225
+ # -- 测试部分 -- #
226
+ if __name__ == "__main__":
227
+ print("=== 测试基本颜色 ===\n=== Test basic colors ===")
228
+ color("红色文本\nRed text", text_color=31)
229
+ color("绿色加粗\nGreen bold", text_color=32, mode=1)
230
+ color("黄字红底\nYellow text Red background", text_color=33, bg_color=41)
231
+ color("红字黄底加粗\nRed text Yellow background Bold", text_color=31, bg_color=43, mode=1)
232
+
233
+ print("\n=== 测试RGB颜色 ===\n=== Test RGB colors ===")
234
+ color_rgb("RGB红色\nRGB Red", r=255, g=0, b=0)
235
+ color_rgb("RGB绿色加粗\nRGB Green Bold", r=0, g=255, b=0, mode=1)
236
+
237
+ print("\n=== 测试清屏 ===\n=== Test Clear Screen ===")
238
+ safe_input("按回车清屏...\nPress Enter to clear screen", 32, None, 1)
239
+ clear_screen()
240
+ color("清屏成功\nScreen cleared successfully", text_color=32, mode=1)
241
+
242
+ print('\n=== 测试"输入"函数 ===\n=== Test "Input" Functions ===')
243
+ name = safe_input("请输入名字(Please Enter Name): ", text_color=36)
244
+ age = safe_int("请输入年龄(Please Enter Age): ", text_color=33)
245
+ height = safe_float("请输入身高(米) (Please Enter height (m)): ", text_color=34)
246
+
247
+ color(f"\n你好 {name}, 年龄 {age}, 身高 {height}m\nHello {name}, Age {age}, height {height}", text_color=32, mode=1)
248
+
249
+ print("\n所有测试完成!\nAll Tests successfully!")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: Luci-Code-Tools
3
- Version: 1.2.3
3
+ Version: 2.0.0
4
4
  Summary: 安全输入工具库,支持彩色输出,KeyboardInterrupt保护等
5
5
  Home-page: https://github.com/Luci-Codeing/code_tools
6
6
  Author: Luci_Code
@@ -52,7 +52,6 @@ import Luci_code_tools
52
52
  | color_rgb | 输出RGB彩色字符 | data, r, g, b, mode, newline |
53
53
  | clear_screen | 可跨平台的清屏 | None (没有参数) |
54
54
  | safe_* | 安全的输入, 防止Ctrl + C和输入流关闭等报错 | prompt, text_color, bg_color, mode |
55
- | cloudflare_play | 输出 CloudFlare 人机验证 | None (没有参数) |
56
55
 
57
56
  **提示**: ***参数需要按照顺序填***
58
57
 
@@ -125,12 +124,15 @@ clear_screen()
125
124
  **clear_screen源码**
126
125
 
127
126
  ```python
128
- def clear_screen() -> None:
127
+ def clear_screen() -> None: # 清屏: 兼容版
129
128
  """ 跨平台兼容清屏 """
130
- if platform == "win32":
131
- run(['cmd', '/c', 'cls'])
132
- else:
133
- run(['clear'])
129
+ try: # 检测环境
130
+ if platform == "win32": # 环境为 Windows
131
+ run(['cmd', '/c', 'cls']) # 安全: 使用列表命令, 防注入
132
+ else: # 包括 MacOS, Linux (Android 也是 Linux 列)
133
+ run(['clear'], check=False) # check = 遇到错误我自己来, 别报错
134
+ except Exception: # 非交互式环境 (.py 打包.exe类环境等没有cmd命令的环境)
135
+ print("\n" * 200) # 手动清屏
134
136
  ```
135
137
 
136
138
  **clear_screen** 可以直接使用, 无需参数, 自动适配平台并运行相应的 *清屏命令*
@@ -167,16 +169,6 @@ safe_input, safe_int, safe_float
167
169
 
168
170
  ---
169
171
 
170
- - cloudflare_play
171
-
172
- 这个函数用着玩的, 无参数, 运行输出:
173
- ┏━━━━━━━━━━━━━━┓
174
- ┃ ┌┐ 请验证您是真人 ☁️ ┃
175
- ┃ └┘ cloudflare ┃
176
- ┗━━━━━━━━━━━━━━┛
177
-
178
- ---
179
-
180
172
  # 其他
181
173
 
182
174
  **ANSI常用序列**
@@ -30,7 +30,6 @@ import Luci_code_tools
30
30
  | color_rgb | 输出RGB彩色字符 | data, r, g, b, mode, newline |
31
31
  | clear_screen | 可跨平台的清屏 | None (没有参数) |
32
32
  | safe_* | 安全的输入, 防止Ctrl + C和输入流关闭等报错 | prompt, text_color, bg_color, mode |
33
- | cloudflare_play | 输出 CloudFlare 人机验证 | None (没有参数) |
34
33
 
35
34
  **提示**: ***参数需要按照顺序填***
36
35
 
@@ -103,12 +102,15 @@ clear_screen()
103
102
  **clear_screen源码**
104
103
 
105
104
  ```python
106
- def clear_screen() -> None:
105
+ def clear_screen() -> None: # 清屏: 兼容版
107
106
  """ 跨平台兼容清屏 """
108
- if platform == "win32":
109
- run(['cmd', '/c', 'cls'])
110
- else:
111
- run(['clear'])
107
+ try: # 检测环境
108
+ if platform == "win32": # 环境为 Windows
109
+ run(['cmd', '/c', 'cls']) # 安全: 使用列表命令, 防注入
110
+ else: # 包括 MacOS, Linux (Android 也是 Linux 列)
111
+ run(['clear'], check=False) # check = 遇到错误我自己来, 别报错
112
+ except Exception: # 非交互式环境 (.py 打包.exe类环境等没有cmd命令的环境)
113
+ print("\n" * 200) # 手动清屏
112
114
  ```
113
115
 
114
116
  **clear_screen** 可以直接使用, 无需参数, 自动适配平台并运行相应的 *清屏命令*
@@ -145,16 +147,6 @@ safe_input, safe_int, safe_float
145
147
 
146
148
  ---
147
149
 
148
- - cloudflare_play
149
-
150
- 这个函数用着玩的, 无参数, 运行输出:
151
- ┏━━━━━━━━━━━━━━┓
152
- ┃ ┌┐ 请验证您是真人 ☁️ ┃
153
- ┃ └┘ cloudflare ┃
154
- ┗━━━━━━━━━━━━━━┛
155
-
156
- ---
157
-
158
150
  # 其他
159
151
 
160
152
  **ANSI常用序列**
@@ -5,7 +5,7 @@ with open("README.md", "r", encoding="utf-8") as f:
5
5
 
6
6
  setup(
7
7
  name="Luci-Code-Tools",
8
- version="1.2.3",
8
+ version="2.0.0",
9
9
  author="Luci_Code",
10
10
  author_email="3967742419@qq.com",
11
11
  description="安全输入工具库,支持彩色输出,KeyboardInterrupt保护等",
@@ -1,179 +0,0 @@
1
- from sys import platform, exit
2
- from subprocess import run
3
- from typing import Optional
4
-
5
- __version__ = "1.2.3"
6
-
7
- class _AnsiCode:
8
- ESC = "\033"
9
- RESET = f"{ESC}[0m"
10
- @staticmethod
11
- def build(mode: int = 0, fg: Optional[int] = None, bg: Optional[int] = None) -> str:
12
- parts = []
13
- if mode != 0:
14
- parts.append(str(mode))
15
- if fg is not None:
16
- parts.append(str(fg))
17
- if bg is not None:
18
- parts.append(str(bg))
19
- if parts:
20
- return f"{_AnsiCode.ESC}[{';'.join(parts)}m"
21
- return ""
22
-
23
- def _ansi_return(data: str, text_color: Optional[int] = None, bg_color: Optional[int] = None, mode: int = 0) -> str:
24
- """ 生成ANSI彩色文本(不自动输出)
25
-
26
- 参数:
27
- data: 要着色的文本内容
28
- text_color: 前景色代码 (30-37普通色, 90-97亮色)
29
- bg_color: 背景色代码 (40-47普通色, 100-107亮色)
30
- mode: 显示方式 (0=默认,1=粗体,2=暗淡,3=斜体,4=下划线,5=闪烁,7=反色,8=隐藏,9=删除线)
31
-
32
- 返回:
33
- 带ANSI转义序列的字符串
34
- """
35
-
36
- if text_color is None and bg_color is None:
37
- return data
38
-
39
- ansi = _AnsiCode.build(mode, text_color, bg_color)
40
- return f"{ansi}{data}{_AnsiCode.RESET}"
41
-
42
- def _ansi_rgb(data: str, r: int, g: int, b: int, mode: int = 0) -> str:
43
- """ 内部函数 """
44
- if mode == 0:
45
- return f"{_AnsiCode.ESC}[38;2;{r};{g};{b}m{data}{_AnsiCode.RESET}"
46
- else:
47
- return f"{_AnsiCode.ESC}[{mode};38;2;{r};{g};{b}m{data}{_AnsiCode.RESET}"
48
-
49
- def color_rgb(data: str, r: int, g: int, b: int, mode: int = 0, newline: bool = True) -> None:
50
- """ 输出 rgb 字符 """
51
- print(_ansi_rgb(data, r, g, b, mode))
52
- if newline:
53
- print()
54
-
55
- def color(data: str, text_color: Optional[int] = None, bg_color: Optional[int] = None, mode: int = 0, newline: bool =True) -> None:
56
- """ 输出彩色字符到控制台
57
-
58
- 参数:
59
- data: 要输出的文本内容
60
- text_color: 前景色代码 (30-37普通色, 90-97亮色)
61
- bg_color: 背景色代码 (40-47普通色, 100-107亮色)
62
- mode: 显示方式 (0=默认,1=粗体,2=暗淡,3=斜体,4=下划线,5=闪烁,7=反色,8=隐藏,9=删除线)
63
- newline: 是否在输出后添加空行,默认True
64
-
65
- 示例:
66
- color("错误信息", text_color=31, mode=1) # 红色加粗
67
- color("警告", text_color=33, bg_color=41) # 黄字红底
68
- """
69
-
70
- print(_ansi_return(data, text_color, bg_color, mode))
71
- if newline:
72
- print()
73
-
74
- """ newline: 提高观感
75
- 普通输出:
76
- 1
77
- 2
78
- 3
79
- 1-1
80
- 2-1
81
- Input: Luci_Code
82
- newline 输出:
83
- 1
84
- 2
85
- 3
86
-
87
- 1-1
88
- 2-1
89
-
90
- Input: Luci_Code
91
- """
92
-
93
- def clear_screen() -> None:
94
- """ 跨平台兼容清屏 """
95
- if platform == "win32":
96
- run(['cmd', '/c', 'cls'])
97
- else:
98
- run(['clear'])
99
-
100
- def safe_input(prompt: str, text_color: Optional[int] = None, bg_color: Optional[int] = None, mode: int = 0) -> str:
101
- """ 安全输出字符串 """
102
- if text_color:
103
- prompt = _ansi_return(prompt, text_color, bg_color, mode)
104
- try:
105
- return input(prompt)
106
- except KeyboardInterrupt:
107
- clear_screen()
108
- color("退出成功(Ctrl + C)", 32, None, 1)
109
- exit(0)
110
- except EOFError:
111
- clear_screen()
112
- color("输入流关闭, 自动退出", 34, None, 1)
113
- exit(0)
114
-
115
- def _safe_number(prompt: str, convert_func, text_color: Optional[int] = None, bg_color: Optional[int] = None, mode: int = 0):
116
- """ 内部函数:安全的数字输入 """
117
- if convert_func is None:
118
- raise ValueError("convert_func cannot be None")
119
- if text_color:
120
- prompt = _ansi_return(prompt, text_color, bg_color, mode)
121
- while True:
122
- try:
123
- return convert_func(input(prompt))
124
- except ValueError:
125
- color("\n输入类型错误", 34, None, 1)
126
- continue
127
- except KeyboardInterrupt:
128
- clear_screen()
129
- color("退出成功(Ctrl + C)", 34, None, 1)
130
- exit(0)
131
- except EOFError:
132
- clear_screen()
133
- color("输入流关闭, 自动退出", 34, None, 1)
134
- exit(0)
135
-
136
- def safe_int(prompt: str, text_color: Optional[int] = None, bg_color: Optional[int] = None, mode: int = 0) -> int:
137
- """ 安全输出整数 """
138
- return _safe_number(prompt, int, text_color, bg_color, mode)
139
-
140
- def safe_float(prompt: str, text_color: Optional[int] = None, bg_color: Optional[int] = None, mode: int = 0) -> float:
141
- """ 安全输出浮点数 """
142
- return _safe_number(prompt, float, text_color, bg_color, mode)
143
-
144
- def cloudflare_play() -> None:
145
- cloudflare_box = """
146
- ┏━━━━━━━━━━━━━━┓
147
- ┃ ┌┐ 请验证您是真人 ☁️ ┃
148
- ┃ └┘ cloudflare ┃
149
- ┗━━━━━━━━━━━━━━┛
150
- """
151
- color(cloudflare_box, 32, None, 1)
152
-
153
- # -- 测试部分 -- #
154
- if __name__ == "__main__":
155
- print("=== 测试基本颜色 ===")
156
- color("红色文本", text_color=31)
157
- color("绿色加粗", text_color=32, mode=1)
158
- color("黄字红底", text_color=33, bg_color=41)
159
-
160
- print("\n=== 测试RGB颜色 ===")
161
- color_rgb("RGB红色", r=255, g=0, b=0)
162
- color_rgb("RGB绿色加粗", r=0, g=255, b=0, mode=1)
163
-
164
- print("\n=== 测试清屏 ===")
165
- input("按回车清屏...")
166
- clear_screen()
167
- color("清屏成功", text_color=32, mode=1)
168
-
169
- print("\n=== 测试CloudFlare样式 ===")
170
- cloudflare_play()
171
-
172
- print("\n=== 测试输入函数 ===")
173
- name = safe_input("请输入名字: ", text_color=36)
174
- age = safe_int("请输入年龄: ", text_color=33)
175
- height = safe_float("请输入身高(m): ", text_color=34)
176
-
177
- color(f"\n你好 {name}, 年龄 {age}, 身高 {height}m", text_color=32, mode=1)
178
-
179
- print("\n所有测试完成!")
File without changes