UptimeMeter.c 1.45 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
/*
Hisham Muhammad's avatar
Hisham Muhammad committed
2
htop - UptimeMeter.c
Hisham Muhammad's avatar
Hisham Muhammad committed
3
(C) 2004-2011 Hisham H. Muhammad
Hisham Muhammad's avatar
Hisham Muhammad committed
4
5
6
7
8
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/

#include "UptimeMeter.h"
9
#include "Platform.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
10
11
#include "CRT.h"

Hisham Muhammad's avatar
Hisham Muhammad committed
12
13
14
15
/*{
#include "Meter.h"
}*/

16
17
18
int UptimeMeter_attributes[] = {
   UPTIME
};
19

20
static void UptimeMeter_updateValues(Meter* this, char* buffer, int len) {
21
22
23
24
   int totalseconds = Platform_getUptime();
   if (totalseconds == -1) {
      snprintf(buffer, len, "(unknown)");
      return;
Hisham Muhammad's avatar
Hisham Muhammad committed
25
   }
26
   int seconds = totalseconds % 60;
Hisham Muhammad's avatar
Hisham Muhammad committed
27
28
29
   int minutes = (totalseconds/60) % 60;
   int hours = (totalseconds/3600) % 24;
   int days = (totalseconds/86400);
30
31
32
   this->values[0] = days;
   if (days > this->total) {
      this->total = days;
Hisham Muhammad's avatar
Hisham Muhammad committed
33
   }
34
   char daysbuf[15];
35
   if (days > 100) {
36
      snprintf(daysbuf, sizeof(daysbuf), "%d days(!), ", days);
37
   } else if (days > 1) {
38
      snprintf(daysbuf, sizeof(daysbuf), "%d days, ", days);
39
   } else if (days == 1) {
40
      snprintf(daysbuf, sizeof(daysbuf), "1 day, ");
41
42
   } else {
      daysbuf[0] = '\0';
Hisham Muhammad's avatar
Hisham Muhammad committed
43
   }
44
   snprintf(buffer, len, "%s%02d:%02d:%02d", daysbuf, hours, minutes, seconds);
Hisham Muhammad's avatar
Hisham Muhammad committed
45
}
46

47
48
49
50
51
MeterClass UptimeMeter_class = {
   .super = {
      .extends = Class(Meter),
      .delete = Meter_delete
   },
52
   .updateValues = UptimeMeter_updateValues,
53
   .defaultMode = TEXT_METERMODE,
54
   .maxItems = 1,
55
56
57
58
59
60
   .total = 100.0,
   .attributes = UptimeMeter_attributes,
   .name = "Uptime",
   .uiName = "Uptime",
   .caption = "Uptime: "
};