Show colorized error messages

This commit is contained in:
Demian Ferreiro
2013-03-01 22:42:26 -03:00
parent 3182475207
commit 55c99dfaec
4 changed files with 23 additions and 10 deletions

View File

@@ -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');

View File

@@ -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;
};

View File

@@ -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}"

View File

@@ -17,12 +17,17 @@ exports.CompilerError = class CompilerError extends Error
# Creates a nice error message like, following the "standard" format
# <filename>:<line>:<col>: <message> 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}