Skip to content

Instantly share code, notes, and snippets.

@winny-
Forked from spedru/bubble.c
Last active August 29, 2015 14:16
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 winny-/cef9e20d9ac4728f9d4f to your computer and use it in GitHub Desktop.
Save winny-/cef9e20d9ac4728f9d4f to your computer and use it in GitHub Desktop.
/* cum pile with -std=c99 or c11 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define _POSIX_C_SOURCE 200809L
#include <time.h>
const struct timespec sleepytime = {0, 70000000};
const int bubble[] = {2, 3, 4, 5, 4, 3, 3, 2};
int main(int argc, char *argv[])
{
size_t len = 0;
for (int i = 1; i < argc; i++)
len += 1 + strlen(argv[i]);
char *buf = calloc(len, 1);
for (int i = 1; i < argc; i++)
strcat(strcat(buf, argv[i]), " ");
buf[len - 1] = '\0';
size_t rail = len * 2 + (sizeof bubble / sizeof (int));
int *spaces = calloc(rail, sizeof (int));
memcpy(spaces + len, bubble, sizeof bubble);
// CSI Ps A Cursor Up Ps Times (default = 1) (CUU).
// More about xterm escapes: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
printf("\n\033[1A");
for (size_t s = len + (sizeof bubble / sizeof (int)); s; s--) {
for (size_t t = 0; t < len; t++)
printf("%*c", spaces[s + t], buf[t]);
fflush(stdout);
nanosleep(&sleepytime, NULL);
putchar('\r');
}
// CSI Ps B Cursor Down Ps Times (default = 1) (CUD).
printf("\033[1B");
free(buf);
free(spaces);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment