6

我想知道如何实现ArgumentCompleter这样一个,如果我完成一个完整且有效的命令,那么它将开始为一个新命令完成制表符。

我会假设它可以像这样构建:

final ConsoleReader consoleReader = new ConsoleReader()

final ArgumentCompleter cyclicalArgument = new ArgumentCompleter();
cyclicalArgument.getCompleters().addAll(Arrays.asList(
        new StringsCompleter("foo"), 
        new StringsCompleter("bar"), 
        cyclicalArgument));

consoleReader.addCompleter(cyclicalArgument);
consoleReader.readLine();

但是现在这在标签完成第一个后停止工作foo bar

有没有足够熟悉图书馆的人告诉我我将如何实现这个?还是有一种我想念的已知方法来做到这一点?这也是使用 JLine2。

4

1 回答 1

4

那是一项艰巨的任务:-)

它由您正在使用的完成者处理。完成者的complete()方法只能用于搜索最后一个空白之后的内容。

如果你看一下FileNameCompleter图书馆的例子:这根本没有完成,所以你会发现没有完成,因为完成者搜索<input1> <input2>而不仅仅是<input2>:-)

您将必须自己实现能够找到 input2 的完成程序。

此外,CompletionHandler必须将您找到的内容附加到您已经输入的内容中。

这是更改默认值的基本实现FileNameCompleter

  protected int matchFiles(final String buffer, final String translated, final File[] files,
         final List<CharSequence> candidates) {
      // THIS IS NEW
      String[] allWords = translated.split(" ");
      String lastWord = allWords[allWords.length - 1];
      // the lastWord is used when searching the files now
      // ---

      if (files == null) {
         return -1;
      }

      int matches = 0;

      // first pass: just count the matches
      for (File file : files) {
         if (file.getAbsolutePath().startsWith(lastWord)) {
            matches++;
         }
      }
      for (File file : files) {
         if (file.getAbsolutePath().startsWith(lastWord)) {
            CharSequence name = file.getName() + (matches == 1 && file.isDirectory() ? this.separator() : " ");
            candidates.add(this.render(file, name).toString());
         }
      }

      final int index = buffer.lastIndexOf(this.separator());

      return index + this.separator().length();
   }

这里是complete()更改CompletionHandler默认值的方法CandidateListCompletionHandler

  @Override
   public boolean complete(final ConsoleReader reader, final List<CharSequence> candidates, final int pos)
         throws IOException {
      CursorBuffer buf = reader.getCursorBuffer();

      // THIS IS NEW
      String[] allWords = buf.toString().split(" ");
      String firstWords = "";
      if (allWords.length > 1) {
         for (int i = 0; i < allWords.length - 1; i++) {
            firstWords += allWords[i] + " ";
         }
      }
      //-----

      // if there is only one completion, then fill in the buffer
      if (candidates.size() == 1) {
         String value = Ansi.stripAnsi(candidates.get(0).toString());

         if (buf.cursor == buf.buffer.length() && this.printSpaceAfterFullCompletion && !value.endsWith(" ")) {
            value += " ";
         }

         // fail if the only candidate is the same as the current buffer
         if (value.equals(buf.toString())) {
            return false;
         }

         CandidateListCompletionHandler.setBuffer(reader, firstWords + " " + value, pos);

         return true;
      } else if (candidates.size() > 1) {
         String value = this.getUnambiguousCompletions(candidates);
         CandidateListCompletionHandler.setBuffer(reader, value, pos);
      }

      CandidateListCompletionHandler.printCandidates(reader, candidates);

      // redraw the current console buffer
      reader.drawLine();

      return true;
   }
于 2016-02-10T18:55:21.040 回答