This is an automated email from the ASF dual-hosted git repository.
aradzinski pushed a commit to branch NLPCRAFT-346
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
The following commit(s) were added to refs/heads/NLPCRAFT-346 by this push:
new a21c741 Update NCCliFileNameCompleter.java
a21c741 is described below
commit a21c7413e0abe61100b543b589f67c22ea629fce
Author: Aaron Radzinzski <[email protected]>
AuthorDate: Wed Jun 30 15:27:36 2021 -0700
Update NCCliFileNameCompleter.java
---
.../tools/cmdline/NCCliFileNameCompleter.java | 152 ++++++++++++++-------
1 file changed, 100 insertions(+), 52 deletions(-)
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCliFileNameCompleter.java
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCliFileNameCompleter.java
index 9b28018..01c3f09 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCliFileNameCompleter.java
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCliFileNameCompleter.java
@@ -29,100 +29,148 @@ import java.nio.file.*;
import java.util.*;
/**
- *
+ * File name completer heavily based on JLine3 internal implementation.
*/
-public class NCCliFileNameCompleter implements org.jline.reader.Completer
+class NCCliFileNameCompleter
{
- protected static StyleResolver resolver = Styles.lsStyle();
-
- public void complete(LineReader reader, ParsedLine commandLine, final
List<Candidate> candidates) {
- assert commandLine != null;
- assert candidates != null;
-
- String buffer = commandLine.word().substring(0,
commandLine.wordCursor());
+ private static final StyleResolver resolver = Styles.lsStyle();
- Path current;
+ /**
+ *
+ * @param reader Line reader for JLine.
+ * @param pathBuf Current path string.
+ * @param candidates List of completion candidates to fill.
+ */
+ void fillCandidates(LineReader reader, String pathBuf, final
List<Candidate> candidates) {
+ Path curPath;
String curBuf;
- String sep =
getSeparator(reader.isSet(LineReader.Option.USE_FORWARD_SLASH));
- int lastSep = buffer.lastIndexOf(sep);
+ String sep =
getSeparator(reader.isSet(LineReader.Option.USE_FORWARD_SLASH));
+ int lastSep = pathBuf.lastIndexOf(sep);
try {
if (lastSep >= 0) {
- curBuf = buffer.substring(0, lastSep + 1);
+ curBuf = pathBuf.substring(0, lastSep + 1);
+
if (curBuf.startsWith("~")) {
- if (curBuf.startsWith("~" + sep)) {
- current = getUserHome().resolve(curBuf.substring(2));
- } else {
- current =
getUserHome().getParent().resolve(curBuf.substring(1));
- }
- } else {
- current = getUserDir().resolve(curBuf);
+ if (curBuf.startsWith("~" + sep))
+ curPath = getUserHome().resolve(curBuf.substring(2));
+ else
+ curPath =
getUserHome().getParent().resolve(curBuf.substring(1));
}
- } else {
+ else
+ curPath = getUserDir().resolve(curBuf);
+ }
+ else {
curBuf = "";
- current = getUserDir();
+ curPath = getUserDir();
}
- try (DirectoryStream<Path> directory =
Files.newDirectoryStream(current, this::accept)) {
- directory.forEach(p -> {
- String value = curBuf + p.getFileName().toString();
- if (Files.isDirectory(p)) {
+
+ try (DirectoryStream<Path> dir = Files.newDirectoryStream(curPath,
this::accept)) {
+ dir.forEach(path -> {
+ String value = curBuf + path.getFileName().toString();
+
+ if (Files.isDirectory(path)) {
candidates.add(
- new Candidate(value +
(reader.isSet(LineReader.Option.AUTO_PARAM_SLASH) ? sep : ""),
- getDisplay(reader.getTerminal(), p, resolver,
sep), null, null,
-
reader.isSet(LineReader.Option.AUTO_REMOVE_SLASH) ? sep : null, null, false));
+ new Candidate(
+ value +
(reader.isSet(LineReader.Option.AUTO_PARAM_SLASH) ? sep : ""),
+ getDisplay(reader.getTerminal(), path,
resolver, sep),
+ null,
+ null,
+
reader.isSet(LineReader.Option.AUTO_REMOVE_SLASH) ? sep : null,
+ null,
+ false
+ )
+ );
} else {
- candidates.add(new Candidate(value,
getDisplay(reader.getTerminal(), p, resolver, sep), null, null, null, null,
- true));
+ candidates.add(
+ new Candidate(
+ value,
+ getDisplay(reader.getTerminal(), path,
resolver, sep),
+ null,
+ null,
+ null,
+ null,
+ true
+ )
+ );
}
});
- } catch (IOException e) {
- // Ignore
}
- } catch (Exception e) {
- // Ignore
+ catch (IOException e) {
+ // Ignore.
+ }
+ }
+ catch (Exception e) {
+ // Ignore.
}
}
- protected boolean accept(Path path) {
+ /**
+ *
+ * @param path Path to check.
+ */
+ private boolean accept(Path path) {
try {
- return !Files.isHidden(path);
- } catch (IOException e) {
+ return !Files.isHidden(path) && Files.isReadable(path);
+ }
+ catch (IOException e) {
return false;
}
}
- protected Path getUserDir() {
+ /**
+ *
+ */
+ private Path getUserDir() {
return Paths.get(System.getProperty("user.dir"));
}
- protected Path getUserHome() {
+ /**
+ *
+ */
+ private Path getUserHome() {
return Paths.get(System.getProperty("user.home"));
}
- protected String getSeparator(boolean useForwardSlash) {
+ /**
+ *
+ * @param useForwardSlash
+ */
+ private String getSeparator(boolean useForwardSlash) {
return useForwardSlash ? "/" :
getUserDir().getFileSystem().getSeparator();
}
- protected String getDisplay(Terminal terminal, Path p, StyleResolver
resolver, String separator) {
+ /**
+ *
+ * @param term JLine terminal.
+ * @param path Path.
+ * @param resolver Style resolver.
+ * @param sep Path separator.
+ */
+ private String getDisplay(Terminal term, Path path, StyleResolver
resolver, String sep) {
AttributedStringBuilder sb = new AttributedStringBuilder();
- String name = p.getFileName().toString();
+
+ String name = path.getFileName().toString();
+
int idx = name.lastIndexOf(".");
+
String type = idx != -1 ? ".*" + name.substring(idx): null;
- if (Files.isSymbolicLink(p)) {
+
+ if (Files.isSymbolicLink(path))
sb.styled(resolver.resolve(".ln"), name).append("@");
- } else if (Files.isDirectory(p)) {
- sb.styled(resolver.resolve(".di"), name).append(separator);
- } else if (Files.isExecutable(p) && !OSUtils.IS_WINDOWS) {
+ else if (Files.isDirectory(path))
+ sb.styled(resolver.resolve(".di"), name).append(sep);
+ else if (Files.isExecutable(path) && !OSUtils.IS_WINDOWS)
sb.styled(resolver.resolve(".ex"), name).append("*");
- } else if (type != null && resolver.resolve(type).getStyle() != 0) {
+ else if (type != null && resolver.resolve(type).getStyle() != 0)
sb.styled(resolver.resolve(type), name);
- } else if (Files.isRegularFile(p)) {
+ else if (Files.isRegularFile(path))
sb.styled(resolver.resolve(".fi"), name);
- } else {
+ else
sb.append(name);
- }
- return sb.toAnsi(terminal);
+
+ return sb.toAnsi(term);
}
}