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