diff --git a/include/chsrc.h b/include/chsrc.h index 6ba3d60..c5929d2 100644 --- a/include/chsrc.h +++ b/include/chsrc.h @@ -7,7 +7,7 @@ * Contributors : Peng Gao * | * Created on : <2023-08-29> - * Last modified : <2024-08-22> + * Last modified : <2024-08-23> * * chsrc 头文件 * ------------------------------------------------------------*/ @@ -18,10 +18,11 @@ #define App_Name "chsrc" /* 命令行选项 */ -bool CliOpt_IPv6 = false; -bool CliOpt_Locally = false; +bool CliOpt_IPv6 = false; +bool CliOpt_Locally = false; bool CliOpt_InEnglish = false; -bool CliOpt_DryRun = false; +bool CliOpt_DryRun = false; +bool CliOpt_NoColor = false; /** * -local 的含义是启用 *项目级* 换源 diff --git a/include/xy.h b/include/xy.h index 26baa12..23e1f41 100644 --- a/include/xy.h +++ b/include/xy.h @@ -8,7 +8,7 @@ * Contributors : Nil Null * | * Created On : <2023-08-28> - * Last Modified : <2024-08-17> + * Last Modified : <2024-08-23> * * xy: 襄阳、咸阳 * Corss-Platform C utilities for CLI applications in Ruby flavor @@ -17,7 +17,7 @@ #ifndef XY_H #define XY_H -#define _XY_Version "v0.1.4.1-2024/08/17" +#define _XY_Version "v0.1.4.2-2024/08/23" #define _XY_Maintain_URL "https://gitee.com/RubyMetric/chsrc/blob/main/include/xy.h" #include @@ -29,6 +29,9 @@ #include #include +/* Global */ +bool xy_enable_color = true; + // #define NDEBUG #ifdef _WIN32 @@ -263,6 +266,12 @@ _xy_str_to_terminal_style (int style, const char *str) { char *color_fmt_str = NULL; + if (!xy_enable_color) + { + color_fmt_str = "%s"; + goto new_str; + } + switch (style) { case _XY_Str_Red: @@ -291,6 +300,7 @@ _xy_str_to_terminal_style (int style, const char *str) color_fmt_str = "\e[9m%s\e[0m"; break; } +new_str: // -2 把中间%s减掉 size_t len = strlen (color_fmt_str) - 2; char *buf = malloc (strlen (str) + len + 1); diff --git a/src/chsrc.c b/src/chsrc.c index f890386..590c619 100644 --- a/src/chsrc.c +++ b/src/chsrc.c @@ -11,7 +11,7 @@ * | BlockLune * | * Created On : <2023-08-28> - * Last Modified : <2024-08-22> + * Last Modified : <2024-08-23> * * chsrc: Change Source —— 全平台通用命令行换源工具 * ------------------------------------------------------------*/ @@ -124,7 +124,8 @@ Chsrc_Usage[] = { "-dry Dry Run,模拟换源过程,命令仅打印并不运行", "-ipv6 使用IPv6测速", "-local 仅对本项目而非全局换源 (通过ls 查看支持情况)", - "-en(glish) 使用英文输出\n", + "-en(glish) 使用英文输出", + "-no-color 无颜色输出\n", "维护: <" Chsrc_Maintain_URL ">" }; @@ -155,7 +156,8 @@ Chsrc_Usage_English[] = { "-dry Dry Run. Simulate the source changing process, command only prints, not run", "-ipv6 Speed measurement using IPv6", "-local Change source only for this project rather than globally (Via ls )", - "-en(glish) Output in English\n", + "-en(glish) Output in English", + "-no-color Output without color\n", "Maintain: <" Chsrc_Maintain_URL ">" }; @@ -523,6 +525,11 @@ main (int argc, char const *argv[]) CliOpt_DryRun = true; chsrc_log (bdyellow ("**开启Dry Run模式,模拟换源过程(跳过测速),命令仅打印并不运行**\n")); } + else if (xy_streql (argv[i], "-no-color") || xy_streql (argv[i], "-no-colour")) + { + CliOpt_NoColor = true; + xy_enable_color = false; + } else if (xy_streql (argv[i], "-h") || xy_streql (argv[i], "-help") || xy_streql (argv[i], "--help"))