Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@leptos-null
Last active April 28, 2019 19:37
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 leptos-null/8a6a5c373e28f158dd0bb8c0771d9ba7 to your computer and use it in GitHub Desktop.
Save leptos-null/8a6a5c373e28f158dd0bb8c0771d9ba7 to your computer and use it in GitHub Desktop.
Example of a VT100 progress bar in C
//
// progress_bar.c
//
// Created by Leptos on 2/17/19.
// Copyright © 2018 Leptos. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <unistd.h>
int main(int argc, const char *argv[]) {
const char *argOne = argv[1];
if (argOne) {
long const parsedInput = atol(argOne);
if (parsedInput < 0) {
puts("length cannot be negative");
return 1;
}
size_t const len = parsedInput;
char buff[len + 4];
buff[0] = '[';
memset(buff + 1, ' ', len);
buff[len + 1] = ']';
buff[len + 2] = '\r';
buff[len + 3] = '\0';
ptrdiff_t const buffOffset = 1;
for (ptrdiff_t i = buffOffset; i < (len + buffOffset); i++) {
buff[i] = '-';
fputs(buff, stdout);
fflush(stdout);
usleep(100000);
}
} else {
printf("Usage: %s <length>\n"
" Print a line of dashes (-) over time for a given length\n"
" This program is an example of a moving \"progress bar\" in a terminal\n", argv[0]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment