Created
December 19, 2018 01:33
-
-
Save ruda/d65ad76d1344be793e4b2b728a8072f0 to your computer and use it in GitHub Desktop.
ASCII animation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2018 Rudá Moura. All rights reserved. | |
*/ | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <termios.h> | |
#include <sys/ioctl.h> | |
void emit(char ch, int n) { | |
for (int i=0; i < n; i++) | |
putchar(ch); | |
putchar('\r'); | |
fflush(stdout); | |
} | |
void toggle(char ch1, char ch2, int n, useconds_t delay) { | |
emit(ch1, n); | |
usleep(delay/2); | |
emit(ch2, n); | |
usleep(delay/2); | |
} | |
void blink(int n, useconds_t delay) { | |
toggle('+', 'X', n, delay); | |
} | |
void spin(int n, useconds_t delay) { | |
toggle('|', '/', n, delay/2); | |
toggle('-', '\\', n, delay/2); | |
} | |
int | |
main(int argc, char *argv[]) | |
{ | |
struct winsize ws; | |
ioctl(STDIN_FILENO, TIOCGWINSZ, &ws); | |
int size = (ws.ws_col) * (ws.ws_row); | |
for(;;) | |
spin(size, 250000); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment