This is an automated email from the ASF dual-hosted git repository. paulk pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/groovy.git
commit ca66fb66f1281c3d4826f14b1b16228dcf057cf7 Author: Paul King <[email protected]> AuthorDate: Sat Aug 23 19:04:36 2025 +1000 GROOVY-11741: provide header control switches for groovysh /grep inspired by ubuntu grep --- .../groovy/groovysh/jline/GroovyPosixCommands.java | 32 +++++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyPosixCommands.java b/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyPosixCommands.java index c06803a1e7..4019daabd5 100644 --- a/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyPosixCommands.java +++ b/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyPosixCommands.java @@ -501,6 +501,9 @@ public class GroovyPosixCommands extends PosixCommands { " -w --word-regexp Select only whole words", " -x --line-regexp Select only whole lines", " -c --count Only print a count of matching lines per file", + " -z --zero When used with -c, don't print a count of zero", + " -h --header Display filename header for each file (defaults to true if multiple files are given)", + " -H --no-filename Do not display filename header", " --color=WHEN Use markers to distinguish the matching string, may be `always', `never' or `auto'", " -B --before-context=NUM Print NUM lines of leading context before matching lines", " -A --after-context=NUM Print NUM lines of trailing context after matching lines", @@ -546,6 +549,7 @@ public class GroovyPosixCommands extends PosixCommands { before = contextLines; } boolean count = opt.isSet("count"); + boolean zero = opt.isSet("zero"); boolean quiet = opt.isSet("quiet"); boolean invert = opt.isSet("invert-match"); boolean lineNumber = opt.isSet("line-number"); @@ -586,6 +590,12 @@ public class GroovyPosixCommands extends PosixCommands { .collect(Collectors.toList())); } } + boolean filenameHeader = sources.size() > 1; + if (opt.isSet("header")) { + filenameHeader = true; + } else if (opt.isSet("no-filename")) { + filenameHeader = false; + } boolean match = false; for (GrepSource src : sources) { List<String> lines = new ArrayList<>(); @@ -605,7 +615,7 @@ public class GroovyPosixCommands extends PosixCommands { if (matches) { nb++; if (!count && !quiet) { - if (sources.size() > 1) { + if (filenameHeader) { if (colored) { applyStyle(sbl, colors, "fn"); } @@ -649,7 +659,7 @@ public class GroovyPosixCommands extends PosixCommands { } else if (lineMatch > 0) { context.out().println(lines.remove(0)); lineMatch--; - if (sources.size() > 1) { + if (filenameHeader) { if (colored) { applyStyle(sbl, colors, "fn"); } @@ -674,7 +684,7 @@ public class GroovyPosixCommands extends PosixCommands { } sbl.append(line); } else { - if (sources.size() > 1) { + if (filenameHeader) { if (colored) { applyStyle(sbl, colors, "fn"); } @@ -725,7 +735,21 @@ public class GroovyPosixCommands extends PosixCommands { } } if (count) { - context.out().println(nb); + if (nb != 0 || !zero) { + AttributedStringBuilder sbl = new AttributedStringBuilder(); + if (filenameHeader) { + if (colored) { + applyStyle(sbl, colors, "fn"); + } + sbl.append(src.getName()); + if (colored) { + applyStyle(sbl, colors, "se"); + } + sbl.append(":"); + } + sbl.append(Integer.toString(nb)); + context.out().println(sbl.toAnsi(context.terminal())); + } } match |= nb > 0; }
