~james/gengo

gengo/samples-test/samples/C/progress-bar.c -rw-r--r-- 2.8 KiB
50871857 — james chore(import): snapshot james.dad.town/gengo at 8f775550b0ee 16 hours ago
                                                                                
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
107
108
109
110
111
112
113
114
115
#include "progress-bar.h"
#include <stdio.h>
#include <stdbool.h>
#include <string.h>

ProgressBar init(char symbol, int length, int total) {
    ProgressBar pb;
    pb.symbol = symbol;
    pb.length = length;
    pb.progress = 0;
    pb.showPercent = false;
    pb.total = total;
    pb.startSymbol = '[';
    pb.endSymbol = ']';
    pb.format = "{bar} {percent} {count}";
    return pb;
}

ProgressBar update(ProgressBar *pb, int progress) {
    pb->progress = progress;
    return *pb;
}

ProgressBar showPercent(ProgressBar *pb, bool show) {
    pb->showPercent = show;
    return *pb;
}

ProgressBar showCount(ProgressBar *pb, bool show) {
    pb->showCount = show;
    return *pb;
}

ProgressBar setStartEndSymbols(ProgressBar *pb, char start, char end) {
    pb->startSymbol = start;
    pb->endSymbol = end;
    return *pb;
}

ProgressBar setCustomFormat(ProgressBar *pb, char* format) {
    pb->format = format;
    return *pb;
}

ProgressBar tick(ProgressBar *pb) {
    pb->progress++;
    return *pb;
}

void print(ProgressBar pb) {
    char bar[256] = "";
    char percent_str[32] = "";
    char count_str[32] = "";
    char result[512] = "";
    char *format = pb.format;
    
    // Generate bar component
    sprintf(bar, "%c", pb.startSymbol);
    int scaled_progress = (int)((float)pb.progress * pb.length / pb.total);
    
    for (int i = 0; i < pb.length; i++) {
        if (i < scaled_progress) {
            sprintf(bar + strlen(bar), "%c", pb.symbol);
        } else {
            strcat(bar, " ");
        }
    }
    sprintf(bar + strlen(bar), "%c", pb.endSymbol);
    
    // Generate percent component
    int percent = (pb.progress * 100) / pb.total;
    if (pb.showPercent) {
        sprintf(percent_str, "%d%%", percent);
    }
    
    // Generate count component
    if (pb.showCount) {
        sprintf(count_str, "%d/%d", pb.progress, pb.total);
    }
    
    // Process format string
    char *ptr = format;
    while (*ptr) {
        if (*ptr == '{') {
            if (strncmp(ptr, "{bar}", 5) == 0) {
                strcat(result, bar);
                ptr += 5;
            } else if (strncmp(ptr, "{percent}", 9) == 0) {
                if (pb.showPercent) {
                    strcat(result, percent_str);
                }
                ptr += 9;
            } else if (strncmp(ptr, "{count}", 7) == 0) {
                if (pb.showCount) {
                    strcat(result, count_str);
                }
                ptr += 7;
            } else {
                strncat(result, ptr, 1);
                ptr++;
            }
        } else {
            strncat(result, ptr, 1);
            ptr++;
        }
    }
    
    printf("%s", result);
    
    if (percent < 100) {
        printf("\r");
    } else {
        printf("\n");
    }
}