summaryrefslogtreecommitdiff
path: root/src/progressbar.c
blob: e8cd54bd0a7151683087ab7595d45826138e7e6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "progressbar.h"

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>

int create_progressbar( cssh_progressbar_t *progressbar, uint64_t min_value, uint64_t max_value, size_t size, const char *label_fmt, ... )
{
	progressbar->min_value = min_value;
	progressbar->max_value = max_value;

	va_list va;
	va_start( va, label_fmt );
	
	progressbar->label = (char *)malloc( size );
	if( progressbar->label == NULL ) {
		return -1;
	}

	(void)vsnprintf( progressbar->label, size, label_fmt, va );
	
	progressbar->value = progressbar->min_value;
	
	va_end( va );
	
	return 0;
}

void free_progressbar( cssh_progressbar_t *progressbar )
{
	free( progressbar->label );
}

void set_value_of_progressbar( cssh_progressbar_t *progressbar, uint64_t value )
{
	progressbar->value = value;
}

void redraw_progressbar( cssh_progressbar_t *progressbar )
{
	fprintf( stderr, "%s %"PRIu64"  \n", progressbar->label, progressbar->value );
}

int create_progressbar_pool( cssh_progressbar_pool_t *pool, size_t initial_size )
{
	pool->N = 0;
	pool->capacity = initial_size;
	pool->progressbar = (cssh_progressbar_t **)malloc( pool->capacity * sizeof( cssh_progressbar_t * ) );
	if( pool->progressbar == NULL ) {
		return -1;
	}
	
	return 0;
}

void free_progressbar_pool( cssh_progressbar_pool_t *pool )
{
	for( size_t i = 0; i < pool->N; i++ ) {
		free_progressbar( pool->progressbar[i] );
	}
	
	free( pool->progressbar );
}

int append_progressbar_to_pool( cssh_progressbar_pool_t *pool, cssh_progressbar_t *progressbar )
{
	if( pool->N + 1 > pool->capacity ) {
		return -1;
	}
	
	pool->progressbar[pool->N] = progressbar;
	pool->N++;
	
	return 0;
}

int remove_progressbar_from_pool( cssh_progressbar_pool_t *pool, cssh_progressbar_t *progressbar )
{
	if( pool->N == 0 ) {
		return -1;
	}
	
	for( size_t i = 0; i < pool->N; i++ ) {
		if( pool->progressbar[i] ) {
			if( i < pool->N - 1 ) {
				memmove( &pool->progressbar[i], &pool->progressbar[i+1],
					( pool->N - i ) * sizeof( cssh_progressbar_t * ) );
			}
			pool->N--;
			return 0;
		}
	}
	
	return -1;
}

void redraw_progressbars( cssh_progressbar_pool_t *pool )
{
	
	for( size_t i = 0; i < pool->N; i++ ) {
		redraw_progressbar( pool->progressbar[i] );
	}
	printf( "\33[%dF\n", pool->N + 1 );
}