]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
compiler: Add --color=WHEN option
authorRico Tzschichholz <ricotz@ubuntu.com>
Wed, 12 Apr 2017 11:19:32 +0000 (13:19 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Mon, 17 Apr 2017 08:58:45 +0000 (10:58 +0200)
It follows the format of other tools like diff and git-diff.

compiler/valacompiler.vala
doc/valac.1
doc/valac.h2m
vala/valareport.vala
vapi/glib-2.0.vapi

index 76976efb828015ab947ad62b31201e5ae310dd37..0b76eece245a7653a092ebb6b5da79b4d4812d04 100644 (file)
@@ -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);
                        }
                }
 
index 6f1b286543103aaf021f901602683a92127ce7f6..db2f4e54e410bb3e50e557a06179a3e016199ec6 100644 (file)
@@ -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
index ce9bc23ba36e3faf76bf12c7a84de82fb3439058..87b41b7d28c9850afbe876dfee646796db17641c 100644 (file)
@@ -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
 
index f977dce036fb6b351c8940f86d3c8f8e633b3011..69e2103757e2cecbb48f92311c2e24a1a00a7891 100644 (file)
@@ -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;
index b0532406e1143665650328aed1dc702462540afb..67b06234e2f60365f12935f7249f9c7a416dbba6 100644 (file)
@@ -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 */