【问题标题】:Which tool for colorizing output of javac?哪个工具可以为javac的输出着色?
【发布时间】:2012-09-24 01:20:26
【问题描述】:

我们有一个高度并行化的构建过程,所以我经常不得不浏览 javac 的大量输出来查找构建错误。

为了使这更容易,如果有一些工具可以为我的终端的 javac 输出着色,突出代码中的错误,那就太好了。

我可以使用什么工具来为 javac 的输出着色?

【问题讨论】:

标签: java colors terminal javac


【解决方案1】:

使用任何正则表达式匹配器来匹配您的文本并使用终端颜色转义码将其包围以应用颜色,从而滚动您自己的 javac 错误着色器:

使用readfilesubstitute 意识形态:

#1.  Do your javac and pipe the result to a file:
javac whatever.java 2>/tmp/javac_errors.out;

#define the escape start and stop codes that your terminal 
#uses to apply foreground and background color:
let redbackground        = '\\e[48;5;196m'
let normalbackground     = '\\e[0;0m'

#iterate the lines in the saved file:
for line in readfile("/tmp/javac_errors.out")

    #Use sed, match, substitute or whatever to regex substitute 
    #the text with the text surrounded by the color escape codes
    #find and replace the text 'error:' with the same surrounded by escape codes
    let line = substitute(line, 
                          'error:',
                           redbackground . 
                           'error:' . 
                           normalbackground, 'g')

    #use echo -e flag to tell the terminal to interpret the escape codes:
    echo -e line
endfor

为我工作:

这段代码和上面一样,但是它使用了一个终端行迭代器和一个sed替换思想:

#run javac pipe to file
javac whatever.java 2>/tmp/errors.out

#Define terminal color codes
redbackground='\\e[48;5;196m'
normalbackground='\\e[0;0m'

#read the file and print out each line 
filename="/tmp/errors.out" 
while read -r line; do  
    #replace error surround with escape codes 
    line=`sed "s/error:/${redbackground}error:${normalbackground}/g" <<<"$line"` 
    echo -e "$line" 
done < "$filename" 

【讨论】:

    【解决方案2】:

    grep 与“--color”选项一起使用?

    ~$ javac Test.java 2>&1 | egrep --color "^|error"
    

    【讨论】:

    • 除了这会抑制所有不是错误的输出,这是我不想要的。
    • 在 -A/-B 中添加一个高数字感觉有点骇人听闻? :)
    • grepping 使用 "^|error" 将打印所有行,突出显示“错误”,并且不会使用 hack 打印 10000 行。
    【解决方案3】:

    我最终使用了一个名为 Generic Colorizer Tool 的工具并编写了自己的配置来为最重要的输出着色。工作得很好。 :)

    【讨论】:

    • 我正在尝试解决同样的问题,您的 grc 配置是否可以在某处下载?
    • 不,基本上我只需要突出显示错误输出,这样我就可以在来自 16 个并发线程的数千行编译器输出中找到它。 :) 所以我的过滤器只是为读取的错误行着色并使其变为粗体。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多