From 55c99dfaec6fac007cff00c75fa9bd2cc5664934 Mon Sep 17 00:00:00 2001 From: Demian Ferreiro Date: Fri, 1 Mar 2013 22:42:26 -0300 Subject: [PATCH] Show colorized error messages --- lib/coffee-script/command.js | 4 ++-- lib/coffee-script/error.js | 15 +++++++++++---- src/command.coffee | 3 ++- src/error.coffee | 11 ++++++++--- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/lib/coffee-script/command.js b/lib/coffee-script/command.js index dc8954f1..3dd12d03 100644 --- a/lib/coffee-script/command.js +++ b/lib/coffee-script/command.js @@ -161,7 +161,7 @@ }; compileScript = function(file, input, base) { - var message, o, options, t, task; + var message, o, options, t, task, useColors; o = opts; options = compileOptions(file); try { @@ -196,7 +196,7 @@ if (CoffeeScript.listeners('failure').length) { return; } - message = err instanceof CompilerError ? err.prettyMessage(file || '[stdin]', input) : err.stack || ("ERROR: " + err); + message = err instanceof CompilerError ? (useColors = process.stdout.isTTY && !process.env.NODE_DISABLE_COLORS, err.prettyMessage(file || '[stdin]', input, useColors)) : err.stack || ("ERROR: " + err); if (o.watch) { if (o.watch) { return printLine(message + '\x07'); diff --git a/lib/coffee-script/error.js b/lib/coffee-script/error.js index 67100f9b..98f1a472 100644 --- a/lib/coffee-script/error.js +++ b/lib/coffee-script/error.js @@ -29,12 +29,19 @@ return new CompilerError(message, first_line, first_column, last_line, last_column); }; - CompilerError.prototype.prettyMessage = function(fileName, code) { - var end, errorLine, marker, message, start; + CompilerError.prototype.prettyMessage = function(fileName, code, useColors) { + var colorize, end, errorLine, marker, message, start; errorLine = code.split('\n')[this.startLine]; start = this.startColumn; - end = this.startLine === this.endLine ? this.endColumn : errorLine.length - 1; - marker = repeat(' ', start) + repeat('^', end - start + 1); + end = this.startLine === this.endLine ? this.endColumn + 1 : errorLine.length; + marker = repeat(' ', start) + repeat('^', end - start); + if (useColors) { + colorize = function(str) { + return "\x1B[1;31m" + str + "\x1B[0m"; + }; + errorLine = errorLine.slice(0, start) + colorize(errorLine.slice(start, end)) + errorLine.slice(end); + marker = colorize(marker); + } message = "" + fileName + ":" + (this.startLine + 1) + ":" + (this.startColumn + 1) + ": error: " + this.message + "\n" + errorLine + "\n" + marker; return message; }; diff --git a/src/command.coffee b/src/command.coffee index 1ca16b1f..f542b0d2 100644 --- a/src/command.coffee +++ b/src/command.coffee @@ -141,7 +141,8 @@ compileScript = (file, input, base) -> return if CoffeeScript.listeners('failure').length message = if err instanceof CompilerError - err.prettyMessage file or '[stdin]', input + useColors = process.stdout.isTTY and not process.env.NODE_DISABLE_COLORS + err.prettyMessage file or '[stdin]', input, useColors else err.stack or "ERROR: #{err}" diff --git a/src/error.coffee b/src/error.coffee index 8076c214..e0e07402 100644 --- a/src/error.coffee +++ b/src/error.coffee @@ -17,12 +17,17 @@ exports.CompilerError = class CompilerError extends Error # Creates a nice error message like, following the "standard" format # ::: plus the line with the error and a marker # showing where the error is. - prettyMessage: (fileName, code) -> + prettyMessage: (fileName, code, useColors) -> errorLine = code.split('\n')[@startLine] start = @startColumn # Show only the first line on multi-line errors. - end = if @startLine is @endLine then @endColumn else errorLine.length - 1 - marker = repeat(' ', start) + repeat('^', end - start + 1) + end = if @startLine is @endLine then @endColumn + 1 else errorLine.length + marker = repeat(' ', start) + repeat('^', end - start) + + if useColors + colorize = (str) -> "\x1B[1;31m#{str}\x1B[0m" + errorLine = errorLine[...start] + colorize(errorLine[start...end]) + errorLine[end..] + marker = colorize marker message = """ #{fileName}:#{@startLine + 1}:#{@startColumn + 1}: error: #{@message}