Skip to content

Commit 1579fc0

Browse files
committedDec 8, 2022
command less: manage object arguments, fixes #811
1 parent 9243e6d commit 1579fc0

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed
 

‎builtins/src/main/java/org/jline/builtins/Commands.java

+32-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002-2021, the original author or authors.
2+
* Copyright (c) 2002-2022, the original author or authors.
33
*
44
* This software is distributable under the BSD license. See the terms of the
55
* BSD license in the documentation provided with this software.
@@ -110,33 +110,51 @@ public static void nano(Terminal terminal, PrintStream out, PrintStream err,
110110

111111
public static void less(Terminal terminal, InputStream in, PrintStream out, PrintStream err,
112112
Path currentDir,
113-
String[] argv) throws Exception {
113+
Object[] argv) throws Exception {
114114
less(terminal, in, out, err, currentDir, argv, null);
115115
}
116116

117117
public static void less(Terminal terminal, InputStream in, PrintStream out, PrintStream err,
118118
Path currentDir,
119-
String[] argv,
119+
Object[] argv,
120120
ConfigurationPath configPath) throws Exception {
121121
Options opt = Options.compile(Less.usage()).parse(argv);
122122
if (opt.isSet("help")) {
123123
throw new HelpException(opt.usage());
124124
}
125125
Less less = new Less(terminal, currentDir, opt, configPath);
126126
List<Source> sources = new ArrayList<>();
127-
if (opt.args().isEmpty()) {
128-
opt.args().add("-");
129-
}
130-
for (String arg : opt.args()) {
131-
arg = arg.startsWith("~") ? arg.replace("~", System.getProperty("user.home")) : arg;
132-
if ("-".equals(arg)) {
133-
sources.add(new StdInSource(in));
134-
} else if (arg.contains("*") || arg.contains("?")) {
135-
for (Path p: findFiles(currentDir, arg)) {
136-
sources.add(new URLSource(p.toUri().toURL(), p.toString()));
127+
if (opt.argObjects().isEmpty()) {
128+
opt.argObjects().add("-");
129+
}
130+
131+
for (Object o : opt.argObjects()) {
132+
if (o instanceof String) {
133+
String arg = (String)o;
134+
arg = arg.startsWith("~") ? arg.replace("~", System.getProperty("user.home")) : arg;
135+
if ("-".equals(arg)) {
136+
sources.add(new StdInSource(in));
137+
} else if (arg.contains("*") || arg.contains("?")) {
138+
for (Path p : findFiles(currentDir, arg)) {
139+
sources.add(new URLSource(p.toUri().toURL(), p.toString()));
140+
}
141+
} else {
142+
sources.add(new URLSource(currentDir.resolve(arg).toUri().toURL(), arg));
137143
}
144+
} else if (o instanceof Source) {
145+
sources.add((Source) o);
138146
} else {
139-
sources.add(new URLSource(currentDir.resolve(arg).toUri().toURL(), arg));
147+
ByteArrayInputStream bais = null;
148+
if (o instanceof String[]) {
149+
bais = new ByteArrayInputStream(String.join("\n", (String[])o).getBytes());
150+
} else if (o instanceof ByteArrayInputStream) {
151+
bais = (ByteArrayInputStream)o;
152+
} else if (o instanceof byte[]) {
153+
bais = new ByteArrayInputStream((byte[])o);
154+
}
155+
if (bais != null) {
156+
sources.add(new Source.InputStreamSource(bais, true, "Less"));
157+
}
140158
}
141159
}
142160
less.run(sources);

‎console/src/main/java/org/jline/console/impl/Builtins.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public void setLineReader(LineReader reader) {
109109

110110
private void less(CommandInput input) {
111111
try {
112-
Commands.less(input.terminal(), input.in(), input.out(), input.err(), workDir.get(), input.args(), configPath);
112+
Commands.less(input.terminal(), input.in(), input.out(), input.err(), workDir.get(), input.xargs(), configPath);
113113
} catch (Exception e) {
114114
saveException(e);
115115
}

0 commit comments

Comments
 (0)
Please sign in to comment.