52938
|
1 |
/*
|
|
2 |
* Copyright (c) 2002-2018, the original author or authors.
|
|
3 |
*
|
|
4 |
* This software is distributable under the BSD license. See the terms of the
|
|
5 |
* BSD license in the documentation provided with this software.
|
|
6 |
*
|
58903
|
7 |
* https://opensource.org/licenses/BSD-3-Clause
|
52938
|
8 |
*/
|
|
9 |
package jdk.internal.org.jline.reader.impl.completer;
|
|
10 |
|
|
11 |
import java.io.IOException;
|
|
12 |
import java.nio.file.Files;
|
|
13 |
import java.nio.file.Path;
|
|
14 |
import java.nio.file.Paths;
|
|
15 |
import java.util.List;
|
|
16 |
|
|
17 |
import jdk.internal.org.jline.reader.Candidate;
|
|
18 |
import jdk.internal.org.jline.reader.Completer;
|
|
19 |
import jdk.internal.org.jline.reader.LineReader;
|
|
20 |
import jdk.internal.org.jline.reader.LineReader.Option;
|
|
21 |
import jdk.internal.org.jline.reader.ParsedLine;
|
|
22 |
import jdk.internal.org.jline.terminal.Terminal;
|
|
23 |
import jdk.internal.org.jline.utils.AttributedStringBuilder;
|
|
24 |
import jdk.internal.org.jline.utils.AttributedStyle;
|
|
25 |
|
|
26 |
/**
|
|
27 |
* A file name completer takes the buffer and issues a list of
|
|
28 |
* potential completions.
|
|
29 |
* <p>
|
|
30 |
* This completer tries to behave as similar as possible to
|
|
31 |
* <i>bash</i>'s file name completion (using GNU readline)
|
|
32 |
* with the following exceptions:
|
|
33 |
* <ul>
|
|
34 |
* <li>Candidates that are directories will end with "/"</li>
|
|
35 |
* <li>Wildcard regular expressions are not evaluated or replaced</li>
|
|
36 |
* <li>The "~" character can be used to represent the user's home,
|
|
37 |
* but it cannot complete to other users' homes, since java does
|
|
38 |
* not provide any way of determining that easily</li>
|
|
39 |
* </ul>
|
|
40 |
*
|
|
41 |
* @author <a href="mailto:mwp1@cornell.edu">Marc Prud'hommeaux</a>
|
|
42 |
* @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
|
|
43 |
* @since 2.3
|
|
44 |
* @deprecated use <code>org.jline.builtins.Completers$FileNameCompleter</code> instead
|
|
45 |
*/
|
|
46 |
@Deprecated
|
|
47 |
public class FileNameCompleter implements Completer
|
|
48 |
{
|
|
49 |
|
|
50 |
public void complete(LineReader reader, ParsedLine commandLine, final List<Candidate> candidates) {
|
|
51 |
assert commandLine != null;
|
|
52 |
assert candidates != null;
|
|
53 |
|
|
54 |
String buffer = commandLine.word().substring(0, commandLine.wordCursor());
|
|
55 |
|
|
56 |
Path current;
|
|
57 |
String curBuf;
|
|
58 |
String sep = getUserDir().getFileSystem().getSeparator();
|
|
59 |
int lastSep = buffer.lastIndexOf(sep);
|
|
60 |
if (lastSep >= 0) {
|
|
61 |
curBuf = buffer.substring(0, lastSep + 1);
|
|
62 |
if (curBuf.startsWith("~")) {
|
|
63 |
if (curBuf.startsWith("~" + sep)) {
|
|
64 |
current = getUserHome().resolve(curBuf.substring(2));
|
|
65 |
} else {
|
|
66 |
current = getUserHome().getParent().resolve(curBuf.substring(1));
|
|
67 |
}
|
|
68 |
} else {
|
|
69 |
current = getUserDir().resolve(curBuf);
|
|
70 |
}
|
|
71 |
} else {
|
|
72 |
curBuf = "";
|
|
73 |
current = getUserDir();
|
|
74 |
}
|
|
75 |
try {
|
|
76 |
Files.newDirectoryStream(current, this::accept).forEach(p -> {
|
|
77 |
String value = curBuf + p.getFileName().toString();
|
|
78 |
if (Files.isDirectory(p)) {
|
|
79 |
candidates.add(new Candidate(
|
|
80 |
value + (reader.isSet(Option.AUTO_PARAM_SLASH) ? sep : ""),
|
|
81 |
getDisplay(reader.getTerminal(), p),
|
|
82 |
null, null,
|
|
83 |
reader.isSet(Option.AUTO_REMOVE_SLASH) ? sep : null,
|
|
84 |
null,
|
|
85 |
false));
|
|
86 |
} else {
|
|
87 |
candidates.add(new Candidate(value, getDisplay(reader.getTerminal(), p),
|
|
88 |
null, null, null, null, true));
|
|
89 |
}
|
|
90 |
});
|
|
91 |
} catch (IOException e) {
|
|
92 |
// Ignore
|
|
93 |
}
|
|
94 |
}
|
|
95 |
|
|
96 |
protected boolean accept(Path path) {
|
|
97 |
try {
|
|
98 |
return !Files.isHidden(path);
|
|
99 |
} catch (IOException e) {
|
|
100 |
return false;
|
|
101 |
}
|
|
102 |
}
|
|
103 |
|
|
104 |
protected Path getUserDir() {
|
|
105 |
return Paths.get(System.getProperty("user.dir"));
|
|
106 |
}
|
|
107 |
|
|
108 |
protected Path getUserHome() {
|
|
109 |
return Paths.get(System.getProperty("user.home"));
|
|
110 |
}
|
|
111 |
|
|
112 |
protected String getDisplay(Terminal terminal, Path p) {
|
|
113 |
// TODO: use $LS_COLORS for output
|
|
114 |
String name = p.getFileName().toString();
|
|
115 |
if (Files.isDirectory(p)) {
|
|
116 |
AttributedStringBuilder sb = new AttributedStringBuilder();
|
|
117 |
sb.styled(AttributedStyle.BOLD.foreground(AttributedStyle.RED), name);
|
|
118 |
sb.append("/");
|
|
119 |
name = sb.toAnsi(terminal);
|
|
120 |
} else if (Files.isSymbolicLink(p)) {
|
|
121 |
AttributedStringBuilder sb = new AttributedStringBuilder();
|
|
122 |
sb.styled(AttributedStyle.BOLD.foreground(AttributedStyle.RED), name);
|
|
123 |
sb.append("@");
|
|
124 |
name = sb.toAnsi(terminal);
|
|
125 |
}
|
|
126 |
return name;
|
|
127 |
}
|
|
128 |
|
|
129 |
}
|