Skip to content

Commit

Permalink
command less: manage object arguments, fixes #811
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Dec 8, 2022
1 parent 9243e6d commit 1579fc0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
46 changes: 32 additions & 14 deletions builtins/src/main/java/org/jline/builtins/Commands.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2021, the original author or authors.
* Copyright (c) 2002-2022, the original author or authors.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
Expand Down Expand Up @@ -110,33 +110,51 @@ public static void nano(Terminal terminal, PrintStream out, PrintStream err,

public static void less(Terminal terminal, InputStream in, PrintStream out, PrintStream err,
Path currentDir,
String[] argv) throws Exception {
Object[] argv) throws Exception {
less(terminal, in, out, err, currentDir, argv, null);
}

public static void less(Terminal terminal, InputStream in, PrintStream out, PrintStream err,
Path currentDir,
String[] argv,
Object[] argv,
ConfigurationPath configPath) throws Exception {
Options opt = Options.compile(Less.usage()).parse(argv);
if (opt.isSet("help")) {
throw new HelpException(opt.usage());
}
Less less = new Less(terminal, currentDir, opt, configPath);
List<Source> sources = new ArrayList<>();
if (opt.args().isEmpty()) {
opt.args().add("-");
}
for (String arg : opt.args()) {
arg = arg.startsWith("~") ? arg.replace("~", System.getProperty("user.home")) : arg;
if ("-".equals(arg)) {
sources.add(new StdInSource(in));
} else if (arg.contains("*") || arg.contains("?")) {
for (Path p: findFiles(currentDir, arg)) {
sources.add(new URLSource(p.toUri().toURL(), p.toString()));
if (opt.argObjects().isEmpty()) {
opt.argObjects().add("-");
}

for (Object o : opt.argObjects()) {
if (o instanceof String) {
String arg = (String)o;
arg = arg.startsWith("~") ? arg.replace("~", System.getProperty("user.home")) : arg;
if ("-".equals(arg)) {
sources.add(new StdInSource(in));
} else if (arg.contains("*") || arg.contains("?")) {
for (Path p : findFiles(currentDir, arg)) {
sources.add(new URLSource(p.toUri().toURL(), p.toString()));
}
} else {
sources.add(new URLSource(currentDir.resolve(arg).toUri().toURL(), arg));
}
} else if (o instanceof Source) {
sources.add((Source) o);
} else {
sources.add(new URLSource(currentDir.resolve(arg).toUri().toURL(), arg));
ByteArrayInputStream bais = null;
if (o instanceof String[]) {
bais = new ByteArrayInputStream(String.join("\n", (String[])o).getBytes());
} else if (o instanceof ByteArrayInputStream) {
bais = (ByteArrayInputStream)o;
} else if (o instanceof byte[]) {
bais = new ByteArrayInputStream((byte[])o);
}
if (bais != null) {
sources.add(new Source.InputStreamSource(bais, true, "Less"));
}
}
}
less.run(sources);
Expand Down
2 changes: 1 addition & 1 deletion console/src/main/java/org/jline/console/impl/Builtins.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void setLineReader(LineReader reader) {

private void less(CommandInput input) {
try {
Commands.less(input.terminal(), input.in(), input.out(), input.err(), workDir.get(), input.args(), configPath);
Commands.less(input.terminal(), input.in(), input.out(), input.err(), workDir.get(), input.xargs(), configPath);
} catch (Exception e) {
saveException(e);
}
Expand Down

0 comments on commit 1579fc0

Please sign in to comment.