aboutsummaryrefslogtreecommitdiff
path: root/basic_curses.c
blob: 1a48b8d1fbeb2da860e7c0d9ada4de768c105a34 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * Ache - 2017-08-14 - GPLv3
 */

#include "basic_curses.h"

/*
 * Set of functions to easy print on the ncurse interface
 */



void printfc(char* fstrc, int max_length, ...) {
    int i = 0;
    int nbC = 0;
    char* tmp = NULL;
    int toP = 0;


    va_list ap;
    va_start(ap, max_length);


    while(fstrc[i] && nbC < max_length) {
        if( strchr( "${<%>}#", fstrc[i]) ) {
            if( tmp ) {
                printw("%.*s", toP, tmp);
                tmp=NULL;
                toP=0;
            }
            switch( fstrc[i] ) {
                case '$':
                    if( fstrc[i+1] ) {
                        if( isdigit(fstrc[i+1]) ) {
                            if( fstrc[i+1] == '9') {
                                ;
                            }else
                                attrset( COLOR_PAIR(fstrc[i+1]-'0') | A_NORMAL);
                        }else{
#ifdef DEBUG
                            fprintf(stderr,"%c isn't a color, skipping", fstrc[i+1]);
#endif
                        }
                    }else{
#ifdef DEBUG
                        fprintf(stderr,"end of line by '$', expect a color after '$'");
#endif
                        return;
                    }
                    i+=2;
                    break;
                case '%':
                    {
                        int j = 1;
                        char format[10] = "%";
                        while(j < 9 && !strchr("diouxXeEfFgGaAcsp%", format[j] = fstrc[i+j]) );
                        if( j == 9 ) {
#ifdef DEBUG
                            fprintf(stderr,"format too long %s...",format);
#endif
                            return;
                        }
                        if( strchr("di", format[j] ) ) {
                            printw(format, va_arg(ap, int));
                        }else if( strchr("ouxX", format[j]) ) {
                            printw(format, va_arg(ap, unsigned int));
                        }else if( strchr("eE", format[j]) ) {
                            printw(format, va_arg(ap, double));
                        }else if( strchr("fF", format[j]) ) {
                            printw(format, va_arg(ap, double));
                        }else if( strchr("gG", format[j]) ) {
                            printw(format, va_arg(ap, double));
                        }else if( strchr("aA", format[j]) ) {
                            printw(format, va_arg(ap, double));
                        }else if( strchr("c", format[j]) ) {
                            printw(format, va_arg(ap, int));
                        }else if( strchr("s", format[j]) ) {
                            printw(format, va_arg(ap, const char*));
                        }else if( strchr("p", format[j]) ) {
                            printw(format, va_arg(ap, void*));
                        }else if( strchr("%", format[j]) ) {
                            printw("%%");
                        }
//                        nbC+=

                        i+=j;
                    }
            }
        }else{
            if( ! tmp )
                tmp = fstrc+i;
            toP += 1;
            nbC++;
            i++;
        }
    }
    if(tmp)
        printw("%.*s", toP, tmp);
    fflush(NULL);
}

void printc(char* fstrc, int max_length) {
    int i = 0;
    int nbC = 0;
    char* tmp = NULL;
    int toP = 0;
    while(fstrc[i] && nbC < max_length) {
        if( strchr( "${<>}#", fstrc[i]) ) {
            if( tmp ) {
                printw("%.*s", toP, tmp);
                tmp=NULL;
                toP=0;
            }
            switch( fstrc[i] ) {
                case '$':
                    if( fstrc[i+1] ) {
                        if( isdigit(fstrc[i+1]) ) {
                            if( fstrc[i+1] == '9') {
                                ;
                            }else
                                attrset( COLOR_PAIR(fstrc[i+1]-'0') | A_NORMAL);
                        }else{
#ifdef DEBUG
                            fprintf(stderr,"%c isn't a color, skipping", fstrc[i+1]);
#endif                            
                        }
                    }else{
#ifdef DEBUG
                        fprintf(stderr,"end of line by '$', expect a color after '$'");
#endif
                        return;
                    }
                    i+=2;
            }
        }else{
            if( ! tmp )
                tmp = fstrc+i;
            toP += 1;
            nbC++;
            i++;
        }
    }
    if(tmp)
        printw("%.*s", toP, tmp);
    fflush(NULL);
}
void mvprintc(int x, int y, char* fstrc, int max_length) {
    move(y, x);
    printc(fstrc, max_length);
}