Skip to content

Commit

Permalink
Improved script file detection and printing
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Jan 28, 2020
1 parent 09f14e5 commit 33dad4a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 24 deletions.
43 changes: 25 additions & 18 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -310,29 +310,36 @@ private class ScriptFile {

@SuppressWarnings("unchecked")
public ScriptFile(String command, String cmdLine, String[] args) {
this.script = new File(command);
this.cmdLine = cmdLine;
if (script.exists()) {
scriptExtension(command);
} else if (engine.hasVariable(VAR_PATH)) {
boolean found = false;
for (String p: (List<String>)engine.get(VAR_PATH)) {
for (String e : scriptExtensions()) {
String file = command + "." + e;
Path path = Paths.get(p, file);
if (path.toFile().exists()) {
script = path.toFile();
scriptExtension(command);
found = true;
if (!command.matches("[a-zA-Z0-9_-]+")) {
return;
}
try {
this.script = new File(command);
this.cmdLine = cmdLine;
if (script.exists()) {
scriptExtension(command);
} else if (engine.hasVariable(VAR_PATH)) {
boolean found = false;
for (String p : (List<String>) engine.get(VAR_PATH)) {
for (String e : scriptExtensions()) {
String file = command + "." + e;
Path path = Paths.get(p, file);
if (path.toFile().exists()) {
script = path.toFile();
scriptExtension(command);
found = true;
break;
}
}
if (found) {
break;
}
}
if (found) {
break;
}
}
doArgs(args);
} catch (Exception e) {

}
doArgs(args);
}

public ScriptFile(File script, String cmdLine, String[] args) {
Expand Down
21 changes: 15 additions & 6 deletions groovy/src/main/java/org/jline/script/GroovyEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,9 @@ private List<AttributedString> internalHighlight(Map<String, Object> options, Ob
// do nothing
} else if (obj instanceof Map) {
out = highlightMap((Map<String, Object>)obj, width);
} else if (obj instanceof Collection<?>) {
Collection<?> collection = (Collection<?>)obj;
} else if (obj instanceof Collection<?> || obj instanceof Object[]) {
Collection<?> collection = obj instanceof Collection<?> ? (Collection<?>)obj
: Arrays.asList((Object[])obj);
if (!collection.isEmpty()) {
if (collection.size() == 1) {
Object elem = collection.iterator().next();
Expand Down Expand Up @@ -359,11 +360,12 @@ private List<AttributedString> internalHighlight(Map<String, Object> options, Ob
}
out.add(asb2.toAttributedString());
}
} else if (elem instanceof Collection) {
} else if (elem instanceof Collection || elem instanceof Object[]) {
boolean isCollection = elem instanceof Collection;
List<Integer> columns = new ArrayList<>();
for (Object o : collection) {
List<Object> inner = new ArrayList<>();
inner.addAll((Collection<?>)o);
inner.addAll(isCollection ? (Collection<?>)o : Arrays.asList((Object[])o));
for (int i = 0; i < inner.size(); i++) {
int len1 = Utils.toString(inner.get(i)).length() + 1;
if (columns.size() <= i) {
Expand All @@ -383,7 +385,7 @@ private List<AttributedString> internalHighlight(Map<String, Object> options, Ob
row++;
}
List<Object> inner = new ArrayList<>();
inner.addAll((Collection<?>)o);
inner.addAll(isCollection ? (Collection<?>)o : Arrays.asList((Object[])o));
for (int i = 0; i < inner.size(); i++) {
asb.append(Utils.toString(inner.get(i)));
asb.append("\t");
Expand All @@ -394,8 +396,15 @@ private List<AttributedString> internalHighlight(Map<String, Object> options, Ob
out.add(asb.toAttributedString());
}
} else {
Integer row = 0;
Integer tabsize = ((Integer)collection.size()).toString().length() + 1;
for (Object o: collection) {
AttributedStringBuilder asb = new AttributedStringBuilder();
AttributedStringBuilder asb = new AttributedStringBuilder().tabs(tabsize);
if (rownum) {
asb.append(row.toString(), AttributedStyle.DEFAULT.foreground(AttributedStyle.BLUE + AttributedStyle.BRIGHT));
asb.append("\t");
row++;
}
asb.append(Utils.toString(o));
if (asb.columnLength() > width) {
asb.setLength(width);
Expand Down

0 comments on commit 33dad4a

Please sign in to comment.