Package org.crsh.cli.spi

Examples of org.crsh.cli.spi.Completion


              log.fine("No process can handle the key event");
            }
          } else if (type.getAsString().equals("complete")) {
            String prefix = event.get("prefix").getAsString();
            CompletionMatch completion = session.shell.complete(prefix);
            Completion completions = completion.getValue();
            Delimiter delimiter = completion.getDelimiter();
            StringBuilder sb = new StringBuilder();
            List<String> values = new ArrayList<String>();
            try {
              if (completions.getSize() == 1) {
                String value = completions.getValues().iterator().next();
                delimiter.escape(value, sb);
                if (completions.get(value)) {
                  sb.append(delimiter.getValue());
                }
                values.add(sb.toString());
              }
              else {
                String commonCompletion = Utils.findLongestCommonPrefix(completions.getValues());
                if (commonCompletion.length() > 0) {
                  delimiter.escape(commonCompletion, sb);
                  values.add(sb.toString());
                }
                else {
View Full Code Here


  }

  private void complete(CharSequence prefix) {
    log.log(Level.FINE, "About to get completions for " + prefix);
    CompletionMatch completion = shell.complete(prefix.toString());
    Completion completions = completion.getValue();
    log.log(Level.FINE, "Completions for " + prefix + " are " + completions);

    //
    Delimiter delimiter = completion.getDelimiter();

    try {
      // Try to find the greatest prefix among all the results
      if (completions.getSize() == 0) {
        // Do nothing
      } else if (completions.getSize() == 1) {
        Map.Entry<String, Boolean> entry = completions.iterator().next();
        Appendable buffer = term.getDirectBuffer();
        String insert = entry.getKey();
        term.getDirectBuffer().append(delimiter.escape(insert));
        if (entry.getValue()) {
          buffer.append(completion.getDelimiter().getValue());
        }
      } else {
        String commonCompletion = Utils.findLongestCommonPrefix(completions.getValues());

        // Format stuff
        int width = term.getWidth();

        //
        String completionPrefix = completions.getPrefix();

        // Get the max length
        int max = 0;
        for (String suffix : completions.getValues()) {
          max = Math.max(max, completionPrefix.length() + suffix.length());
        }

        // Separator : use two whitespace like in BASH
        max += 2;

        //
        StringBuilder sb = new StringBuilder().append('\n');
        if (max < width) {
          int columns = width / max;
          int index = 0;
          for (String suffix : completions.getValues()) {
            sb.append(completionPrefix).append(suffix);
            for (int l = completionPrefix.length() + suffix.length();l < max;l++) {
              sb.append(' ');
            }
            if (++index >= columns) {
              index = 0;
              sb.append('\n');
            }
          }
          if (index > 0) {
            sb.append('\n');
          }
        } else {
          for (Iterator<String> i = completions.getValues().iterator();i.hasNext();) {
            String suffix = i.next();
            sb.append(commonCompletion).append(suffix);
            if (i.hasNext()) {
              sb.append('\n');
            }
View Full Code Here

public class CompleteTestCase extends AbstractShellTestCase {

  public void testCommandImplementingCompleter() {
    lifeCycle.bindClass("complete", Commands.Complete.class);
    CompletionMatch completionMatch = assertComplete("complete foo");
    Completion completion = completionMatch.getValue();
    assertEquals("foo", completion.getPrefix());
    assertEquals(Collections.singleton("bar"), completion.getValues());
    assertTrue(completion.get("bar"));
  }
View Full Code Here

  public void testSessionAccess() {
    lifeCycle.bindClass("complete", Commands.CompleteWithSession.class);
    session.put("juu", "juu_value");
    CompletionMatch completionMatch = assertComplete("complete foo");
    Completion completion = completionMatch.getValue();
    assertEquals("foo", completion.getPrefix());
    assertEquals(Collections.singleton("juu_value"), completion.getValues());
    assertTrue(completion.get("juu_value"));
  }
View Full Code Here

    assertInternalError("foo");
  }

  public void testComplete() {
    CompletionMatch match = assertComplete("java_");
    Completion completion = match.getValue();
    assertEquals(1, completion.getSize());
    Map.Entry<String, Boolean> entry = completion.iterator().next();
    assertEquals("command", entry.getKey());
    assertTrue(entry.getValue());
  }
View Full Code Here

    assertCompletion("foo" + sep +"bar", Completion.create(sep, false));
    assertCompletion("foo" + sep +"bar" + sep, Completion.create());
  }

  private void assertCompletion(String path, Completion expected) throws Exception {
    Completion completions = completer.complete (null, path);
    assertEquals(expected, completions);
  }
View Full Code Here

    //
    ServerAutomaton server = new ServerAutomaton(serverOOS, serverOIS);
    CompletionMatch completion = server.complete("pref");
    assertEquals(Delimiter.DOUBLE_QUOTE, completion.getDelimiter());
    Completion value = completion.getValue();
    assertEquals("pref", value.getPrefix());
    assertEquals(1, value.getSize());
    assertEquals(Collections.singleton("ix"), value.getValues());
    assertEquals(Boolean.TRUE, value.get("ix"));

    //
    t.interrupt();
    assertJoin(t);
  }
View Full Code Here

TOP

Related Classes of org.crsh.cli.spi.Completion

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.