[cli] Add an option to disable output coloring.
authorMatthias Bolte <matthias.bolte@googlemail.com>
Fri, 10 Apr 2009 00:32:44 +0000 (02:32 +0200)
committerMatthias Bolte <matthias.bolte@googlemail.com>
Fri, 10 Apr 2009 00:32:44 +0000 (02:32 +0200)
Signed-off-by: Matthias Bolte <matthias.bolte@googlemail.com>

cli/core.c
cli/help.c
include/libvsccli/core.h
include/libvsccli/option.h

index e292d75..95a64f6 100644 (file)
@@ -39,6 +39,7 @@ const char *_vsc_cli_name = NULL;
 const char *_vsc_cli_help = NULL;
 const struct VscCliOptionInfo *_vsc_cli_option_infos = NULL;
 const struct VscCliCommandInfo **_vsc_cli_command_infos = NULL;
+int _vsc_cli_output_coloring = TRUE;
 int _vsc_cli_interactive = FALSE;
 
 static void
@@ -69,7 +70,8 @@ void
 vsc_cli_init (struct VscError *error,
               const char *name, const char *help,
               const struct VscCliOptionInfo *option_infos,
-              const struct VscCliCommandInfo **command_infos)
+              const struct VscCliCommandInfo **command_infos,
+              int output_coloring)
 {
        VSC__ASSERT (error != NULL);
        VSC__ASSERT (! error->occured);
@@ -94,6 +96,7 @@ vsc_cli_init (struct VscError *error,
        _vsc_cli_help = help;
        _vsc_cli_option_infos = option_infos;
        _vsc_cli_command_infos = command_infos;
+       _vsc_cli_output_coloring = output_coloring;
 
        rl_readline_name = "vscmgmt-cli";
        /*rl_attempted_completion_function = _readline_completion;*/
@@ -118,17 +121,26 @@ vsc_cli_cleanup (void)
 void
 vsc_cli_error_fprint (struct VscError *error, FILE *stream)
 {
+       static const char *prefix_colored = "\033[01;31mError\033[0m";
+       static const char *prefix = "Error";
+
        if (_verbose_errors) {
                vsc_error_fprint (error, stream);
        } else {
                if (error->message != NULL) {
                        if (error->code == VSC__ERROR_CODE__REMOTE_ERROR) {
-                               fprintf (stream, "\033[01;31mError\033[0m: Remote: %s\n", error->message);
+                               fprintf (stream, "%s: Remote: %s\n",
+                                        _vsc_cli_output_coloring ? prefix_colored : prefix,
+                                        error->message);
                        } else {
-                               fprintf (stream, "\033[01;31mError\033[0m: %s\n", error->message);
+                               fprintf (stream, "%s: %s\n",
+                                        _vsc_cli_output_coloring ? prefix_colored : prefix,
+                                        error->message);
                        }
                } else {
-                       fprintf (stream, "\033[01;31mError\033[0m: %s\n", vsc_error_string(error->code));
+                       fprintf (stream, "%s: %s\n",
+                                _vsc_cli_output_coloring ? prefix_colored : prefix,
+                                vsc_error_string(error->code));
                }
        }
 }
@@ -162,6 +174,9 @@ vsc_cli_parse_option_list (struct VscError *error, const char *input,
                        if (strcmp (option.info->long_name,
                                    VSC_CLI__VERBOSE_ERRORS__OPTION_INFO__LONG_NAME) == 0) {
                                _verbose_errors = TRUE;
+                       } else if (strcmp (option.info->long_name,
+                                   VSC_CLI__NO_COLOR__OPTION_INFO__LONG_NAME) == 0) {
+                               _vsc_cli_output_coloring = FALSE;
                        }
 
                        _vsc_cli_option_list_append (error, &option_list, &option);
index 1135a7b..f243a0f 100644 (file)
@@ -32,6 +32,7 @@ extern const char *_vsc_cli_name;
 extern const char *_vsc_cli_help;
 extern const struct VscCliOptionInfo *_vsc_cli_option_infos;
 extern const struct VscCliCommandInfo **_vsc_cli_command_infos;
+extern int _vsc_cli_output_coloring;
 extern int _vsc_cli_interactive;
 
 static void
@@ -74,11 +75,11 @@ _help (struct VscError *error, const struct VscCliOption *option_list,
                        return;
                }
 
-               printf ("\033[1mNAME\033[0m\n");
+               printf (_vsc_cli_output_coloring ? "\033[1mNAME\033[0m\n" : "NAME\n");
                printf ("       %s - %s\n", command_info->name, command_info->help);
                printf ("\n");
 
-               printf ("\033[1mSYNOPSIS\033[0m\n");
+               printf (_vsc_cli_output_coloring ? "\033[1mSYNOPSIS\033[0m\n" : "SYNOPSIS\n");
                printf ("       %s", command_info->name);
 
                for (i = 0; command_info->option_infos[i].long_name != NULL; ++i) {
@@ -96,13 +97,13 @@ _help (struct VscError *error, const struct VscCliOption *option_list,
                if (command_info->description != NULL &&
                    *command_info->description != '\0') {
                        printf ("\n");
-                       printf ("\033[1mDESCRIPTION\033[0m\n");
+                       printf (_vsc_cli_output_coloring ? "\033[1mDESCRIPTION\033[0m\n" : "DESCRIPTION\n");
                        printf ("       %s\n", command_info->description);
                        printf ("\n");
                }
 
                if (command_info->option_infos[0].long_name != NULL) {
-                       printf ("\033[1mOPTIONS\033[0m\n");
+                       printf (_vsc_cli_output_coloring ? "\033[1mOPTIONS\033[0m\n" : "OPTIONS\n");
 
                        for (i = 0; command_info->option_infos[i].long_name != NULL; ++i) {
                                option_info = &command_info->option_infos[i];
@@ -119,11 +120,11 @@ _help (struct VscError *error, const struct VscCliOption *option_list,
                }
        } else {
                if (! _vsc_cli_interactive) {
-                       printf ("\033[1mNAME\033[0m\n");
+                       printf (_vsc_cli_output_coloring ? "\033[1mNAME\033[0m\n" : "NAME\n");
                        printf ("       %s - %s\n", _vsc_cli_name, _vsc_cli_help);
                        printf ("\n");
 
-                       printf ("\033[1mSYNOPSIS\033[0m\n");
+                       printf (_vsc_cli_output_coloring ? "\033[1mSYNOPSIS\033[0m\n" : "SYNOPSIS\n");
                        printf ("       %s", _vsc_cli_name);
 
                        for (i = 0; _vsc_cli_option_infos[i].long_name != NULL; ++i) {
@@ -140,7 +141,7 @@ _help (struct VscError *error, const struct VscCliOption *option_list,
 
                        if (_vsc_cli_option_infos[0].long_name != NULL) {
                                printf ("\n");
-                               printf ("\033[1mOPTIONS\033[0m\n");
+                               printf (_vsc_cli_output_coloring ? "\033[1mOPTIONS\033[0m\n" : "OPTIONS\n");
 
                                for (i = 0; _vsc_cli_option_infos[i].long_name != NULL; ++i) {
                                        option_info = &_vsc_cli_option_infos[i];
@@ -162,7 +163,7 @@ _help (struct VscError *error, const struct VscCliOption *option_list,
                                printf ("\n");
                        }
 
-                       printf ("\033[1mCOMMANDS\033[0m\n");
+                       printf (_vsc_cli_output_coloring ? "\033[1mCOMMANDS\033[0m\n" : "COMMANDS\n");
 
                        for (i = 0; _vsc_cli_command_infos[i] != NULL; ++i) {
                                printf ("       %-20s  %s\n", _vsc_cli_command_infos[i]->name,
index 077bb22..2092927 100644 (file)
@@ -33,7 +33,8 @@ void
 vsc_cli_init (struct VscError *error,
               const char *name, const char *help,
               const struct VscCliOptionInfo *option_infos,
-              const struct VscCliCommandInfo **command_infos);
+              const struct VscCliCommandInfo **command_infos,
+              int output_coloring);
 
 void
 vsc_cli_cleanup (void);
index 5017cda..d451d4a 100644 (file)
 #include "types.h"
 
 #define VSC_CLI__VERBOSE_ERRORS__OPTION_INFO__LONG_NAME "verbose-errors"
-
 #define VSC_CLI__VERBOSE_ERRORS__OPTION_INFO \
        { -1, VSC_CLI__VERBOSE_ERRORS__OPTION_INFO__LONG_NAME, \
-        "enable verbose error reporting", \
-        VSC_CLI__OPTION_TYPE__BOOL, FALSE }
+         "enable verbose error reporting", \
+         VSC_CLI__OPTION_TYPE__BOOL, FALSE }
+
+#define VSC_CLI__NO_COLOR__OPTION_INFO__LONG_NAME "no-color"
+#define VSC_CLI__NO_COLOR__OPTION_INFO \
+       { -1, VSC_CLI__NO_COLOR__OPTION_INFO__LONG_NAME, \
+         "disable output coloring", \
+         VSC_CLI__OPTION_TYPE__BOOL, FALSE }
 
 #ifdef __cplusplus
 extern "C" {