]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Added colorful output
authorJCWasmx86 <JCWasmx86@t-online.de>
Sun, 5 Jun 2022 13:57:57 +0000 (15:57 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Sat, 29 Apr 2023 19:00:17 +0000 (21:00 +0200)
dbusgen/valadbusgen.vala
doc/valadbusgen.1

index 5daa100bd044a446220ecd3c681f7e00ed26c871..ec563442ab9d758f31d48331b8211be730196318 100644 (file)
@@ -25,7 +25,7 @@
 using GLib;
 
 public class Vala.DBusGen {
-       // TODO: Colored output
+
        public class ConcatenationStrategy : NamespaceStrategy {
 
                public override string? get_namespace (string ns) {
@@ -63,6 +63,8 @@ public class Vala.DBusGen {
        static string[] rename_namespaces;
        [CCode (array_length = false, array_null_terminated = true)]
        static string[] strip_namespaces;
+       static bool disable_colored_output;
+       static Report.Colored colored_output = Report.Colored.AUTO;
 
        CodeContext context;
 
@@ -75,11 +77,14 @@ public class Vala.DBusGen {
                { "rename-namespace", 0, 0, OptionArg.STRING_ARRAY, ref rename_namespaces, "Namespace to rename to", "SOURCE_NS:TARGET_NS..." },
                { "strip-namespace", 0, 0, OptionArg.STRING_ARRAY, ref strip_namespaces, "Namespace to strip", "NAMESPACE..." },
                { "dbus-timeout", 0, 0, OptionArg.INT, ref dbus_timeout, "DBus timeout", 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" },
                { "version", 0, 0, OptionArg.NONE, ref version, "Display version number", null },
                { "quiet", 'q', 0, OptionArg.NONE, ref quiet_mode, "Do not print messages to the console", null },
                { OPTION_REMAINING, 0, 0, OptionArg.FILENAME_ARRAY, ref sources, null, "FILE..." },
                { null }
        };
+       private const string DEFAULT_COLORS = "error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01";
 
        private int quit () {
                if (context.report.get_errors () == 0) {
@@ -97,12 +102,37 @@ public class Vala.DBusGen {
                }
        }
 
+       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 run () {
                context = new CodeContext ();
 
                context.report.enable_warnings = !disable_warnings;
                context.vapi_directories = vapi_directories;
                context.report.set_verbose_errors (!quiet_mode);
+
+               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, colored_output);
+                       } else {
+                               context.report.set_colors (DEFAULT_COLORS, colored_output);
+                       }
+               }
+
                CodeContext.push (context);
 
                context.set_target_profile (Profile.GOBJECT);
index 07d8e82eab658eb8890134fec774950a49803606..2c0188e1a009e656dbe78826510b6a88027242f4 100644 (file)
@@ -41,6 +41,17 @@ Namespace to strip
 \fB\-\-dbus\-timeout\fR
 DBus timeout
 .TP
+\fB\-\-no\-color\fR
+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\-\-version\fR
 Display version number
 .TP