Skip to content

Instantly share code, notes, and snippets.

@ruda
Created December 19, 2018 01:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruda/d65ad76d1344be793e4b2b728a8072f0 to your computer and use it in GitHub Desktop.
Save ruda/d65ad76d1344be793e4b2b728a8072f0 to your computer and use it in GitHub Desktop.
ASCII animation
/*
* 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