Improve ls output on non terminals, add -1 and -C flags
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1736039 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Posix.java b/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Posix.java
index ca89217..8655666 100644
--- a/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Posix.java
+++ b/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Posix.java
@@ -1015,6 +1015,8 @@
"ls - list files",
"Usage: ls [OPTIONS] [PATTERNS...]",
" -? --help show help",
+ " -1 list one entry per line",
+ " -C multi-column output",
" -a list entries starting with .",
" -F append file type indicators",
" -m comma separated",
@@ -1105,7 +1107,7 @@
}
String col = colors.get(type);
boolean addSuffix = opt.isSet("F");
- if (col != null && !col.isEmpty()) {
+ if (col != null && !col.isEmpty() && session.isTty(1)) { // TODO: ability to force colors if piped
return "\033[" + col + "m" + path.toString() + "\033[m" + (addSuffix ? suffix : "") + link;
} else {
return path.toString() + (addSuffix ? suffix : "") + link;
@@ -1235,16 +1237,32 @@
.collect(Collectors.toList());
PrintStream out = System.out;
Consumer<Stream<PathEntry>> display = s -> {
+ boolean optLine = opt.isSet("1");
+ boolean optComma = opt.isSet("m");
+ boolean optLong = opt.isSet("l");
+ boolean optCol = opt.isSet("C");
+ if (!optLine && !optComma && !optLong && !optCol) {
+ if (session.isTty(1)) {
+ optCol = true;
+ }
+ else {
+ optLine = true;
+ }
+ }
+ // One entry per line
+ if (optLine) {
+ s.map(PathEntry::display).forEach(out::println);
+ }
// Comma separated list
- if (opt.isSet("m")) {
+ else if (optComma) {
out.println(s.map(PathEntry::display).collect(Collectors.joining(", ")));
}
// Long listing
- else if (opt.isSet("l")) {
+ else if (optLong) {
s.map(PathEntry::longDisplay).forEach(out::println);
}
// Column listing
- else {
+ else if (optCol) {
toColumn(session, out, s.map(PathEntry::display), opt.isSet("x"));
}
};
@@ -1276,7 +1294,7 @@
private void toColumn(CommandSession session, PrintStream out, Stream<String> ansi, boolean horizontal) {
Terminal terminal = Shell.getTerminal(session);
- int width = terminal.getWidth();
+ int width = session.isTty(1) ? terminal.getWidth() : 80;
List<AttributedString> strings = ansi.map(AttributedString::fromAnsi).collect(Collectors.toList());
if (!strings.isEmpty()) {
int max = strings.stream().mapToInt(AttributedString::columnLength).max().getAsInt();