From 864fb2c77c9491b66e5d200350b8271d83be0776 Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Mon, 4 Dec 2017 13:26:33 +0800 Subject: [PATCH] improve find-in-jars: color grep match pattern #63 --- docs/java.md | 7 +++++++ find-in-jars | 6 ++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/docs/java.md b/docs/java.md index 9c639892..0a31db81 100644 --- a/docs/java.md +++ b/docs/java.md @@ -20,6 +20,7 @@ - [:beer: find-in-jars](#beer-find-in-jars) - [用法](#%E7%94%A8%E6%B3%95-2) - [示例](#%E7%A4%BA%E4%BE%8B-2) + - [运行效果](#%E8%BF%90%E8%A1%8C%E6%95%88%E6%9E%9C) - [参考资料](#%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99) @@ -399,6 +400,12 @@ WEB-INF/lib/aspectjweaver-1.8.8.jar!org/aspectj/weaver/XlintDefault.properties ...... ``` +### 运行效果 + +支持彩色输出,文件名中的匹配部分以`grep`的高亮方式显示。 + +![](https://user-images.githubusercontent.com/1063891/33537698-8869fb0a-d882-11e7-9568-92afca3e7a29.gif) + ### 参考资料 [在多个Jar(Zip)文件查找Log4J配置文件的Shell命令行](http://oldratlee.com/458/tech/shell/find-file-in-jar-zip-files.html) diff --git a/find-in-jars b/find-in-jars index 9a01ded5..660b91e7 100755 --- a/find-in-jars +++ b/find-in-jars @@ -185,15 +185,17 @@ readonly total_count="$(echo "$jar_files" | wc -l)" exit 1 } +$is_console && readonly grep_color_option='--color=always' + counter=1 while read jar_file; do printResponsiveMessage "finding in jar($((counter++))/$total_count): $jar_file" - $command_for_list_zip "${jar_file}" | grep $regex_mode $ignore_case_option "$pattern" | while read file; do + $command_for_list_zip "${jar_file}" | grep $regex_mode $ignore_case_option $grep_color_option -- "$pattern" | while read file; do clearResponsiveMessage $is_console && - echo "$ec[1;31m${jar_file}$eend"\!"$ec[1;32m${file}$eend" || + echo "$ec[1;31m${jar_file}$eend$ec[1;33m"\!"$eend${file}" || echo "${jar_file}"\!"${file}" done