Skip to content

Commit

Permalink
[ADDED] added --jex-include/exclude
Browse files Browse the repository at this point in the history
  • Loading branch information
thevpc committed Sep 26, 2023
1 parent a645a87 commit dd713ec
Show file tree
Hide file tree
Showing 8 changed files with 332 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collectors;

import net.thevpc.nuts.cmdline.NArg;
import net.thevpc.nuts.cmdline.NCmdLine;
Expand Down Expand Up @@ -83,20 +84,6 @@ protected boolean onCmdNextOption(NArg arg, NCmdLine cmdLine, NShellExecutionCon
} else if ((a = cmdLine.next("-e", "--regexp").orNull()) != null) {
//options.regexp = true;
return true;
} else if ((a = cmdLine.nextEntry("--expr", "--like").orNull()) != null) {
processRequireNutsOption(a, cmdLine, options);
options.expressions.add(
new ExpressionInfo()
.setPattern(a.getStringValue().get())
.setIgnoreCase(options.ignoreCase)
.setInvertMatch(options.invertMatch)
.setWord(options.word)
);
return true;
} else if ((a = cmdLine.nextFlag("--summary", "-s").orNull()) != null) {
processRequireNutsOption(a, cmdLine, options);
options.summary = a.getBooleanValue().get();
return true;
} else if ((a = cmdLine.next("-v", "--invert-match").orNull()) != null) {
if (a.isActive()) {
if (a.isNegated()) {
Expand Down Expand Up @@ -164,9 +151,41 @@ protected boolean onCmdNextOption(NArg arg, NCmdLine cmdLine, NShellExecutionCon
options.recursive = true;
options.followSymbolicLinks = true;
return true;
} else if ((a = cmdLine.next("--nuts").orNull()) != null) {
} else if (parseNutsSpecific(cmdLine, options,session)) {
return true;
} else if (cmdLine.next("-n").isPresent()) {
options.n = true;
return true;
} else {
return false;
}
}

private static void processRequireNutsOption(NArg a, NCmdLine cmdLine, GrepOptions options) {
if (!options.withNutsOptions) {
if (options.requireNutsOptions) {
cmdLine.throwUnexpectedArgument(NMsg.ofC("option can be used along with --nuts", a));
} else {
options.withNutsOptions = true;
}
}
}

private boolean parseNutsSpecific(NCmdLine cmdLine, GrepOptions options,NSession session) {
NArg a;
if ((a = cmdLine.next("--nuts").orNull()) != null) {
options.withNutsOptions = true;
return true;
}else if ((a = cmdLine.nextEntry("--expr", "--like").orNull()) != null) {
processRequireNutsOption(a, cmdLine, options);
options.expressions.add(
new ExpressionInfo()
.setPattern(a.getStringValue().get())
.setIgnoreCase(options.ignoreCase)
.setInvertMatch(options.invertMatch)
.setWord(options.word)
);
return true;
} else if ((a = cmdLine.nextEntry("--file-name").orNull()) != null) {
processRequireNutsOption(a, cmdLine, options);
options.fileNames.add(a.getStringValue().get());
Expand All @@ -183,6 +202,18 @@ protected boolean onCmdNextOption(NArg arg, NCmdLine cmdLine, NShellExecutionCon
processRequireNutsOption(a, cmdLine, options);
options.to = NLiteral.of(a).asLong().orElse(null);
return true;
} else if ((a = cmdLine.next("--@include").orNull()) != null) {
processRequireNutsOption(a, cmdLine, options);
for (String s : NPath.of(NLiteral.of(a).asString().get(), session).getLines().collect(Collectors.toList())) {
s = s.trim();
if (!s.isEmpty()) {
if (!s.startsWith("#")) {
String[] found = NCmdLine.parseSystem(s, session).get().toStringArray();
cmdLine.pushBack(found);
}
}
}
return true;
} else if ((a = cmdLine.next("--range").orNull()) != null) {
processRequireNutsOption(a, cmdLine, options);
NumberRangeList rl = NumberRangeList.parse(a.getStringValue().get());
Expand All @@ -192,9 +223,9 @@ protected boolean onCmdNextOption(NArg arg, NCmdLine cmdLine, NShellExecutionCon
options.to = r.getTo();
}
return true;
} else if ((a = cmdLine.next("--jex", "--java-exception").orNull()) != null) {
} else if ((a = cmdLine.nextFlag("--summary", "-s").orNull()) != null) {
processRequireNutsOption(a, cmdLine, options);
options.windowFilter.add(new JavaExceptionWindowFilter());
options.summary = a.getBooleanValue().get();
return true;
} else if ((a = cmdLine.next("--less").orNull()) != null) {
processRequireNutsOption(a, cmdLine, options);
Expand All @@ -208,30 +239,51 @@ protected boolean onCmdNextOption(NArg arg, NCmdLine cmdLine, NShellExecutionCon
processRequireNutsOption(a, cmdLine, options);
options.selectionStyle = NStringUtils.trimToNull(a.getStringValue().get(session));
return true;
} else if (cmdLine.next("-n").isPresent()) {
options.n = true;
} else if (parseJex(cmdLine, options)) {
return true;
} else {
return false;
}
return false;
}

private static void processRequireNutsOption(NArg a, NCmdLine cmdLine, GrepOptions options) {
if (!options.withNutsOptions) {
if (options.requireNutsOptions) {
cmdLine.throwUnexpectedArgument(NMsg.ofC(" option can be used along with --nuts", a));
private boolean parseJex(NCmdLine cmdLine, GrepOptions options) {
NArg a;
if ((a = cmdLine.next("--jex", "--java-exception").orNull()) != null) {
processRequireNutsOption(a, cmdLine, options);
options.windowFilter.add(options.lastJavaExceptionWindowFilter = new JavaExceptionWindowFilter());
return true;
} else if ((a = cmdLine.nextEntry("--jex-rows").orNull()) != null) {
processRequireNutsOption(a, cmdLine, options);
if (options.lastJavaExceptionWindowFilter != null) {
options.lastJavaExceptionWindowFilter.setRows(a.getValue().asInt().get());
} else {
options.withNutsOptions = true;
cmdLine.throwError(NMsg.ofPlain("expected --jex first"));
}
return true;
} else if ((a = cmdLine.nextEntry("--jex-include").orNull()) != null) {
processRequireNutsOption(a, cmdLine, options);
if (options.lastJavaExceptionWindowFilter != null) {
options.lastJavaExceptionWindowFilter.getJexFilters().add(new JavaExceptionWindowFilter.JexFilter(a.getValue().asString().get(), true));
} else {
cmdLine.throwError(NMsg.ofPlain("expected --jex first"));
}
return true;
} else if ((a = cmdLine.nextEntry("--jex-exclude").orNull()) != null) {
processRequireNutsOption(a, cmdLine, options);
if (options.lastJavaExceptionWindowFilter != null) {
options.lastJavaExceptionWindowFilter.getJexFilters().add(new JavaExceptionWindowFilter.JexFilter(a.getValue().asString().get(), true));
} else {
cmdLine.throwError(NMsg.ofPlain("expected --jex first"));
}
return true;
}
return false;
}

@Override
protected void onCmdExec(NCmdLine cmdLine, NShellExecutionContext context) {
GrepOptions options = context.getOptions();
GrepService service=new GrepService();
service.run(options,context.getSession());

GrepService service = new GrepService();
service.run(options, context.getSession());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.thevpc.nuts.cmdline.NCmdLine;
import net.thevpc.nuts.cmdline.NCmdLineConfigurable;
import net.thevpc.nuts.toolbox.nsh.cmds.util.NNumberedObject;
import net.thevpc.nuts.toolbox.nsh.cmds.util.filter.JavaExceptionWindowFilter;
import net.thevpc.nuts.toolbox.nsh.cmds.util.filter.WindowFilterBuilder;
import net.thevpc.nuts.toolbox.nsh.util.FileInfo;

Expand Down Expand Up @@ -36,6 +37,7 @@ class GrepOptions implements NCmdLineConfigurable {
List<FileInfo> files = new ArrayList<>();
List<ExpressionInfo> expressions = new ArrayList<>();
NSession session;
JavaExceptionWindowFilter lastJavaExceptionWindowFilter=null;

@Override
public boolean configureFirst(NCmdLine cmdLine) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.thevpc.nuts.toolbox.nsh.cmds.util;

public enum FilterResult {
ACCEPT,
REJECT,
NEUTRAL,
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ default WindowFilter<T> acceptanceResponsible(T line) {
boolean accept(T line);

WindowFilter<T> copy();

default void prepare(List<T> all, int pivotIndex) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ public WindowFilterIterator(Iterator<T> base, WindowFilter<T> filter, Integer wi

@Override
public boolean hasNext() {
while(true) {
while (true) {
T ll;
if (pushedBack != null) {
ll = pushedBack;
pushedBack=null;
pushedBack = null;
} else {
pushedBack = null;
T line;
Expand Down Expand Up @@ -91,8 +91,11 @@ public boolean hasNext() {
break;
}
}
ret = new WindowObject<>(all, pivotIndex);
return true;
filter.prepare(all, pivotIndex);
if (!all.isEmpty()) {
ret = new WindowObject<>(all, pivotIndex);
return true;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,11 @@ public int getNextWindowSize() {
return max;
}

@Override
public void prepare(List<T> all, int pivotIndex) {
for (WindowFilter<T> a : this.all) {
a.prepare(all,pivotIndex);
}
}

}
Loading

0 comments on commit dd713ec

Please sign in to comment.