[vala/staging: 7/9] compiler: Add --color=WHEN option



commit e1b399b332dbf0875b6de8499f31877db6f9f3a1
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Wed Apr 12 13:19:32 2017 +0200

    compiler: Add --color=WHEN option
    
    It follows the format of other tools like diff and git-diff.

 compiler/valacompiler.vala |   27 ++++++++++++++++++++++-----
 doc/valac.1                |   10 +++++++++-
 doc/valac.h2m              |    7 +++++++
 vala/valareport.vala       |   10 ++++++++--
 vapi/glib-2.0.vapi         |    2 ++
 5 files changed, 48 insertions(+), 8 deletions(-)
---
diff --git a/compiler/valacompiler.vala b/compiler/valacompiler.vala
index 76976ef..0b76eec 100644
--- a/compiler/valacompiler.vala
+++ b/compiler/valacompiler.vala
@@ -88,7 +88,8 @@ class Vala.Compiler {
        static bool enable_version_header;
        static bool disable_version_header;
        static bool fatal_warnings;
-       static bool disable_diagnostic_colors;
+       static bool disable_colored_output;
+       static Report.Colored colored_output = Report.Colored.AUTO;
        static string dependencies;
 
        static string entry_point;
@@ -147,7 +148,8 @@ class Vala.Compiler {
                { "profile", 0, 0, OptionArg.STRING, ref profile, "Use the given profile instead of the 
default", "PROFILE" },
                { "quiet", 'q', 0, OptionArg.NONE, ref quiet_mode, "Do not print messages to the console", 
null },
                { "verbose", 'v', 0, OptionArg.NONE, ref verbose_mode, "Print additional messages to the 
console", null },
-               { "no-color", 0, 0, OptionArg.NONE, ref disable_diagnostic_colors, "Disable colored output", 
null },
+               { "no-color", 0, 0, OptionArg.NONE, ref disable_colored_output, "Disable colored output, 
alias for --color=never", null },
+               { "color", 0, OptionFlags.OPTIONAL_ARG, OptionArg.CALLBACK, (void*) option_parse_color, 
"Enable color output, options are 'always', 'never', or 'auto'", "WHEN" },
                { "target-glib", 0, 0, OptionArg.STRING, ref target_glib, "Target version of glib for code 
generation", "MAJOR.MINOR" },
                { "gresources", 0, 0, OptionArg.STRING_ARRAY, ref gresources, "XML of gresources", "FILE..." 
},
                { "enable-version-header", 0, 0, OptionArg.NONE, ref enable_version_header, "Write vala build 
version in generated files", null },
@@ -156,6 +158,17 @@ class Vala.Compiler {
                { null }
        };
 
+       static bool option_parse_color (string option_name, string? val, void* data) throws OptionError {
+               switch (val) {
+                       case "auto": colored_output = Report.Colored.AUTO; break;
+                       case "never": colored_output = Report.Colored.NEVER; break;
+                       case null:
+                       case "always": colored_output = Report.Colored.ALWAYS; break;
+                       default: throw new OptionError.FAILED ("Invalid --color argument '%s'", val);
+               }
+               return true;
+       }
+
        private int quit () {
                if (context.report.get_errors () == 0 && context.report.get_warnings () == 0) {
                        return 0;
@@ -177,12 +190,16 @@ class Vala.Compiler {
                context = new CodeContext ();
                CodeContext.push (context);
 
-               if (disable_diagnostic_colors == false) {
+               if (disable_colored_output) {
+                       colored_output = Report.Colored.NEVER;
+               }
+
+               if (colored_output != Report.Colored.NEVER) {
                        unowned string env_colors = Environment.get_variable ("VALA_COLORS");
                        if (env_colors != null) {
-                               context.report.set_colors (env_colors);
+                               context.report.set_colors (env_colors, colored_output);
                        } else {
-                               context.report.set_colors (DEFAULT_COLORS);
+                               context.report.set_colors (DEFAULT_COLORS, colored_output);
                        }
                }
 
diff --git a/doc/valac.1 b/doc/valac.1
index 6f1b286..db2f4e5 100644
--- a/doc/valac.1
+++ b/doc/valac.1
@@ -172,7 +172,15 @@ Do not print messages to the console
 Print additional messages to the console
 .TP
 \fB\-\-no\-color\fR
-Disable colored output
+Disable colored output, alias for \fB\-\-color\fR=\fI\,never\/\fR
+.TP
+\fB\-\-color\fR=\fI\,WHEN\/\fR
+Enable color output, options are 'always', 'never', or 'auto'
+.RS
+When no value is given \fIalways\fR is implied. When neither \fB--color\fR
+or \fB--no-color\fR are declared then \fB--color\fR=\fIauto\fR is used where
+output is colored when stderr is a terminal.
+.RE
 .TP
 \fB\-\-target\-glib\fR=\fI\,MAJOR\/\fR.MINOR
 Target version of glib for code generation
diff --git a/doc/valac.h2m b/doc/valac.h2m
index ce9bc23..87b41b7 100644
--- a/doc/valac.h2m
+++ b/doc/valac.h2m
@@ -16,6 +16,13 @@ Vala source code into C source and header files. It uses the GObject
 type system to create classes and interfaces declared in the Vala
 source code.
 
+/Enable color output/
+.RS
+When no value is given \fIalways\fR is implied. When neither \fB--color\fR
+or \fB--no-color\fR are declared then \fB--color\fR=\fIauto\fR is used where
+output is colored when stderr is a terminal.
+.RE
+
 [BUGS]
 https://bugzilla.gnome.org/page.cgi?id=browse.html&product=vala
 
diff --git a/vala/valareport.vala b/vala/valareport.vala
index f977dce..69e2103 100644
--- a/vala/valareport.vala
+++ b/vala/valareport.vala
@@ -27,6 +27,12 @@ using GLib;
  * Namespace to centralize reporting warnings and errors.
  */
 public class Vala.Report : Object {
+       public enum Colored {
+               AUTO,
+               NEVER,
+               ALWAYS
+       }
+
        /**
         * SGR end tag
         */
@@ -109,7 +115,7 @@ public class Vala.Report : Object {
         *   "error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01"
         * }}}
         */
-       public bool set_colors (string str) {
+       public bool set_colors (string str, Report.Colored colored_output = Report.Colored.AUTO) {
                try {
                        if (val_regex == null)
                                val_regex = new Regex ("^\\s*[0-9]+(;[0-9]*)*\\s*$");
@@ -167,7 +173,7 @@ public class Vala.Report : Object {
                        }
                }
 
-               if (is_atty (stderr.fileno ())) {
+               if (colored_output == Report.Colored.ALWAYS || (colored_output == Report.Colored.AUTO && 
is_atty (stderr.fileno ()))) {
                        if (error_color != null) {
                                this.error_color_start = "\x1b[0" + error_color + "m";
                                this.error_color_end = ANSI_COLOR_END;
diff --git a/vapi/glib-2.0.vapi b/vapi/glib-2.0.vapi
index b053240..67b0623 100644
--- a/vapi/glib-2.0.vapi
+++ b/vapi/glib-2.0.vapi
@@ -3814,6 +3814,8 @@ namespace GLib {
        public delegate bool OptionParseFunc (OptionContext context, OptionGroup group, void* data) throws 
OptionError;
        [CCode (has_target = false)]
        public delegate void OptionErrorFunc (OptionContext context, OptionGroup group, void* data, ref Error 
error);
+       [CCode (has_target = false)]
+       public delegate bool OptionArgFunc (string option_name, string val, void* data) throws OptionError;
 
        /* Perl-compatible regular expressions */
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]