diff options
author | Rico Tzschichholz <ricotz@ubuntu.com> | 2017-04-12 13:19:32 +0200 |
---|---|---|
committer | Rico Tzschichholz <ricotz@ubuntu.com> | 2017-04-17 10:58:45 +0200 |
commit | e1b399b332dbf0875b6de8499f31877db6f9f3a1 (patch) | |
tree | 6676de4918b1218b467040d627debd7c0d7bedbc | |
parent | ebdb2048de123187c477096fa8132da8c0e69086 (diff) | |
download | vala-e1b399b332dbf0875b6de8499f31877db6f9f3a1.tar.gz |
compiler: Add --color=WHEN option
It follows the format of other tools like diff and git-diff.
-rw-r--r-- | compiler/valacompiler.vala | 27 | ||||
-rw-r--r-- | doc/valac.1 | 10 | ||||
-rw-r--r-- | doc/valac.h2m | 7 | ||||
-rw-r--r-- | vala/valareport.vala | 10 | ||||
-rw-r--r-- | vapi/glib-2.0.vapi | 2 |
5 files changed, 48 insertions, 8 deletions
diff --git a/compiler/valacompiler.vala b/compiler/valacompiler.vala index 76976efb8..0b76eece2 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 6f1b28654..db2f4e54e 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 ce9bc23ba..87b41b7d2 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 f977dce03..69e210375 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 b0532406e..67b06234e 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 */ |