diff options
author | Saúl Ibarra Corretgé <saghul@gmail.com> | 2017-04-25 08:27:52 +0200 |
---|---|---|
committer | Saúl Ibarra Corretgé <saghul@gmail.com> | 2017-04-28 11:16:15 +0200 |
commit | d59d6e6f22b4c11c37e34843d111a748df73bda2 (patch) | |
tree | f2a46fbfb3e308b3f474d086b122f4b591a78bcc /docs/code/tty-gravity | |
parent | 2ce5734d76a8bfbf01af4a4854edf1e3cc42e029 (diff) | |
download | libuv-d59d6e6f22b4c11c37e34843d111a748df73bda2.tar.gz libuv-d59d6e6f22b4c11c37e34843d111a748df73bda2.zip |
doc: add code samples from uvbook (unadapted)
PR-URL: https://github.com/libuv/libuv/pull/1246
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Diffstat (limited to 'docs/code/tty-gravity')
-rw-r--r-- | docs/code/tty-gravity/main.c | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/docs/code/tty-gravity/main.c b/docs/code/tty-gravity/main.c new file mode 100644 index 00000000..053e7e59 --- /dev/null +++ b/docs/code/tty-gravity/main.c @@ -0,0 +1,48 @@ +#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <uv.h> + +uv_loop_t *loop; +uv_tty_t tty; +uv_timer_t tick; +uv_write_t write_req; +int width, height; +int pos = 0; +char *message = " Hello TTY "; + +void update(uv_timer_t *req) { + char data[500]; + + uv_buf_t buf; + buf.base = data; + buf.len = sprintf(data, "\033[2J\033[H\033[%dB\033[%luC\033[42;37m%s", + pos, + (unsigned long) (width-strlen(message))/2, + message); + uv_write(&write_req, (uv_stream_t*) &tty, &buf, 1, NULL); + + pos++; + if (pos > height) { + uv_tty_reset_mode(); + uv_timer_stop(&tick); + } +} + +int main() { + loop = uv_default_loop(); + + uv_tty_init(loop, &tty, 1, 0); + uv_tty_set_mode(&tty, 0); + + if (uv_tty_get_winsize(&tty, &width, &height)) { + fprintf(stderr, "Could not get TTY information\n"); + uv_tty_reset_mode(); + return 1; + } + + fprintf(stderr, "Width %d, height %d\n", width, height); + uv_timer_init(loop, &tick); + uv_timer_start(&tick, update, 200, 200); + return uv_run(loop, UV_RUN_DEFAULT); +} |