summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHisham Muhammad <hisham@gobolinux.org>2015-08-27 17:33:24 -0300
committerHisham Muhammad <hisham@gobolinux.org>2015-08-27 17:33:24 -0300
commitf585fc98257dbe1dc92aad6bc46c97d2a24b77d8 (patch)
tree71a8fe569a03e97e3806d70a5ed6363b81a2fcc5
parent79356dc125e85b7107b8fe72a7f46a8b636d629a (diff)
parentcf47f4fca1bab93b81a2fb32cb548cb1722bb0f4 (diff)
Merge pull request #208 from eworm-de/dynamic-unit
Dynamic unit
-rw-r--r--MemoryMeter.c29
-rw-r--r--Meter.c28
-rw-r--r--Meter.h2
-rw-r--r--SwapMeter.c29
-rw-r--r--SwapMeter.h5
5 files changed, 59 insertions, 34 deletions
diff --git a/MemoryMeter.c b/MemoryMeter.c
index f1c1e357..dae56b5e 100644
--- a/MemoryMeter.c
+++ b/MemoryMeter.c
@@ -25,29 +25,32 @@ int MemoryMeter_attributes[] = {
};
static void MemoryMeter_setValues(Meter* this, char* buffer, int size) {
+ int written;
Platform_setMemoryValues(this);
- snprintf(buffer, size, "%ld/%ldM", (long int) this->values[0] / 1024, (long int) this->total / 1024);
+
+ written = Meter_humanUnit(buffer, this->values[0], size);
+ buffer += written;
+ if ((size -= written) > 0) {
+ *buffer++ = '/';
+ size--;
+ Meter_humanUnit(buffer, this->total, size);
+ }
}
static void MemoryMeter_display(Object* cast, RichString* out) {
char buffer[50];
Meter* this = (Meter*)cast;
- int k = 1024; const char* format = "%ldM ";
- long int totalMem = this->total / k;
- long int usedMem = this->values[0] / k;
- long int buffersMem = this->values[1] / k;
- long int cachedMem = this->values[2] / k;
RichString_write(out, CRT_colors[METER_TEXT], ":");
- sprintf(buffer, format, totalMem);
+ Meter_humanUnit(buffer, this->total, 50);
RichString_append(out, CRT_colors[METER_VALUE], buffer);
- sprintf(buffer, format, usedMem);
- RichString_append(out, CRT_colors[METER_TEXT], "used:");
+ Meter_humanUnit(buffer, this->values[0], 50);
+ RichString_append(out, CRT_colors[METER_TEXT], " used:");
RichString_append(out, CRT_colors[MEMORY_USED], buffer);
- sprintf(buffer, format, buffersMem);
- RichString_append(out, CRT_colors[METER_TEXT], "buffers:");
+ Meter_humanUnit(buffer, this->values[1], 50);
+ RichString_append(out, CRT_colors[METER_TEXT], " buffers:");
RichString_append(out, CRT_colors[MEMORY_BUFFERS_TEXT], buffer);
- sprintf(buffer, format, cachedMem);
- RichString_append(out, CRT_colors[METER_TEXT], "cache:");
+ Meter_humanUnit(buffer, this->values[2], 50);
+ RichString_append(out, CRT_colors[METER_TEXT], " cache:");
RichString_append(out, CRT_colors[MEMORY_CACHE], buffer);
}
diff --git a/Meter.c b/Meter.c
index cf4560fe..67ae96ef 100644
--- a/Meter.c
+++ b/Meter.c
@@ -141,6 +141,34 @@ Meter* Meter_new(struct ProcessList_* pl, int param, MeterClass* type) {
return this;
}
+int Meter_humanUnit(char* buffer, unsigned long int value, int size) {
+ const char * prefix = "KMGTPEZY";
+ unsigned long int powi = 1;
+ unsigned int written, powj = 1, precision = 2;
+
+ for(;;) {
+ if (value / 1024 < powi)
+ break;
+
+ if (prefix[1] == 0)
+ break;
+
+ powi *= 1024;
+ ++prefix;
+ }
+
+ for (; precision > 0; precision--) {
+ powj *= 10;
+ if (value / powi < powj)
+ break;
+ }
+
+ written = snprintf(buffer, size, "%.*f%c",
+ precision, (double) value / powi, *prefix);
+
+ return written;
+}
+
void Meter_delete(Object* cast) {
if (!cast)
return;
diff --git a/Meter.h b/Meter.h
index 6b268c3f..fe102fb3 100644
--- a/Meter.h
+++ b/Meter.h
@@ -105,6 +105,8 @@ extern MeterClass Meter_class;
Meter* Meter_new(struct ProcessList_* pl, int param, MeterClass* type);
+int Meter_humanUnit(char* buffer, unsigned long int value, int size);
+
void Meter_delete(Object* cast);
void Meter_setCaption(Meter* this, const char* caption);
diff --git a/SwapMeter.c b/SwapMeter.c
index a19c0300..895ca0be 100644
--- a/SwapMeter.c
+++ b/SwapMeter.c
@@ -20,34 +20,31 @@ in the source distribution for its full text.
#include "Meter.h"
}*/
-#define KILOBYTE 1
-#define MEGABYTE 1024
-#define GIGABYTE 1048576
-
int SwapMeter_attributes[] = {
SWAP
};
-/* NOTE: Value is in kilobytes */
-static void SwapMeter_humanNumber(char* buffer, const long int* value) {
- sprintf(buffer, "%ldM ", *value / MEGABYTE);
-}
-
-static void SwapMeter_setValues(Meter* this, char* buffer, int len) {
+static void SwapMeter_setValues(Meter* this, char* buffer, int size) {
+ int written;
Platform_setSwapValues(this);
- snprintf(buffer, len, "%ld/%ldM", (long int) this->values[0] / MEGABYTE, (long int) this->total / MEGABYTE);
+
+ written = Meter_humanUnit(buffer, this->values[0], size);
+ buffer += written;
+ if ((size -= written) > 0) {
+ *buffer++ = '/';
+ size--;
+ Meter_humanUnit(buffer, this->total, size);
+ }
}
static void SwapMeter_display(Object* cast, RichString* out) {
char buffer[50];
Meter* this = (Meter*)cast;
- long int swap = (long int) this->values[0];
- long int total = (long int) this->total;
RichString_write(out, CRT_colors[METER_TEXT], ":");
- SwapMeter_humanNumber(buffer, &total);
+ Meter_humanUnit(buffer, this->total, 50);
RichString_append(out, CRT_colors[METER_VALUE], buffer);
- SwapMeter_humanNumber(buffer, &swap);
- RichString_append(out, CRT_colors[METER_TEXT], "used:");
+ Meter_humanUnit(buffer, this->values[0], 50);
+ RichString_append(out, CRT_colors[METER_TEXT], " used:");
RichString_append(out, CRT_colors[METER_VALUE], buffer);
}
diff --git a/SwapMeter.h b/SwapMeter.h
index bb46a38c..2b57fe54 100644
--- a/SwapMeter.h
+++ b/SwapMeter.h
@@ -11,13 +11,8 @@ in the source distribution for its full text.
#include "Meter.h"
-#define KILOBYTE 1
-#define MEGABYTE 1024
-#define GIGABYTE 1048576
-
extern int SwapMeter_attributes[];
-/* NOTE: Value is in kilobytes */
extern MeterClass SwapMeter_class;
#endif

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