4 个版本
| 0.2.1 | 2023 年 8 月 6 日 |
|---|---|
| 0.2.0 | 2023 年 8 月 6 日 |
| 0.1.1 | 2023 年 8 月 2 日 |
| 0.1.0 | 2023 年 7 月 31 日 |
#903 in 命令行界面
6KB
68 行
setup-console
一个提供在 Windows 命令提示符中修复 ANSI 转义问题的函数的库。
用法
在打印任何内容之前,只需在主函数中运行 setup_console::init() (或 setup_console::try_init()) 即可。
fn main() {
println!("\x1b[31mRed \x1b[32mGreen \x1b[34mBlue\x1b[39m");
// ・[31mRed ・[32mGreen ・[34mBlue・[39m
setup_console::init();
println!("\x1b[31mRed \x1b[32mGreen \x1b[34mBlue\x1b[39m");
// Red Green Blue
}
恐慌
这仅适用于 Windows。
setup_console::init() 不会恐慌,而是显示消息并使用从 GetLastError() 获取的错误代码终止程序。
导致程序终止的情况包括
- 当 stdout 无效时(例如
program 1>nul) - 当程序无法运行
GetConsoleMode() - 当程序无法运行
SetConsoleMode()
如果您不希望这种行为,可以使用 setup_console::try_init(),它返回 Result。
依赖关系
~0–11MB
~68K SLoC