3
(C) 2004 Hisham H. Muhammad
4
Released under the GNU GPL, see the COPYING file
5
in the source distribution for its full text.
8
#include "UptimeMeter.h"
11
#include "ProcessList.h"
19
typedef struct UptimeMeter_ UptimeMeter;
32
UptimeMeter* UptimeMeter_new() {
33
UptimeMeter* this = malloc(sizeof(UptimeMeter));
34
Meter_init((Meter*)this, String_copy("Uptime"), String_copy("Uptime: "), 1);
35
((Meter*)this)->attributes[0] = CRT_colors[UPTIME];
36
((Object*)this)->display = UptimeMeter_display;
37
((Meter*)this)->setValues = UptimeMeter_setValues;
38
Meter_setMode((Meter*)this, TEXT);
39
((Meter*)this)->total = 100.0;
43
void UptimeMeter_setValues(Meter* cast) {
44
UptimeMeter* this = (UptimeMeter*)cast;
46
FILE* fd = fopen(PROCDIR "/uptime", "r");
47
fscanf(fd, "%lf", &uptime);
49
int totalseconds = (int) ceil(uptime);
50
this->seconds = totalseconds % 60;
51
this->minutes = (totalseconds-this->seconds) % 3600 / 60;
52
this->hours = (totalseconds-this->seconds-(this->minutes*60)) % 86400 / 3600;
53
this->days = (totalseconds-this->seconds-(this->minutes*60)-(this->hours*3600)) / 86400;
54
cast->values[0] = this->days;
55
if (this->days > cast->total) {
56
cast->total = this->days;
58
snprintf(cast->displayBuffer.c, 14, "%d", this->days);
61
void UptimeMeter_display(Object* cast, RichString* out) {
62
UptimeMeter* this = (UptimeMeter*)cast;
64
RichString_prune(out);
65
if (this->days > 100) {
66
sprintf(buffer, "%d days, ", this->days);
67
RichString_write(out, CRT_colors[LARGE_NUMBER], buffer);
68
} else if (this->days > 1) {
69
sprintf(buffer, "%d days, ", this->days);
70
RichString_write(out, CRT_colors[UPTIME], buffer);
71
} else if (this->days == 1) {
72
sprintf(buffer, "%d day, ", this->days);
73
RichString_write(out, CRT_colors[UPTIME], buffer);
75
sprintf(buffer, "%02d:%02d:%02d ", this->hours, this->minutes, this->seconds);
76
RichString_append(out, CRT_colors[UPTIME], buffer);