No color mode for xy

This commit is contained in:
Aoran Zeng 2024-08-23 08:09:39 +08:00
parent 9fde99519b
commit a542e77769
No known key found for this signature in database
GPG Key ID: 8F8BA8488E10ED98
3 changed files with 27 additions and 9 deletions

View File

@ -7,7 +7,7 @@
* Contributors : Peng Gao <gn3po4g@outlook.com> * Contributors : Peng Gao <gn3po4g@outlook.com>
* | * |
* Created on : <2023-08-29> * Created on : <2023-08-29>
* Last modified : <2024-08-22> * Last modified : <2024-08-23>
* *
* chsrc * chsrc
* ------------------------------------------------------------*/ * ------------------------------------------------------------*/
@ -18,10 +18,11 @@
#define App_Name "chsrc" #define App_Name "chsrc"
/* 命令行选项 */ /* 命令行选项 */
bool CliOpt_IPv6 = false; bool CliOpt_IPv6 = false;
bool CliOpt_Locally = false; bool CliOpt_Locally = false;
bool CliOpt_InEnglish = false; bool CliOpt_InEnglish = false;
bool CliOpt_DryRun = false; bool CliOpt_DryRun = false;
bool CliOpt_NoColor = false;
/** /**
* -local ** * -local **

View File

@ -8,7 +8,7 @@
* Contributors : Nil Null <nil@null.org> * Contributors : Nil Null <nil@null.org>
* | * |
* Created On : <2023-08-28> * Created On : <2023-08-28>
* Last Modified : <2024-08-17> * Last Modified : <2024-08-23>
* *
* xy: * xy:
* Corss-Platform C utilities for CLI applications in Ruby flavor * Corss-Platform C utilities for CLI applications in Ruby flavor
@ -17,7 +17,7 @@
#ifndef XY_H #ifndef XY_H
#define 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" #define _XY_Maintain_URL "https://gitee.com/RubyMetric/chsrc/blob/main/include/xy.h"
#include <assert.h> #include <assert.h>
@ -29,6 +29,9 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
/* Global */
bool xy_enable_color = true;
// #define NDEBUG // #define NDEBUG
#ifdef _WIN32 #ifdef _WIN32
@ -263,6 +266,12 @@ _xy_str_to_terminal_style (int style, const char *str)
{ {
char *color_fmt_str = NULL; char *color_fmt_str = NULL;
if (!xy_enable_color)
{
color_fmt_str = "%s";
goto new_str;
}
switch (style) switch (style)
{ {
case _XY_Str_Red: 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; color_fmt_str = "\e[9m%s\e[0m"; break;
} }
new_str:
// -2 把中间%s减掉 // -2 把中间%s减掉
size_t len = strlen (color_fmt_str) - 2; size_t len = strlen (color_fmt_str) - 2;
char *buf = malloc (strlen (str) + len + 1); char *buf = malloc (strlen (str) + len + 1);

View File

@ -11,7 +11,7 @@
* | BlockLune <blocklune@gmail.com> * | BlockLune <blocklune@gmail.com>
* | * |
* Created On : <2023-08-28> * Created On : <2023-08-28>
* Last Modified : <2024-08-22> * Last Modified : <2024-08-23>
* *
* chsrc: Change Source * chsrc: Change Source
* ------------------------------------------------------------*/ * ------------------------------------------------------------*/
@ -124,7 +124,8 @@ Chsrc_Usage[] = {
"-dry Dry Run模拟换源过程命令仅打印并不运行", "-dry Dry Run模拟换源过程命令仅打印并不运行",
"-ipv6 使用IPv6测速", "-ipv6 使用IPv6测速",
"-local 仅对本项目而非全局换源 (通过ls <target>查看支持情况)", "-local 仅对本项目而非全局换源 (通过ls <target>查看支持情况)",
"-en(glish) 使用英文输出\n", "-en(glish) 使用英文输出",
"-no-color 无颜色输出\n",
"维护: <" Chsrc_Maintain_URL ">" "维护: <" Chsrc_Maintain_URL ">"
}; };
@ -155,7 +156,8 @@ Chsrc_Usage_English[] = {
"-dry Dry Run. Simulate the source changing process, command only prints, not run", "-dry Dry Run. Simulate the source changing process, command only prints, not run",
"-ipv6 Speed measurement using IPv6", "-ipv6 Speed measurement using IPv6",
"-local Change source only for this project rather than globally (Via ls <target>)", "-local Change source only for this project rather than globally (Via ls <target>)",
"-en(glish) Output in English\n", "-en(glish) Output in English",
"-no-color Output without color\n",
"Maintain: <" Chsrc_Maintain_URL ">" "Maintain: <" Chsrc_Maintain_URL ">"
}; };
@ -523,6 +525,11 @@ main (int argc, char const *argv[])
CliOpt_DryRun = true; CliOpt_DryRun = true;
chsrc_log (bdyellow ("**开启Dry Run模式模拟换源过程(跳过测速),命令仅打印并不运行**\n")); 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") else if (xy_streql (argv[i], "-h")
|| xy_streql (argv[i], "-help") || xy_streql (argv[i], "-help")
|| xy_streql (argv[i], "--help")) || xy_streql (argv[i], "--help"))