Java Code Examples for io.termd.core.tty.TtyConnection#setStdinHandler()

The following examples show how to use io.termd.core.tty.TtyConnection#setStdinHandler() . 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: Shell.java    From termd with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(final TtyConnection conn, List<String> args) throws Exception {

  // Subscribe to key events and print them
  conn.setStdinHandler(new Consumer<int[]>() {
    @Override
    public void accept(int[] keys) {
      for (int key : keys) {
        conn.write(key + " pressed\n");
      }
    }
  });

  try {
    // Wait until interrupted
    new CountDownLatch(1).await();
  } finally {
    conn.setStdinHandler(null);
  }
}
 
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: Shell.java    From termd with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(TtyConnection conn, List<String> args) throws Exception {

  // Subscribe to key events and print them
  conn.setStdinHandler(keys -> {
    for (int key : keys) {
      conn.write(key + " pressed\n");
    }
  });

  try {
    // Wait until interrupted
    new CountDownLatch(1).await();
  } finally {
    conn.setStdinHandler(null);
  }
}
 
Example 4
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 5
Source File: SnakeGame.java    From termd with Apache License 2.0 4 votes vote down vote up
public Game(final TtyConnection conn) {
  this.conn = conn;

  // When user resize the screen : launch a new game
  conn.setSizeHandler(new Consumer<Vector>() {
    @Override
    public void accept(Vector size) {
      reset(size);
    }
  });

  // Ctrl-C ends the game
  conn.setEventHandler(new BiConsumer<TtyEvent, Integer>() {
    @Override
    public void accept(TtyEvent event, Integer ch) {
      switch (event) {
        case INTR:
          interrupted = true;
          conn.close();
          break;
      }
    }
  });

  // Keyboard handling
  conn.setStdinHandler(new Consumer<int[]>() {
    @Override
    public void accept(int[] keys) {
      if (keys.length == 3) {
        if (keys[0] == 27 && keys[1] == '[') {
          switch (keys[2]) {
            case 'A':
              game.direction = Direction.UP;
              break;
            case 'B':
              game.direction = Direction.DOWN;
              break;
            case 'C':
              game.direction = Direction.RIGHT;
              break;
            case 'D':
              game.direction = Direction.LEFT;
              break;
          }
        }
      }
    }
  });

  // Init current game
  reset(conn.size());
}
 
Example 6
Source File: SnakeGame.java    From termd with Apache License 2.0 4 votes vote down vote up
public Game(TtyConnection conn) {
  this.conn = conn;

  // When user resize the screen : launch a new game
  conn.setSizeHandler(this::reset);

  // Ctrl-C ends the game
  conn.setEventHandler((event, ch) -> {
    switch (event) {
      case INTR:
        interrupted = true;
        conn.close();
        break;
    }
  });

  // Keyboard handling
  conn.setStdinHandler(keys -> {
    if (keys.length == 3) {
      if (keys[0] == 27 && keys[1] == '[') {
        switch (keys[2]) {
          case 'A':
            game.direction = Direction.UP;
            break;
          case 'B':
            game.direction = Direction.DOWN;
            break;
          case 'C':
            game.direction = Direction.RIGHT;
            break;
          case 'D':
            game.direction = Direction.LEFT;
            break;
        }
      }
    }
  });

  // Init current game
  reset(conn.size());
}