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

The following examples show how to use io.termd.core.tty.TtyConnection#write() . 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 {
  if (args.isEmpty()) {
    conn.write("usage: sleep seconds\n");
    return;
  }
  int time = -1;
  try {
    time = Integer.parseInt(args.get(0));
  } catch (NumberFormatException ignore) {
  }
  if (time > 0) {
    // Sleep until timeout or Ctrl-C interrupted
    Thread.sleep(time * 1000);
  }
}
 
Example 2
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 {
  if (args.isEmpty()) {
    conn.write("usage: sleep seconds\n");
    return;
  }
  int time = -1;
  try {
    time = Integer.parseInt(args.get(0));
  } catch (NumberFormatException ignore) {
  }
  if (time > 0) {
    // Sleep until timeout or Ctrl-C interrupted
    Thread.sleep(time * 1000);
  }
}
 
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 {
  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 4
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 5
Source File: Shell.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(TtyConnection conn, List<String> args) throws Exception {
  for (int i = 0;i < args.size();i++) {
    if (i > 0) {
      conn.write(" ");
    }
    conn.write(args.get(i));
  }
  conn.write("\n");
}
 
Example 6
Source File: Shell.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(TtyConnection conn, List<String> args) throws Exception {
  StringBuilder msg = new StringBuilder("Demo term, try commands: ");
  Command[] commands = Command.values();
  for (int i = 0;i < commands.length;i++) {
    if (i > 0) {
      msg.append(",");
    }
    msg.append(" ").append(commands[i].name());
  }
  msg.append("...\n");
  conn.write(msg.toString());
}
 
Example 7
Source File: Shell.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(TtyConnection conn, List<String> args) throws Exception {
  while (true) {

    StringBuilder buf = new StringBuilder();
    Formatter formatter = new Formatter(buf);

    List<Thread> threads = new ArrayList<Thread>(Thread.getAllStackTraces().keySet());
    for (int i = 1;i <= conn.size().y();i++) {

      // Change cursor position and erase line with ANSI escape code magic
      buf.append("\033[").append(i).append(";1H\033[K");

      //
      String format = "  %1$-5s %2$-10s %3$-50s %4$s";
      if (i == 1) {
        formatter.format(format,
            "ID",
            "STATE",
            "NAME",
            "GROUP");
      } else {
        int index = i - 2;
        if (index < threads.size()) {
          Thread thread = threads.get(index);
          formatter.format(format,
              thread.getId(),
              thread.getState().name(),
              thread.getName(),
              thread.getThreadGroup().getName());
        }
      }
    }

    conn.write(buf.toString());

    // Sleep until we refresh the list of interrupted
    Thread.sleep(1000);
  }
}
 
Example 8
Source File: Readline.java    From termd with Apache License 2.0 5 votes vote down vote up
/**
 * Read a line until a request can be processed.
 *
 * @param requestHandler the requestHandler
 */
public void readline(TtyConnection conn, String prompt, Consumer<String> requestHandler, Consumer<Completion> completionHandler) {
  synchronized (this) {
    if (interaction != null) {
      throw new IllegalStateException("Already reading a line");
    }
    interaction = new Interaction(conn, prompt, requestHandler, completionHandler);
  }
  interaction.install();
  conn.write(prompt);
  schedulePendingEvent();
}
 
Example 9
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 10
Source File: Shell.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(TtyConnection conn, List<String> args) throws Exception {
  for (int i = 0;i < args.size();i++) {
    if (i > 0) {
      conn.write(" ");
    }
    conn.write(args.get(i));
  }
  conn.write("\n");
}
 
Example 11
Source File: Shell.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(TtyConnection conn, List<String> args) throws Exception {
  StringBuilder msg = new StringBuilder("Demo term, try commands: ");
  Command[] commands = Command.values();
  for (int i = 0;i < commands.length;i++) {
    if (i > 0) {
      msg.append(",");
    }
    msg.append(" ").append(commands[i].name());
  }
  msg.append("...\n");
  conn.write(msg.toString());
}
 
Example 12
Source File: Shell.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(TtyConnection conn, List<String> args) throws Exception {
  while (true) {

    StringBuilder buf = new StringBuilder();
    Formatter formatter = new Formatter(buf);

    List<Thread> threads = new ArrayList<>(Thread.getAllStackTraces().keySet());
    for (int i = 1;i <= conn.size().y();i++) {

      // Change cursor position and erase line with ANSI escape code magic
      buf.append("\033[").append(i).append(";1H\033[K");

      //
      String format = "  %1$-5s %2$-10s %3$-50s %4$s";
      if (i == 1) {
        formatter.format(format,
            "ID",
            "STATE",
            "NAME",
            "GROUP");
      } else {
        int index = i - 2;
        if (index < threads.size()) {
          Thread thread = threads.get(index);
          formatter.format(format,
              thread.getId(),
              thread.getState().name(),
              thread.getName(),
              thread.getThreadGroup().getName());
        }
      }
    }

    conn.write(buf.toString());

    // Sleep until we refresh the list of interrupted
    Thread.sleep(1000);
  }
}
 
Example 13
Source File: Readline.java    From termd with Apache License 2.0 5 votes vote down vote up
/**
 * Read a line until a request can be processed.
 *
 * @param requestHandler the requestHandler
 */
public void readline(TtyConnection conn, String prompt, Consumer<String> requestHandler, Consumer<Completion> completionHandler) {
  synchronized (this) {
    if (interaction != null) {
      throw new IllegalStateException("Already reading a line");
    }
    interaction = new Interaction(conn, prompt, requestHandler, completionHandler);
  }
  interaction.install();
  conn.write(prompt);
  schedulePendingEvent();
}
 
Example 14
Source File: Plasma.java    From termd with Apache License 2.0 4 votes vote down vote up
public void run(final TtyConnection conn) {

    int width = conn.size().x();
    int height = conn.size().y();

    long t = System.currentTimeMillis();
    double a = 0.10 * 128 / width;
    double b = 0.0015;
    double c = 0.07;
    double d = 0.08 * 128 / width;
    double e = 0.002;
    double f = 0.01;
    double g = 0.08 * 32 / height;
    double h = 0.003;
    double i = 0.01;
    double j = 0.1 * 32 / height;
    double k = 0.001;
    double l = 0.06;

    double[][] abc = new double[width][height];
    for (int y = 0; y < height; y++) {
      for (int x = 0; x < width; x++) {
        abc[x][y] = 32 * (
            cos(a * x + b * t + c) +
                cos(d * x + e * t + f) +
                cos(g * y + h * t + i) +
                cos(j * y + k * t + l) + 4);
      }
    }

    // Refresh the screen using Ansi magic + the unicode block code points
    StringBuilder sb = new StringBuilder();
    for (int y = 0; y < height; y++) {
      sb.append("\033[").append(y + 1).append(";1H");
      for (int x = 0; x < width; x++) {
        double val = abc[x][y];
        if (val < 51) {
          sb.append('\u2588');
        } else if (val < 102) {
          sb.append('\u2593');
        } else if (val < 153) {
          sb.append('\u2592');
        } else if (val < 204) {
          sb.append('\u2591');
        } else {
          sb.append(' ');
        }
      }
    }

    conn.write(sb.toString());

    //
    if (!interrupted) {
      conn.schedule(new Runnable() {
        @Override
        public void run() {
          Plasma.this.run(conn);
        }
      }, 50, TimeUnit.MILLISECONDS);
    } else {
      conn.close();
    }
  }
 
Example 15
Source File: Plasma.java    From termd with Apache License 2.0 4 votes vote down vote up
public void run(TtyConnection conn) {

    int width = conn.size().x();
    int height = conn.size().y();

    long t = System.currentTimeMillis();
    double a = 0.10 * 128 / width;
    double b = 0.0015;
    double c = 0.07;
    double d = 0.08 * 128 / width;
    double e = 0.002;
    double f = 0.01;
    double g = 0.08 * 32 / height;
    double h = 0.003;
    double i = 0.01;
    double j = 0.1 * 32 / height;
    double k = 0.001;
    double l = 0.06;

    double[][] abc = new double[width][height];
    for (int y = 0; y < height; y++) {
      for (int x = 0; x < width; x++) {
        abc[x][y] = 32 * (
            cos(a * x + b * t + c) +
                cos(d * x + e * t + f) +
                cos(g * y + h * t + i) +
                cos(j * y + k * t + l) + 4);
      }
    }

    // Refresh the screen using Ansi magic + the unicode block code points
    StringBuilder sb = new StringBuilder();
    for (int y = 0; y < height; y++) {
      sb.append("\033[").append(y + 1).append(";1H");
      for (int x = 0; x < width; x++) {
        double val = abc[x][y];
        if (val < 51) {
          sb.append('\u2588');
        } else if (val < 102) {
          sb.append('\u2593');
        } else if (val < 153) {
          sb.append('\u2592');
        } else if (val < 204) {
          sb.append('\u2591');
        } else {
          sb.append(' ');
        }
      }
    }

    conn.write(sb.toString());

    //
    if (!interrupted) {
      conn.schedule(() -> {
        run(conn);
      }, 50, TimeUnit.MILLISECONDS);
    } else {
      conn.close();
    }
  }