Files
apps
core
deps
codemirror
crypt_blowfish
libbacktrace
libbacktrace_config
libsodium
libuv
.github
cmake-toolchains
docs
code
cgi
default-loop
detach
dns
helloworld
idle-basic
idle-compute
interfaces
locks
multi-echo-server
onchange
pipe-echo-server
plugin
proc-streams
progress
queue-cancel
queue-work
ref-timer
signal
spawn
tcp-echo-server
thread-create
tty
tty-gravity
main.c
udp-dhcp
uvcat
uvstop
uvtee
uvwget
.gitignore
CMakeLists.txt
src
Makefile
make.bat
requirements.txt
img
include
m4
src
test
tools
.gitattributes
.gitignore
.mailmap
.readthedocs.yaml
AUTHORS
CMakeLists.txt
CONTRIBUTING.md
ChangeLog
LICENSE
LICENSE-docs
LICENSE-extra
LINKS.md
MAINTAINERS.md
Makefile.am
README.md
SUPPORTED_PLATFORMS.md
autogen.sh
configure.ac
libuv-static.pc.in
libuv.pc.in
tsansupp.txt
uv_win_longpath.manifest
lit
openssl
picohttpparser
quickjs
smoothie
speedscope
split
sqlite
xopt
zlib
docs
src
tools
.dockerignore
Dockerfile
LICENSE
Makefile
README.md
tildefriends/deps/libuv/docs/code/tty-gravity/main.c

49 lines
1.1 KiB
C
Raw Normal View History

#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, STDOUT_FILENO, 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);
}