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