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

The following examples show how to use io.termd.core.tty.TtyConnection#setSizeHandler() . 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(TtyConnection conn, List<String> args) throws Exception {
  conn.write("Current window size " + conn.size() + ", try resize it\n");

  // Refresh the screen with the new size
  conn.setSizeHandler(size -> {
    conn.write("Window resized " + size + "\n");
  });

  try {
    // Wait until interrupted
    new CountDownLatch(1).await();
  } finally {
    conn.setSizeHandler(null);
  }
}
 
Example 2
Source File: SnakeGame.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public void accept(TtyConnection conn) {
  if (conn.size() != null) {
    new Game(conn).execute();
  } else {
    conn.setSizeHandler(size -> new Game(conn).execute());
  }
}
 
Example 3
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 4
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 5
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());
}