Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9326694

Browse filesBrowse files
committed
Initial commit; Tested in Linux
0 parents  commit 9326694
Copy full SHA for 9326694

File tree

Expand file treeCollapse file tree

3 files changed

+126
-0
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+126
-0
lines changed

‎bar.c

Copy file name to clipboard
+69Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include<stdio.h>
2+
#include<stddef.h> // for NULL
3+
#include<stdlib.h> // for malloc,atoi
4+
#include<string.h> // for strlen
5+
#include<sys/ioctl.h> // for ioctl
6+
#include"bar.h"
7+
8+
#define STDOUT 0
9+
10+
static char *bar;
11+
static int columns;
12+
13+
char blankChar = '-';
14+
char filledChar = '=';
15+
char leftChar = '[';
16+
char rightChar = ']';
17+
18+
static int getNeededColumnCount()
19+
{
20+
struct winsize w;
21+
ioctl(STDOUT,TIOCGWINSZ,&w);
22+
23+
return w.ws_col;
24+
}
25+
26+
static void bar_init()
27+
{
28+
columns = getNeededColumnCount();
29+
30+
//create the bar
31+
int totalsize = columns * 2;
32+
33+
// free the bar before creating a new one.
34+
if (bar != NULL) free(bar);
35+
bar = malloc(sizeof(char) * totalsize);
36+
37+
// Fill in the chareacters into the buffer
38+
char c = blankChar;
39+
while(--totalsize)
40+
{
41+
if (totalsize == (columns -1))
42+
c = filledChar;
43+
bar[totalsize] = c;
44+
}
45+
}
46+
47+
void print_progressbar(float progress,char *left_text,char *right_text)
48+
{
49+
int index,artifact_strlen,bar_length;
50+
artifact_strlen = (leftChar == '\0')? 0:1;
51+
artifact_strlen += (rightChar == '\0')? 0:1;
52+
artifact_strlen += strlen(left_text);
53+
artifact_strlen += strlen(right_text);
54+
55+
int current_columns = getNeededColumnCount();
56+
if (current_columns > columns){
57+
bar_init();
58+
}
59+
60+
bar_length = current_columns - artifact_strlen;
61+
index = columns - (progress * bar_length);
62+
printf("%s%c%.*s%c%s\r",left_text,
63+
leftChar,
64+
bar_length,
65+
&bar[index],
66+
rightChar,
67+
right_text);
68+
fflush(stdout);
69+
}

‎bar.h

Copy file name to clipboard
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef _PROGRESS_BAR_H
2+
#define _PROGRESS_BAR_H
3+
4+
// In order to customize the progress bar set these variables in your program
5+
// (before the first use of print_progressbar function.)
6+
7+
// Default values:
8+
// * blankChar = '-'
9+
// * filledChar = '='
10+
// * leftChar = '['
11+
// * rightChar = ']'
12+
13+
extern char blankChar; // makes the yet to be filled part of the bar.
14+
extern char filledChar; // makes the filled part of the progress bar.
15+
extern char leftChar; // left boundary char of the progress bar.
16+
extern char rightChar; // right boundary char of the progress bar.
17+
18+
void print_progressbar(float progress,char *left_text,char *right_text);
19+
20+
#endif // _PROGRESS_BAR_H

‎demo.c

Copy file name to clipboard
+37Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* This is a demostration of the progressbar.
3+
* Author: Arjob Mukherjee (arjobmukherjee@gmail.com)
4+
* Date: 12 Jan 2019
5+
*/
6+
7+
#include<stdio.h>
8+
#include<unistd.h> // for sleep
9+
#include"bar.h"
10+
11+
#define PROGRESS_BAR_SETUP() blankChar = '-'; \
12+
filledChar = '#'; \
13+
leftChar = '\0'; \
14+
rightChar = '\0'
15+
16+
int main()
17+
{
18+
// Customizes the progress bar. Uncomment to see the effect.
19+
//PROGRESS_BAR_SETUP();
20+
21+
char right_text[10], left_text[100];
22+
float progress = 0.0;
23+
24+
sprintf(left_text,"%-50s ","Progress:");
25+
26+
for(;;){
27+
sprintf(right_text,"%6.1f%%",progress * 100);
28+
print_progressbar(progress,left_text,right_text);
29+
progress += 0.1f;
30+
if (progress > 1.0){
31+
printf("\n");
32+
progress = 0;
33+
}
34+
sleep(1);
35+
}
36+
return 0;
37+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.