main.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <uv.h>
  5. uv_loop_t *loop;
  6. uv_tty_t tty;
  7. uv_timer_t tick;
  8. uv_write_t write_req;
  9. int width, height;
  10. int pos = 0;
  11. char *message = " Hello TTY ";
  12. void update(uv_timer_t *req) {
  13. char data[500];
  14. uv_buf_t buf;
  15. buf.base = data;
  16. buf.len = sprintf(data, "\033[2J\033[H\033[%dB\033[%luC\033[42;37m%s",
  17. pos,
  18. (unsigned long) (width-strlen(message))/2,
  19. message);
  20. uv_write(&write_req, (uv_stream_t*) &tty, &buf, 1, NULL);
  21. pos++;
  22. if (pos > height) {
  23. uv_tty_reset_mode();
  24. uv_timer_stop(&tick);
  25. }
  26. }
  27. int main() {
  28. loop = uv_default_loop();
  29. uv_tty_init(loop, &tty, STDOUT_FILENO, 0);
  30. uv_tty_set_mode(&tty, 0);
  31. if (uv_tty_get_winsize(&tty, &width, &height)) {
  32. fprintf(stderr, "Could not get TTY information\n");
  33. uv_tty_reset_mode();
  34. return 1;
  35. }
  36. fprintf(stderr, "Width %d, height %d\n", width, height);
  37. uv_timer_init(loop, &tick);
  38. uv_timer_start(&tick, update, 200, 200);
  39. return uv_run(loop, UV_RUN_DEFAULT);
  40. }