Hello,

I really like the colored error message output in the clang C compiler,
I feel that it really helps the readability of the errors and warnings.
I would like the same colorful error messages in Vala, so I wrote a
patch that adds this to valac (based on the current git master).

To see what I am talking about see this image:
http://i.imgur.com/HXTlJ.png

The patched version is on the left of course.

However, my patch is only half complete. The colored output works, as
the screenshot shows, but there is no fallback for terminals that do not
support colors. I attempted to support this through curses, but instead
I just caused valac to segfault. If someone could finish it that would
be absolutely great.

-Nate

>From 32a0457a9626f6a2ddc04efe9429d304774a658c Mon Sep 17 00:00:00 2001
From: Nate Stedman <[email protected]>
Date: Wed, 1 Dec 2010 23:05:17 -0500
Subject: [PATCH] Adds colors to error and warning messages.

---
 vala/Makefile.am     |   14 +++++++++-
 vala/valareport.vala |   67 +++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 74 insertions(+), 7 deletions(-)

diff --git a/vala/Makefile.am b/vala/Makefile.am
index b20c832..f0e246c 100644
--- a/vala/Makefile.am
+++ b/vala/Makefile.am
@@ -176,9 +176,21 @@ valainclude_HEADERS = \
 	$(NULL)
 
 vala.vapi vala.vala.stamp: $(libvalacore_la_VALASOURCES)
-	$(VALA_V)$(VALAC) $(COVERAGE_VALAFLAGS) $(VALAFLAGS) -C --vapidir $(srcdir)/../vapi --pkg gobject-2.0 --vapidir $(srcdir)/../gee --pkg gee --vapidir $(srcdir)/../ccode --pkg ccode --pkg config -H vala.h --library vala $^
+	$(VALA_V)$(VALAC) $(COVERAGE_VALAFLAGS) $(VALAFLAGS) -C \
+	--vapidir $(srcdir)/../vapi --pkg gobject-2.0 \
+	--vapidir $(srcdir)/../gee --pkg gee \
+	--vapidir $(srcdir)/../ccode --pkg ccode --pkg config \
+	-H vala.h --library vala $^
 	@touch $@
 
+libvalacore_la_CFLAGS = \
+	-DVALA_REPORT_ANSI_RED=\"\\e[31m\"\
+	-DVALA_REPORT_ANSI_BRIGHT=\"\\e[1m\"\
+	-DVALA_REPORT_ANSI_GREEN=\"\\e[32m\"\
+	-DVALA_REPORT_ANSI_YELLOW=\"\\e[33m\"\
+	-DVALA_REPORT_ANSI_RESET=\"\\e[0m\"\
+	$(NULL)
+
 libvalacore_la_LIBADD = \
 	$(COVERAGE_LIBS) \
 	$(GLIB_LIBS) \
diff --git a/vala/valareport.vala b/vala/valareport.vala
index 14e3583..d497453 100644
--- a/vala/valareport.vala
+++ b/vala/valareport.vala
@@ -53,6 +53,16 @@ public class Vala.Report : Object {
 	public int get_errors () {
 		return errors;
 	}
+	
+	/**
+	 * The ANSI colors, which must be defined externally in Makefile.am because
+	 * Vala does not like the escape sequences.
+	 */
+	private extern const string ANSI_RED;
+	private extern const string ANSI_YELLOW;
+	private extern const string ANSI_GREEN;
+	private extern const string ANSI_RESET;
+	private extern const string ANSI_BRIGHT;
 
 	/**
 	 * Pretty-print the actual line of offending code if possible.
@@ -69,6 +79,10 @@ public class Vala.Report : Object {
 			stderr.printf ("%s\n", offending_line);
 			int idx;
 			
+			// make the ^^^^ colorful if available
+			bool colors = true;//Curses.has_colors() && Curses.can_change_color();
+			if (colors) stderr.printf (ANSI_GREEN);
+			
 			/* We loop in this manner so that we don't fall over on differing
 			 * tab widths. This means we get the ^s in the right places.
 			 */
@@ -87,8 +101,9 @@ public class Vala.Report : Object {
 					stderr.printf ("^");
 				}
 			}
-
-			stderr.printf ("\n");
+			
+			// newline, reset colors if they were set
+			stderr.printf ("%s\n", colors ? ANSI_RESET : "");
 		}
 	}
 
@@ -123,12 +138,32 @@ public class Vala.Report : Object {
 		if (!enable_warnings) {
 			return;
 		}
+		
+		bool colors = true;//Curses.has_colors() && Curses.can_change_color();
 
 		warnings++;
 		if (source == null) {
-			stderr.printf ("warning: %s\n", message);
+			if (colors) stderr.printf ("%s%s", ANSI_BRIGHT, ANSI_YELLOW);
+			stderr.printf ("warning: ");
+			
+			if (colors) stderr.printf ("%s%s", ANSI_RESET, ANSI_BRIGHT);
+			stderr.printf ("%s", message);
+			
+			if (colors) stderr.printf (ANSI_RESET);
+			stderr.printf ("\n");
 		} else {
-			stderr.printf ("%s: warning: %s\n", source.to_string (), message);
+			if (colors) stderr.printf ("%s", ANSI_BRIGHT);
+			stderr.printf ("%s: ", source.to_string ());
+			
+			if (colors) stderr.printf ("%s", ANSI_YELLOW);
+			stderr.printf ("warning: ");
+			
+			if (colors) stderr.printf ("%s%s", ANSI_RESET, ANSI_BRIGHT);
+			stderr.printf ("%s", message);
+			
+			if (colors) stderr.printf (ANSI_RESET);
+			stderr.printf ("\n");
+			
 			if (verbose_errors) {
 				report_source (source);
 			}
@@ -143,10 +178,30 @@ public class Vala.Report : Object {
 	 */
 	public virtual void err (SourceReference? source, string message) {
 		errors++;
+		bool colors = true;//Curses.has_colors() && Curses.can_change_color();
+		
 		if (source == null) {
-			stderr.printf ("error: %s\n", message);
+			if (colors) stderr.printf ("%s%s", ANSI_BRIGHT, ANSI_RED);
+			stderr.printf ("error: ");
+			
+			if (colors) stderr.printf ("%s%s", ANSI_RESET, ANSI_BRIGHT);
+			stderr.printf ("%s", message);
+			
+			if (colors) stderr.printf (ANSI_RESET);
+			stderr.printf ("\n");
 		} else {
-			stderr.printf ("%s: error: %s\n", source.to_string (), message);
+			if (colors) stderr.printf ("%s", ANSI_BRIGHT);
+			stderr.printf ("%s: ", source.to_string ());
+			
+			if (colors) stderr.printf ("%s", ANSI_RED);
+			stderr.printf ("warning: ");
+			
+			if (colors) stderr.printf ("%s%s", ANSI_RESET, ANSI_BRIGHT);
+			stderr.printf ("%s", message);
+			
+			if (colors) stderr.printf (ANSI_RESET);
+			stderr.printf ("\n");
+			
 			if (verbose_errors) {
 				report_source (source);
 			}
-- 
1.7.1

_______________________________________________
vala-list mailing list
[email protected]
http://mail.gnome.org/mailman/listinfo/vala-list

Reply via email to