io.termd.core.util.Vector Java Examples

The following examples show how to use io.termd.core.util.Vector. 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: SnakeGame.java    From termd with Apache License 2.0 6 votes vote down vote up
GameState(int width, int height, int size) {
  this.width = width;
  this.height = height;
  tiles = new HashSet<Vector>();
  while (size > 0) {
    int x = new Random().nextInt(width);
    int y = new Random().nextInt(height);
    Vector tile = new Vector(x, y);
    if (tiles.add(tile)) {
      size--;
    }
  }
  snake.addFirst(new Vector(0, 0));
  snake.addFirst(new Vector(1, 0));
  snake.addFirst(new Vector(2, 0));
  snake.addFirst(new Vector(3, 0));
  direction = Direction.RIGHT;
}
 
Example #2
Source File: TtyTestBase.java    From termd with Apache License 2.0 6 votes vote down vote up
@Test
public void testResize() throws Exception {
  server(new Consumer<TtyConnection>() {
    @Override
    public void accept(final TtyConnection conn) {
      conn.setSizeHandler(new Consumer<Vector>() {
        @Override
        public void accept(Vector size) {
          assertEquals(40, conn.size().x());
          assertEquals(12, conn.size().y());
          testComplete();
        }
      });
    }
  });

  assertConnect();
  resize(40, 12);
  await();
}
 
Example #3
Source File: ReadlineTest.java    From termd with Apache License 2.0 6 votes vote down vote up
@Test
public void testPreserveOriginalHandlers() {
  TestTerm term = new TestTerm(this);
  Consumer<int[]> readHandler = new Consumer<int[]>() {
    @Override
    public void accept(int[] ints) {
      // no op
    }
  };
  Consumer<Vector> sizeHandler = new Consumer<Vector>() {
    @Override
    public void accept(Vector vector) {
      // no op
    }
  };
  term.readHandler = readHandler;
  term.sizeHandler = sizeHandler;
  term.readlineComplete();
  assertFalse(term.readHandler == readHandler);
  assertFalse(term.sizeHandler == sizeHandler);
  term.read('\r');
  assertEquals(term.readHandler, readHandler);
  assertEquals(term.sizeHandler, sizeHandler);
}
 
Example #4
Source File: SnakeGame.java    From termd with Apache License 2.0 6 votes vote down vote up
GameState(int width, int height, int size) {
  this.width = width;
  this.height = height;
  tiles = new HashSet<>();
  while (size > 0) {
    int x = new Random().nextInt(width);
    int y = new Random().nextInt(height);
    Vector tile = new Vector(x, y);
    if (tiles.add(tile)) {
      size--;
    }
  }
  snake.addFirst(new Vector(0, 0));
  snake.addFirst(new Vector(1, 0));
  snake.addFirst(new Vector(2, 0));
  snake.addFirst(new Vector(3, 0));
  direction = Direction.RIGHT;
}
 
Example #5
Source File: Readline.java    From termd with Apache License 2.0 6 votes vote down vote up
private void install() {
  prevReadHandler = conn.getStdinHandler();
  prevSizeHandler = conn.getSizeHandler();
  prevEventHandler = conn.getEventHandler();
  conn.setStdinHandler(new Consumer<int[]>() {
    @Override
    public void accept(int[] data) {
      synchronized (Readline.this) {
        decoder.append(data);
      }
      deliver();
    }
  });
  size = conn.size();
  conn.setSizeHandler(new Consumer<Vector>() {
    @Override
    public void accept(Vector dim) {
      if (size != null) {
        // Not supported for now
        // interaction.resize(size.width(), dim.width());
      }
      size = dim;
    }
  });
  conn.setEventHandler(null);
}
 
Example #6
Source File: SockJSTtyConnection.java    From vertx-shell with Apache License 2.0 5 votes vote down vote up
private static Vector getInitialSize(SockJSSocket socket) {
  QueryStringDecoder decoder = new QueryStringDecoder(socket.uri());
  Map<String, List<String>> params = decoder.parameters();
  try {
    int cols = getParamValue(params, "cols", 80);
    int rows = getParamValue(params, "rows", 24);
    return new Vector(cols, rows);
  } catch (Exception e) {
    return new Vector(80, 24);
  }
}
 
Example #7
Source File: HttpTtyConnection.java    From termd with Apache License 2.0 5 votes vote down vote up
public HttpTtyConnection(Charset charset, Vector size) {
  this.charset = charset;
  this.size = size;
  this.eventDecoder = new TtyEventDecoder(3, 26, 4);
  this.decoder = new BinaryDecoder(512, charset, eventDecoder);
  this.stdout = new TtyOutputMode(new BinaryEncoder(charset, new Consumer<byte[]>() {
    @Override
    public void accept(byte[] bytes) {
      write(bytes);
    }
  }));
}
 
Example #8
Source File: TelnetTtyConnection.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
protected void onSize(int width, int height) {
  this.size = new Vector(width, height);
  if (sizeHandler != null) {
    sizeHandler.accept(size);
  }
}
 
Example #9
Source File: TelnetTtyConnection.java    From termd with Apache License 2.0 5 votes vote down vote up
public TelnetTtyConnection(boolean inBinary, boolean outBinary, Charset charset, Consumer<TtyConnection> handler) {
  this.charset = charset;
  this.inBinary = inBinary;
  this.outBinary = outBinary;
  this.handler = handler;
  this.size = new Vector();
  this.decoder = new BinaryDecoder(512, TelnetCharset.INSTANCE, readBuffer);
  this.encoder = new BinaryEncoder(charset, new Consumer<byte[]>() {
    @Override
    public void accept(byte[] data) {
      conn.write(data);
    }
  });
  this.stdout = new TtyOutputMode(encoder);
}
 
Example #10
Source File: LineBufferTest.java    From termd with Apache License 2.0 5 votes vote down vote up
@Test
public void testCursorPosition() {
  LineBuffer buffer = new LineBuffer();
  buffer.insert('a', 'b', 'c');
  assertEquals(new Vector(3, 0), buffer.getCursorPosition(4));
  assertEquals(new Vector(0, 1), buffer.getCursorPosition(3));
  assertEquals(new Vector(1, 1), buffer.getCursorPosition(2));
  assertEquals(new Vector(0, 3), buffer.getCursorPosition(1));
}
 
Example #11
Source File: LineBufferTest.java    From termd with Apache License 2.0 5 votes vote down vote up
@Test
public void testCursorPositionWithNewLine() {
  LineBuffer buffer = new LineBuffer();
  buffer.insert('a', 'b', '\n', 'c');
  assertEquals(new Vector(1, 1), buffer.getCursorPosition(4));
  assertEquals(new Vector(1, 1), buffer.getCursorPosition(3));
  assertEquals(new Vector(1, 2), buffer.getCursorPosition(2));
  assertEquals(new Vector(0, 4), buffer.getCursorPosition(1));
}
 
Example #12
Source File: LineBufferTest.java    From termd with Apache License 2.0 5 votes vote down vote up
public void testCursorPositionWithCR() {
  LineBuffer buffer = new LineBuffer();
  buffer.insert('a', '\r', 'b', 'c');
  assertEquals(new Vector(2, 0), buffer.getCursorPosition(4));
  assertEquals(new Vector(2, 0), buffer.getCursorPosition(3));
  assertEquals(new Vector(0, 1), buffer.getCursorPosition(2));
  assertEquals(new Vector(0, 3), buffer.getCursorPosition(1));
}
 
Example #13
Source File: LineBufferTest.java    From termd with Apache License 2.0 5 votes vote down vote up
public void testCursorControlChar() {
  LineBuffer buffer = new LineBuffer();
  buffer.insert('a', '\t', 'c');
  assertEquals(new Vector(2, 0), buffer.getCursorPosition(4));
  assertEquals(new Vector(2, 0), buffer.getCursorPosition(3));
  assertEquals(new Vector(0, 1), buffer.getCursorPosition(2));
  assertEquals(new Vector(0, 2), buffer.getCursorPosition(1));
}
 
Example #14
Source File: LineBufferTest.java    From termd with Apache License 2.0 5 votes vote down vote up
public void testCursorInvisibleChar() {
  LineBuffer buffer = new LineBuffer();
  buffer.insert('a', '\0', 'c');
  assertEquals(new Vector(2, 0), buffer.getCursorPosition(4));
  assertEquals(new Vector(2, 0), buffer.getCursorPosition(3));
  assertEquals(new Vector(0, 1), buffer.getCursorPosition(2));
  assertEquals(new Vector(0, 2), buffer.getCursorPosition(1));
}
 
Example #15
Source File: LineBufferTest.java    From termd with Apache License 2.0 5 votes vote down vote up
public void testCursorPositionWithMultiCell1() {
  LineBuffer buffer = new LineBuffer();
  buffer.insert('한', 'b');
  assertEquals(new Vector(3, 0), buffer.getCursorPosition(4));
  assertEquals(new Vector(0, 1), buffer.getCursorPosition(3));
  assertEquals(new Vector(1, 1), buffer.getCursorPosition(2));
  try {
    buffer.getCursorPosition(1);
    fail();
  } catch (UnsupportedOperationException ignore) {
  }
}
 
Example #16
Source File: LineBufferTest.java    From termd with Apache License 2.0 5 votes vote down vote up
public void testCursorPositionWithMultiCell2() {
  LineBuffer buffer = new LineBuffer();
  buffer.insert('a', '한');
  assertEquals(new Vector(3, 0), buffer.getCursorPosition(4));
  assertEquals(new Vector(0, 1), buffer.getCursorPosition(3));
  assertEquals(new Vector(0, 2), buffer.getCursorPosition(2));
  try {
    buffer.getCursorPosition(1);
    fail();
  } catch (UnsupportedOperationException ignore) {
  }
}
 
Example #17
Source File: TestTerm.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public void setSizeHandler(Consumer<Vector> handler) {
  sizeHandler = handler;
  if (handler != null) {
    handler.accept(new Vector(40, 20));
  }
}
 
Example #18
Source File: TestTerm.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public void setSizeHandler(Consumer<Vector> handler) {
  sizeHandler = handler;
  if (handler != null) {
    handler.accept(new Vector(40, 20));
  }
}
 
Example #19
Source File: Screencaster.java    From termd with Apache License 2.0 5 votes vote down vote up
private void broadcast() {
  if (interrupted) {
    conn.close();
    return;
  }
  BufferedImage capture = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
  Vector size = conn.size();
  Image temp = capture.getScaledInstance(size.x(), size.y(), Image.SCALE_SMOOTH);
  BufferedImage scaled = new BufferedImage(size.x(), size.y(), BufferedImage.TYPE_INT_ARGB);
  Graphics2D g2d = scaled.createGraphics();
  g2d.drawImage(temp, 0, 0, null);
  g2d.dispose();
  StringBuilder sb = new StringBuilder();
  for (int y = 0; y < size.y(); y++) {
    sb.append("\033[").append(y + 1).append(";1H");
    for (int x = 0; x < size.x(); x++) {
      Color pixel = new Color(scaled.getRGB(x, y));
      int r = pixel.getRed();
      int g = pixel.getGreen();
      int b = pixel.getBlue();
      double grey = (r + g + b) / 3.0;
      if (grey < 51) {
        sb.append('\u2588');
      } else if (grey < 102) {
        sb.append('\u2593');
      } else if (grey < 153) {
        sb.append('\u2592');
      } else if (grey < 204) {
        sb.append('\u2591');
      } else {
        sb.append(' ');
      }
    }
  }
  conn.write(sb.toString());
  conn.schedule(this::broadcast, 100, TimeUnit.MILLISECONDS);
}
 
Example #20
Source File: HttpTtyConnection.java    From termd with Apache License 2.0 5 votes vote down vote up
public HttpTtyConnection(Charset charset, Vector size) {
  this.charset = charset;
  this.size = size;
  this.eventDecoder = new TtyEventDecoder(3, 26, 4);
  this.decoder = new BinaryDecoder(512, charset, eventDecoder);
  this.stdout = new TtyOutputMode(new BinaryEncoder(charset, this::write));
}
 
Example #21
Source File: TelnetTtyConnection.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
protected void onSize(int width, int height) {
  this.size = new Vector(width, height);
  if (sizeHandler != null) {
    sizeHandler.accept(size);
  }
}
 
Example #22
Source File: ReadlineTest.java    From termd with Apache License 2.0 5 votes vote down vote up
@Test
public void testPreserveOriginalHandlers() {
  TestTerm term = new TestTerm(this);
  Consumer<int[]> readHandler = buf -> {};
  Consumer<Vector> sizeHandler = size -> {};
  term.readHandler = readHandler;
  term.sizeHandler = sizeHandler;
  term.readlineComplete();
  assertFalse(term.readHandler == readHandler);
  assertFalse(term.sizeHandler == sizeHandler);
  term.read('\r');
  assertEquals(term.readHandler, readHandler);
  assertEquals(term.sizeHandler, sizeHandler);
}
 
Example #23
Source File: LineBufferTest.java    From termd with Apache License 2.0 5 votes vote down vote up
@Test
public void testCursorPosition() {
  LineBuffer buffer = new LineBuffer();
  buffer.insert('a', 'b', 'c');
  assertEquals(new Vector(3, 0), buffer.getCursorPosition(4));
  assertEquals(new Vector(0, 1), buffer.getCursorPosition(3));
  assertEquals(new Vector(1, 1), buffer.getCursorPosition(2));
  assertEquals(new Vector(0, 3), buffer.getCursorPosition(1));
}
 
Example #24
Source File: LineBufferTest.java    From termd with Apache License 2.0 5 votes vote down vote up
@Test
public void testCursorPositionWithNewLine() {
  LineBuffer buffer = new LineBuffer();
  buffer.insert('a', 'b', '\n', 'c');
  assertEquals(new Vector(1, 1), buffer.getCursorPosition(4));
  assertEquals(new Vector(1, 1), buffer.getCursorPosition(3));
  assertEquals(new Vector(1, 2), buffer.getCursorPosition(2));
  assertEquals(new Vector(0, 4), buffer.getCursorPosition(1));
}
 
Example #25
Source File: LineBufferTest.java    From termd with Apache License 2.0 5 votes vote down vote up
public void testCursorPositionWithCR() {
  LineBuffer buffer = new LineBuffer();
  buffer.insert('a', '\r', 'b', 'c');
  assertEquals(new Vector(2, 0), buffer.getCursorPosition(4));
  assertEquals(new Vector(2, 0), buffer.getCursorPosition(3));
  assertEquals(new Vector(0, 1), buffer.getCursorPosition(2));
  assertEquals(new Vector(0, 3), buffer.getCursorPosition(1));
}
 
Example #26
Source File: LineBufferTest.java    From termd with Apache License 2.0 5 votes vote down vote up
public void testCursorControlChar() {
  LineBuffer buffer = new LineBuffer();
  buffer.insert('a', '\t', 'c');
  assertEquals(new Vector(2, 0), buffer.getCursorPosition(4));
  assertEquals(new Vector(2, 0), buffer.getCursorPosition(3));
  assertEquals(new Vector(0, 1), buffer.getCursorPosition(2));
  assertEquals(new Vector(0, 2), buffer.getCursorPosition(1));
}
 
Example #27
Source File: LineBufferTest.java    From termd with Apache License 2.0 5 votes vote down vote up
public void testCursorInvisibleChar() {
  LineBuffer buffer = new LineBuffer();
  buffer.insert('a', '\0', 'c');
  assertEquals(new Vector(2, 0), buffer.getCursorPosition(4));
  assertEquals(new Vector(2, 0), buffer.getCursorPosition(3));
  assertEquals(new Vector(0, 1), buffer.getCursorPosition(2));
  assertEquals(new Vector(0, 2), buffer.getCursorPosition(1));
}
 
Example #28
Source File: LineBufferTest.java    From termd with Apache License 2.0 5 votes vote down vote up
public void testCursorPositionWithMultiCell1() {
  LineBuffer buffer = new LineBuffer();
  buffer.insert('한', 'b');
  assertEquals(new Vector(3, 0), buffer.getCursorPosition(4));
  assertEquals(new Vector(0, 1), buffer.getCursorPosition(3));
  assertEquals(new Vector(1, 1), buffer.getCursorPosition(2));
  try {
    buffer.getCursorPosition(1);
    fail();
  } catch (UnsupportedOperationException ignore) {
  }
}
 
Example #29
Source File: LineBufferTest.java    From termd with Apache License 2.0 5 votes vote down vote up
public void testCursorPositionWithMultiCell2() {
  LineBuffer buffer = new LineBuffer();
  buffer.insert('a', '한');
  assertEquals(new Vector(3, 0), buffer.getCursorPosition(4));
  assertEquals(new Vector(0, 1), buffer.getCursorPosition(3));
  assertEquals(new Vector(0, 2), buffer.getCursorPosition(2));
  try {
    buffer.getCursorPosition(1);
    fail();
  } catch (UnsupportedOperationException ignore) {
  }
}
 
Example #30
Source File: Completion.java    From termd with Apache License 2.0 4 votes vote down vote up
/**
 * @return the current screen dimension at the moment the completion was initiated
 */
public Vector size() {
  return interaction.size();
}