aboutsummaryrefslogtreecommitdiffstats
path: root/Meter.c
blob: 45cf7e2fa8a6aa46d1144bdea4ec8c7e38114b10 (plain) (blame)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
htop
(C) 2004 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/

#include "Meter.h"
#include "Object.h"
#include "CRT.h"
#include "ListItem.h"
#include "String.h"

#include <stdlib.h>
#include <curses.h>
#include <string.h>
#include <math.h>

#include "debug.h"
#include <assert.h>

#define METER_BARBUFFER_LEN 128
#define METER_GRAPHBUFFER_LEN 128

/*{

typedef struct Meter_ Meter;

typedef void(*Method_Meter_setValues)(Meter*);
typedef void(*Method_Meter_draw)(Meter*, int, int, int);

typedef enum MeterMode_ {
   UNSET,
   BAR,
   TEXT,
   GRAPH,
   LED,
   LAST_METERMODE
} MeterMode;

struct Meter_ {
   Object super;
   
   int h;
   int w;
   Method_Meter_draw draw;
   Method_Meter_setValues setValues;
   int items;
   int* attributes;
   double* values;
   double total;
   char* caption;
   char* name;
   union {
      RichString* rs;
      char* c;
      double* graph;
   } displayBuffer;
   MeterMode mode;
};

extern char* METER_CLASS;

}*/

#ifndef MIN
#define MIN(a,b) ((a)<(b)?(a):(b))
#endif

/* private property */
char* METER_CLASS = "Meter";

/* private */
char* Meter_ledDigits[3][10] = {
   { " __ ","    "," __ "," __ ","    "," __ "," __ "," __ "," __ "," __ "},
   { "|  |","   |"," __|"," __|","|__|","|__ ","|__ ","   |","|__|","|__|"},
   { "|__|","   |","|__ "," __|","   |"," __|","|__|","   |","|__|"," __|"},
};

/* private property */
char Meter_barCharacters[] = "|#*@$%&";

Meter* Meter_new(char* name, char* caption, int items) {
   Meter* this = malloc(sizeof(Meter));
   Meter_init(this, name, caption, items);
   return this;
}

void Meter_init(Meter* this, char* name, char* caption, int items) {
   ((Object*)this)->delete = Meter_delete;
   ((Object*)this)->class = METER_CLASS;
   this->items = items;
   this->name = name;
   this->caption = caption;
   this->attributes = malloc(sizeof(int) * items);
   this->values = malloc(sizeof(double) * items);
   this->displayBuffer.c = NULL;
   this->mode = UNSET;
   this->h = 0;
}

void Meter_delete(Object* cast) {
   Meter* this = (Meter*) cast;
   assert (this != NULL);
   Meter_done(this);
   free(this);
}

/* private */
void Meter_freeBuffer(Meter* this) {
   switch (this->mode) {
   case BAR: {
      free(this->displayBuffer.c);
      break;
   }
   case LED:
   case TEXT: {
      free(this->displayBuffer.rs);
      break;
   }
   case GRAPH: {
      free(this->displayBuffer.graph);
      break;
   }
   default: {
   }
   }
   this->h = 0;
}

void Meter_done(Meter* this) {
   free(this->caption);
   free(this->attributes);
   free(this->values);
   free(this->name);
   Meter_freeBuffer(this);
}

/* private */
void Meter_drawBar(Meter* this, int x, int y, int w) {
   
   w -= 2;
   attrset(CRT_colors[METER_TEXT]);
   mvaddstr(y, x, this->caption);
   int captionLen = strlen(this->caption);
   x += captionLen;
   w -= captionLen;
   attrset(CRT_colors[BAR_BORDER]);
   mvaddch(y, x, '[');
   mvaddch(y, x + w, ']');
   
   w--;
   x++;
   char bar[w];
   
   this->setValues(this);
   
   int blockSizes[10];
   for (int i = 0; i < w; i++)
      bar[i] = ' ';

   sprintf(bar + (w-strlen(this->displayBuffer.c)), "%s", this->displayBuffer.c);

   // First draw in the bar[] buffer...
   double total = 0.0;
   int offset = 0;
   for (int i = 0; i < this->items; i++) {
      this->values[i] = MAX(this->values[i], 0);
      this->values[i] = MIN(this->values[i], this->total);
      double value = this->values[i];
      if (value > 0) {
         blockSizes[i] = ceil((value/this->total) * w);
      } else {
         blockSizes[i] = 0;
      }
      int nextOffset = offset + blockSizes[i];
      // (Control against invalid values)
      nextOffset = MAX(nextOffset, 0);
      nextOffset = MIN(nextOffset, w);
      for (int j = offset; j < nextOffset; j++)
         if (bar[j] == ' ') {
            if (CRT_hasColors) {
               bar[j] = '|';
            } else {
               bar[j] = Meter_barCharacters[i];
            }
         }
      offset = nextOffset;
      total += this->values[i];
   }

   // ...then print the buffer.
   offset = 0;
   for (int i = 0; i < this->items; i++) {
      attrset(this->attributes[i]);
      mvaddnstr(y, x + offset, bar + offset, blockSizes[i]);
      offset += blockSizes[i];
      offset = MAX(offset, 0);
      offset = MIN(offset, w);
   }
   if (offset < w) {
      attrset(CRT_colors[BAR_SHADOW]);
      mvaddnstr(y, x + offset, bar + offset, w - offset);
   }

   move(y, x + w + 1);
   attrset(CRT_colors[RESET_COLOR]);
}

/* private */
void Meter_drawText(Meter* this, int x, int y, int w) {
   this->setValues(this);
   this->w = w;
   attrset(CRT_colors[METER_TEXT]);
   mvaddstr(y, x, this->caption);
   int captionLen = strlen(this->caption);
   w -= captionLen;
   x += captionLen;
   ((Object*)this)->display((Object*)this, this->displayBuffer.rs);
   mvhline(y, x, ' ', CRT_colors[DEFAULT_COLOR]);
   attrset(CRT_colors[RESET_COLOR]);
   mvaddchstr(y, x, this->displayBuffer.rs->chstr);
}

/* private */
void Meter_drawDigit(int x, int y, int n) {
   for (int i = 0; i < 3; i++) {
      mvaddstr(y+i, x, Meter_ledDigits[i][n]);
   }
}

/* private */
void Meter_drawLed(Meter* this, int x, int y, int w) {
   this->setValues(this);
   ((Object*)this)->display((Object*)this, this->displayBuffer.rs);
   attrset(CRT_colors[LED_COLOR]);
   mvaddstr(y+2, x, this->caption);
   int xx = x + strlen(this->caption);
   for (int i = 0; i < this->displayBuffer.rs->len; i++) {
      char c = this->displayBuffer.rs->chstr[i];
      if (c >= '0' && c <= '9') {
         Meter_drawDigit(xx, y, c-48);
	 xx += 4;
      } else {
         mvaddch(y+2, xx, c);
         xx += 1;
      }
   }
   attrset(CRT_colors[RESET_COLOR]);
}

#define DrawDot(a,y,c) do { \
   attrset(a); \
   mvaddstr(y, x+k, c); \
} while(0)

/* private */
void Meter_drawGraph(Meter* this, int x, int y, int w) {

   for (int i = 0; i < METER_GRAPHBUFFER_LEN - 1; i++) {
      this->displayBuffer.graph[i] = this->displayBuffer.graph[i+1];
   }

   this->setValues(this);

   double value = 0.0;
   for (int i = 0; i < this->items; i++)
      value += this->values[i] / this->total;
   this->displayBuffer.graph[METER_GRAPHBUFFER_LEN - 1] = value;

   for (int i = METER_GRAPHBUFFER_LEN - w, k = 0; i < METER_GRAPHBUFFER_LEN; i++, k++) {
      double value = this->displayBuffer.graph[i];
      DrawDot( CRT_colors[DEFAULT_COLOR], y, " " );
      DrawDot( CRT_colors[DEFAULT_COLOR], y+1, " " );
      DrawDot( CRT_colors[DEFAULT_COLOR], y+2, " " );
      if (value >= 1.00)      DrawDot( CRT_colors[GRAPH_1], y, "^" );
      else if (value >= 0.95) DrawDot( CRT_colors[GRAPH_1], y, "`" );
      else if (value >= 0.90) DrawDot( CRT_colors[GRAPH_1], y, "'" );
      else if (value >= 0.85) DrawDot( CRT_colors[GRAPH_2], y, "-" );
      else if (value >= 0.80) DrawDot( CRT_colors[GRAPH_2], y, "." );
      else if (value >= 0.75) DrawDot( CRT_colors[GRAPH_2], y, "," );
      else if (value >= 0.70) DrawDot( CRT_colors[GRAPH_3], y, "_" );
      else if (value >= 0.65) DrawDot( CRT_colors[GRAPH_3], y+1, "~" );
      else if (value >= 0.60) DrawDot( CRT_colors[GRAPH_3], y+1, "`" );
      else if (value >= 0.55) DrawDot( CRT_colors[GRAPH_4], y+1, "'" );
      else if (value >= 0.50) DrawDot( CRT_colors[GRAPH_4], y+1, "-" );
      else if (value >= 0.45) DrawDot( CRT_colors[GRAPH_4], y+1, "." );
      else if (value >= 0.40) DrawDot( CRT_colors[GRAPH_5], y+1, "," );
      else if (value >= 0.35) DrawDot( CRT_colors[GRAPH_5], y+1, "_" );
      else if (value >= 0.30) DrawDot( CRT_colors[GRAPH_6], y+2, "~" );
      else if (value >= 0.25) DrawDot( CRT_colors[GRAPH_7], y+2, "`" );
      else if (value >= 0.20) DrawDot( CRT_colors[GRAPH_7], y+2, "'" );
      else if (value >= 0.15) DrawDot( CRT_colors[GRAPH_7], y+2, "-" );
      else if (value >= 0.10) DrawDot( CRT_colors[GRAPH_8], y+2, "." );
      else if (value >= 0.05) DrawDot( CRT_colors[GRAPH_8], y+2, "," );
      else                    DrawDot( CRT_colors[GRAPH_9], y+2, "_" );
   }
   attrset(CRT_colors[RESET_COLOR]);
}

void Meter_setMode(Meter* this, MeterMode mode) {
   Meter_freeBuffer(this);
   switch (mode) {
   case UNSET: {
      // fallthrough to a sane default.
      mode = TEXT;
   }
   case TEXT: {
      this->draw = Meter_drawText;
      this->displayBuffer.rs = malloc(sizeof(RichString));
      this->h = 1;
      break;
   }
   case LED: {
      this->draw = Meter_drawLed;
      this->displayBuffer.rs = malloc(sizeof(RichString));
      this->h = 3;
      break;
   }
   case BAR: {
      this->draw = Meter_drawBar;
      this->displayBuffer.c = malloc(METER_BARBUFFER_LEN);
      this->h = 1;
      break;
   }
   case GRAPH: {
      this->draw = Meter_drawGraph;
      this->displayBuffer.c = calloc(METER_GRAPHBUFFER_LEN, sizeof(double));
      this->h = 3;
      break;
   }
   default: {
      assert(false);
   }
   }
   this->mode = mode;
}

ListItem* Meter_toListItem(Meter* this) {
   char buffer[50]; char* mode = NULL;
   switch (this->mode) {
   case BAR: mode = "Bar"; break;
   case LED: mode = "LED"; break;
   case TEXT: mode = "Text"; break;
   case GRAPH: mode = "Graph"; break;
   default: {
      assert(false);
   }
   }
   sprintf(buffer, "%s [%s]", this->name, mode);
   return ListItem_new(String_copy(buffer)); 
}

© 2014-2024 Faster IT GmbH | imprint | privacy policy