1+ /*
2+ * Copyright (c) 2008-2018 Haulmont.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ package com.haulmont.cuba.cli.commands
18+
19+ import org.jline.builtins.Completers
20+ import org.jline.reader.Candidate
21+ import org.jline.reader.LineReader
22+ import org.jline.reader.ParsedLine
23+ import java.io.IOException
24+ import java.nio.file.Files
25+ import java.nio.file.Path
26+
27+ /* *
28+ * Replacement of default directories completer for normal work on windows
29+ */
30+ class DirectoriesCompleter (currentDir : Path ) : Completers.DirectoriesCompleter(currentDir) {
31+ override fun complete (reader : LineReader , commandLine : ParsedLine , candidates : MutableList <Candidate >) {
32+ val buffer = commandLine.word().substring(0 , commandLine.wordCursor())
33+
34+ val current: Path
35+ val curBuf: String
36+ val lastSep = buffer.lastIndexOf(" /" )
37+ if (lastSep >= 0 ) {
38+ curBuf = buffer.substring(0 , lastSep + 1 )
39+ current = if (curBuf.startsWith(" ~" )) {
40+ if (curBuf.startsWith(" ~/" )) {
41+ userHome.resolve(curBuf.substring(2 ))
42+ } else {
43+ userHome.parent.resolve(curBuf.substring(1 ))
44+ }
45+ } else {
46+ userDir.resolve(curBuf)
47+ }
48+ } else {
49+ curBuf = " "
50+ current = userDir
51+ }
52+ try {
53+ Files .newDirectoryStream(current) { this .accept(it) }.forEach { p ->
54+ val value = curBuf + p.fileName.toString()
55+ if (Files .isDirectory(p)) {
56+ candidates.add(Candidate (
57+ value + if (reader.isSet(LineReader .Option .AUTO_PARAM_SLASH )) " /" else " " ,
58+ getDisplay(reader.terminal, p), null , null ,
59+ if (reader.isSet(LineReader .Option .AUTO_REMOVE_SLASH )) " /" else null , null ,
60+ false ))
61+ } else {
62+ candidates.add(Candidate (value, getDisplay(reader.terminal, p), null , null , null , null , true ))
63+ }
64+ }
65+ } catch (e: IOException ) {
66+ // Ignore
67+ }
68+
69+ }
70+ }
0 commit comments