Mercurial
comparison third_party/libuv/docs/code/tty-gravity/main.c @ 173:827c6ac504cd hg-web
Merged in default here.
| author | MrJuneJune <me@mrjunejune.com> |
|---|---|
| date | Mon, 19 Jan 2026 18:59:10 -0800 |
| parents | 948de3f54cea |
| children |
comparison
equal
deleted
inserted
replaced
| 151:c033667da5f9 | 173:827c6ac504cd |
|---|---|
| 1 #include <stdio.h> | |
| 2 #include <string.h> | |
| 3 #include <unistd.h> | |
| 4 #include <uv.h> | |
| 5 | |
| 6 uv_loop_t *loop; | |
| 7 uv_tty_t tty; | |
| 8 uv_timer_t tick; | |
| 9 uv_write_t write_req; | |
| 10 int width, height; | |
| 11 int pos = 0; | |
| 12 char *message = " Hello TTY "; | |
| 13 | |
| 14 void 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 | |
| 32 int 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 } |