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

The following examples show how to use io.termd.core.tty.TtyConnection#setEventHandler() . 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: 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 2
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 3
Source File: Screencaster.java    From termd with Apache License 2.0 5 votes vote down vote up
public Screencaster(Robot robot, TtyConnection conn) {
  this.robot = robot;
  this.conn = conn;
  conn.setEventHandler(new BiConsumer<TtyEvent, Integer>() {
    @Override
    public void accept(TtyEvent event, Integer key) {
      interrupted = true;
    }
  });
}
 
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: Plasma.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public void accept(TtyConnection conn) {
  conn.setEventHandler((event, key) -> {
    if (event == TtyEvent.INTR) {
      interrupted = true;
    }
  });
  if (conn.size() != null) {
    run(conn);
  } else {
    conn.setSizeHandler(size -> run(conn));
  }
}
 
Example 6
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 7
Source File: Screencaster.java    From termd with Apache License 2.0 4 votes vote down vote up
public Screencaster(Robot robot, TtyConnection conn) {
  this.robot = robot;
  this.conn = conn;
  conn.setEventHandler((event, key) -> interrupted = true);
}
 
Example 8
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());
}
 
Example 9
Source File: TtyBridge.java    From termd with Apache License 2.0 4 votes vote down vote up
private void doneHandler(TtyConnection conn, Readline readline) {
  conn.setEventHandler(null);
  conn.execute(() -> read(conn, readline));
}