io.termd.core.readline.Readline Java Examples

The following examples show how to use io.termd.core.readline.Readline. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ReverseFunction.java    From termd with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(Readline.Interaction interaction) {
  int[] points = interaction.buffer().toArray();

  // Reverse the buffer
  for (int i = 0; i < points.length / 2; i++) {
    int temp = points[i];
    points[i] = points[points.length - 1 - i];
    points[points.length - 1 - i] = temp;
  }

  // Refresh buffer
  interaction.refresh(new LineBuffer().insert(points));

  // Resume readline
  interaction.resume();
}
 
Example #2
Source File: TermImpl.java    From arthas with Apache License 2.0 6 votes vote down vote up
public TermImpl(Keymap keymap, TtyConnection conn) {
    this.conn = conn;
    readline = new Readline(keymap);
    readline.setHistory(FileUtils.loadCommandHistory(new File(Constants.CMD_HISTORY_FILE)));
    for (Function function : readlineFunctions) {
        readline.addFunction(function);
    }

    echoHandler = new DefaultTermStdinHandler(this);
    conn.setStdinHandler(echoHandler);
    conn.setEventHandler(new EventHandler(this));
}
 
Example #3
Source File: ReverseFunction.java    From termd with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(Readline.Interaction interaction) {
  int[] points = interaction.buffer().toArray();

  // Reverse the buffer
  for (int i = 0; i < points.length / 2; i++) {
    int temp = points[i];
    points[i] = points[points.length - 1 - i];
    points[points.length - 1 - i] = temp;
  }

  // Refresh buffer
  interaction.refresh(new LineBuffer().insert(points));

  // Resume readline
  interaction.resume();
}
 
Example #4
Source File: PreviousHistory.java    From termd with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(Readline.Interaction interaction) {
  List<int[]> history = interaction.history();
  if (history.size() > 0) {
    int curr = interaction.getHistoryIndex();
    int next = curr + 1;
    if (next < history.size()) {
      if (curr == -1) {
        int[] tmp = interaction.buffer().toArray();
        interaction.data().put("abc", tmp);
      }
      int[] line = history.get(next);
      interaction.refresh(new LineBuffer().insert(line));
      interaction.setHistoryIndex(next);
    }
  }
  interaction.resume();
}
 
Example #5
Source File: TtyBridge.java    From termd with Apache License 2.0 6 votes vote down vote up
private void onNewLine(TtyConnection conn, Readline readline, String line) {
  if (processStdinListener != null) {
    processStdinListener.accept(line);
  }
  if (line == null) {
    conn.close();
    return;
  }
  PtyMaster task = new PtyMaster(line, buffer -> onStdOut(conn, buffer), empty -> doneHandler(conn, readline));
  conn.setEventHandler((event,cp) -> {
    if (event == TtyEvent.INTR) {
      task.interruptProcess();
    }
  });
  if (processListener != null) {
    processListener.accept(task);
  }
  task.start();
}
 
Example #6
Source File: NextHistory.java    From termd with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(Readline.Interaction interaction) {
  List<int[]> history = interaction.history();
  int curr = interaction.getHistoryIndex();
  if (curr >= 0) {
    int next = curr - 1;
    int[] line;
    if (next == -1) {
      line = (int[]) interaction.data().get("abc");
    } else {
      line = history.get(next);
    }
    interaction.refresh(new LineBuffer().insert(line));
    interaction.setHistoryIndex(next);
  }
  interaction.resume();
}
 
Example #7
Source File: NextHistory.java    From termd with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(Readline.Interaction interaction) {
  List<int[]> history = interaction.history();
  int curr = interaction.getHistoryIndex();
  if (curr >= 0) {
    int next = curr - 1;
    int[] line;
    if (next == -1) {
      line = (int[]) interaction.data().get("abc");
    } else {
      line = history.get(next);
    }
    interaction.refresh(new LineBuffer().insert(line));
    interaction.setHistoryIndex(next);
  }
  interaction.resume();
}
 
Example #8
Source File: PreviousHistory.java    From termd with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(Readline.Interaction interaction) {
  List<int[]> history = interaction.history();
  if (history.size() > 0) {
    int curr = interaction.getHistoryIndex();
    int next = curr + 1;
    if (next < history.size()) {
      if (curr == -1) {
        int[] tmp = interaction.buffer().toArray();
        interaction.data().put("abc", tmp);
      }
      int[] line = history.get(next);
      interaction.refresh(new LineBuffer().insert(line));
      interaction.setHistoryIndex(next);
    }
  }
  interaction.resume();
}
 
Example #9
Source File: BackwardWord.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(Readline.Interaction interaction) {
  LineBuffer buf = interaction.buffer().copy();
  buf.setCursor(findPos(buf));
  interaction.refresh(buf);
  interaction.resume();
}
 
Example #10
Source File: Complete.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(Readline.Interaction interaction) {
  Consumer<Completion> handler = interaction.completionHandler();
  if (handler != null) {
    Completion completion = new Completion(interaction);
    handler.accept(completion);
  } else {
    interaction.resume();
  }
}
 
Example #11
Source File: BackwardDeleteChar.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(Readline.Interaction interaction) {
  LineBuffer buf = interaction.buffer().copy();
  buf.delete(-1);
  interaction.refresh(buf);
  interaction.resume();
}
 
Example #12
Source File: DeleteChar.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(Readline.Interaction interaction) {
  LineBuffer buf = interaction.buffer().copy();
  buf.delete(1);
  interaction.refresh(buf);
  interaction.resume();
}
 
Example #13
Source File: KillLine.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(Readline.Interaction interaction) {
  LineBuffer buf = interaction.buffer().copy();
  buf.setSize(buf.getCursor());
  interaction.refresh(buf);
  interaction.resume();
}
 
Example #14
Source File: BackwardKillWord.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(Readline.Interaction interaction) {
  LineBuffer buf = interaction.buffer().copy();
  int cursor = BackwardWord.findPos(buf);
  buf.delete(cursor - buf.getCursor());
  interaction.refresh(buf);
  interaction.resume();
}
 
Example #15
Source File: EndOfLine.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(Readline.Interaction interaction) {
  LineBuffer buf = interaction.buffer().copy();
  buf.setCursor(buf.getSize());
  interaction.refresh(buf);
  interaction.resume();
}
 
Example #16
Source File: BackwardChar.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(Readline.Interaction interaction) {
  LineBuffer buf = interaction.buffer().copy();
  buf.moveCursor(-1);
  interaction.refresh(buf);
  interaction.resume();
}
 
Example #17
Source File: Shell.java    From termd with Apache License 2.0 5 votes vote down vote up
/**
 * Use {@link Readline} to read a user input and then process it
 *
 * @param conn the tty connection
 * @param readline the readline object
 */
public void read(final TtyConnection conn, final Readline readline) {

  // Just call readline and get a callback when line is read
  readline.readline(conn, "% ", line -> {

    // Ctrl-D
    if (line == null) {
      conn.write("logout\n").close();
      return;
    }

    Matcher matcher = splitter.matcher(line);
    if (matcher.find()) {
      String cmd = matcher.group();

      // Gather args
      List<String> args = new ArrayList<>();
      while (matcher.find()) {
        args.add(matcher.group());
      }

      try {
        new Task(conn, readline, Command.valueOf(cmd), args).start();
        return;
      } catch (IllegalArgumentException e) {
        conn.write(cmd + ": command not found\n");
      }
    }
    read(conn, readline);
  });
}
 
Example #18
Source File: Shell.java    From termd with Apache License 2.0 5 votes vote down vote up
public void accept(final TtyConnection conn) {
  InputStream inputrc = Keymap.class.getResourceAsStream("inputrc");
  Keymap keymap = new Keymap(inputrc);
  Readline readline = new Readline(keymap);
  for (io.termd.core.readline.Function function : Helper.loadServices(Thread.currentThread().getContextClassLoader(), io.termd.core.readline.Function.class)) {
    readline.addFunction(function);
  }
  conn.write("Welcome to Term.d shell example\n\n");
  read(conn, readline);
}
 
Example #19
Source File: ReadlineFunctionExample.java    From termd with Apache License 2.0 5 votes vote down vote up
public static void handle(TtyConnection conn) {

    // The reverse function simply reverse the edit buffer
    Function reverseFunction = new ReverseFunction();

    ReadlineExample.readline(
        // Bind reverse to Ctrl-g to the reverse function
        new Readline(Keymap.getDefault().bindFunction("\\C-g", "reverse")).
            addFunctions(Function.loadDefaults()).addFunction(reverseFunction),
        conn);
  }
 
Example #20
Source File: ForwardChar.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(Readline.Interaction interaction) {
  LineBuffer buf = interaction.buffer().copy();
  buf.moveCursor(1);
  interaction.refresh(buf);
  interaction.resume();
}
 
Example #21
Source File: TermImpl.java    From vertx-shell with Apache License 2.0 5 votes vote down vote up
public TermImpl(Vertx vertx, Keymap keymap, TtyConnection conn) {
  this.vertx = vertx;
  this.conn = conn;
  readline = new Readline(keymap);
  readlineFunctions.forEach(readline::addFunction);
  echoHandler = codePoints -> {
    // Echo
    echo(codePoints);
    readline.queueEvent(codePoints);
  };
  conn.setStdinHandler(echoHandler);
  conn.setEventHandler((event, key) -> {
    switch (event) {
      case INTR:
        if (interruptHandler == null || !interruptHandler.deliver(key)) {
          echo(key, '\n');
        }
        break;
      case EOF:
        // Pseudo signal
        if (stdinHandler != null) {
          stdinHandler.handle(Helper.fromCodePoints(new int[]{key}));
        } else {
          echo(key);
          readline.queueEvent(new int[]{key});
        }
        break;
      case SUSP:
        if (suspendHandler == null || !suspendHandler.deliver(key)) {
          echo(key, 'Z' - 64);
        }
        break;
    }
  });
}
 
Example #22
Source File: ReadlineFunctionExample.java    From termd with Apache License 2.0 5 votes vote down vote up
public static void handle(TtyConnection conn) {

    // The reverse function simply reverse the edit buffer
    Function reverseFunction = new ReverseFunction();

    ReadlineExample.readline(
        // Bind reverse to Ctrl-g to the reverse function
        new Readline(Keymap.getDefault().bindFunction("\\C-g", "reverse")).
            addFunctions(Functions.loadDefaults()).addFunction(reverseFunction),
        conn);
  }
 
Example #23
Source File: BackwardKillWord.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(Readline.Interaction interaction) {
  LineBuffer buf = interaction.buffer().copy();
  int cursor = BackwardWord.findPos(buf);
  buf.delete(cursor - buf.getCursor());
  interaction.refresh(buf);
  interaction.resume();
}
 
Example #24
Source File: BackwardChar.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(Readline.Interaction interaction) {
  LineBuffer buf = interaction.buffer().copy();
  buf.moveCursor(-1);
  interaction.refresh(buf);
  interaction.resume();
}
 
Example #25
Source File: EndOfLine.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(Readline.Interaction interaction) {
  LineBuffer buf = interaction.buffer().copy();
  buf.setCursor(buf.getSize());
  interaction.refresh(buf);
  interaction.resume();
}
 
Example #26
Source File: HistorySearchForward.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(Readline.Interaction interaction) {
    LineBuffer buf = interaction.buffer().copy();
    int cursor = buf.getCursor();

    List<int[]> history = interaction.history();

    int currentHistoryIndex = interaction.getHistoryIndex();

    if (currentHistoryIndex == 0 && LineBufferUtils.equals(buf, history.get(currentHistoryIndex))) {
        // 当前索引为0,说明上一个输入是一个空行里输入一个UP。所以重新设置为空行
        interaction.refresh(new LineBuffer());
        interaction.setHistoryIndex(-1);
    } else if (currentHistoryIndex > 0 && LineBufferUtils.equals(buf, history.get(currentHistoryIndex))
                    && cursor == buf.getSize()) {
        // 当前索引大于0,并且当前行的内容和当前history index一致,说明是输入多个UP翻页得到的。所以直接返回下一条history,光标设置为行尾。
        interaction.refresh(new LineBuffer().insert(history.get(currentHistoryIndex - 1)));
        interaction.setHistoryIndex(currentHistoryIndex - 1);
    } else {
        // 找到当前光标内容 和 历史记录 里匹配的项,光标仍然设置为当前的位置
        int searchStart = currentHistoryIndex - 1;
        for (int i = searchStart; i >= 0; --i) {
            int[] line = history.get(i);

            if (LineBufferUtils.equals(buf, line)) {
                continue;
            }

            if (LineBufferUtils.matchBeforeCursor(buf, line)) {
                interaction.refresh(new LineBuffer().insert(line).setCursor(cursor));
                interaction.setHistoryIndex(i);
                break;
            }
        }
    }

    interaction.resume();
}
 
Example #27
Source File: Shell.java    From termd with Apache License 2.0 5 votes vote down vote up
public void accept(final TtyConnection conn) {
  InputStream inputrc = Keymap.class.getResourceAsStream("inputrc");
  Keymap keymap = new Keymap(inputrc);
  Readline readline = new Readline(keymap);
  for (io.termd.core.readline.Function function : Helper.loadServices(Thread.currentThread().getContextClassLoader(), io.termd.core.readline.Function.class)) {
    readline.addFunction(function);
  }
  conn.write("Welcome to Term.d shell example\n\n");
  read(conn, readline);
}
 
Example #28
Source File: Undo.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(Readline.Interaction interaction) {
    LineBuffer buf = interaction.buffer().copy();
    buf.setSize(0);
    interaction.refresh(buf);
    interaction.resume();
}
 
Example #29
Source File: BackwardKillLine.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(Readline.Interaction interaction) {
    LineBuffer buf = interaction.buffer().copy();
    buf.delete(-buf.getCursor());
    interaction.refresh(buf);
    interaction.resume();
}
 
Example #30
Source File: Shell.java    From termd with Apache License 2.0 5 votes vote down vote up
/**
 * Use {@link Readline} to read a user input and then process it
 *
 * @param conn the tty connection
 * @param readline the readline object
 */
public void read(final TtyConnection conn, final Readline readline) {

  // Just call readline and get a callback when line is read
  readline.readline(conn, "% ", new Consumer<String>() {
    @Override
    public void accept(String line) {
      // Ctrl-D
      if (line == null) {
        conn.write("logout\n").close();
        return;
      }

      Matcher matcher = splitter.matcher(line);
      if (matcher.find()) {
        String cmd = matcher.group();

        // Gather args
        List<String> args = new ArrayList<String>();
        while (matcher.find()) {
          args.add(matcher.group());
        }

        try {
          new Task(conn, readline, Command.valueOf(cmd), args).start();
          return;
        } catch (IllegalArgumentException e) {
          conn.write(cmd + ": command not found\n");
        }
      }
      read(conn, readline);
    }
  });
}