Skip to content

Commit

Permalink
Save text buffer in Meter
Browse files Browse the repository at this point in the history
  • Loading branch information
cgzones authored and BenBE committed Mar 4, 2021
1 parent 23c5b9c commit 2d1042a
Show file tree
Hide file tree
Showing 23 changed files with 73 additions and 68 deletions.
6 changes: 3 additions & 3 deletions BatteryMeter.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ static const int BatteryMeter_attributes[] = {
BATTERY
};

static void BatteryMeter_updateValues(Meter* this, char* buffer, size_t len) {
static void BatteryMeter_updateValues(Meter* this) {
ACPresence isOnAC;
double percent;

Platform_getBattery(&percent, &isOnAC);

if (isnan(percent)) {
this->values[0] = NAN;
xSnprintf(buffer, len, "N/A");
xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "N/A");
return;
}

Expand All @@ -49,7 +49,7 @@ static void BatteryMeter_updateValues(Meter* this, char* buffer, size_t len) {
break;
}

xSnprintf(buffer, len, "%.1f%%%s", percent, text);
xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "%.1f%%%s", percent, text);
}

const MeterClass BatteryMeter_class = {
Expand Down
6 changes: 3 additions & 3 deletions CPUMeter.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ static void CPUMeter_init(Meter* this) {
Meter_setCaption(this, "Avg");
}

static void CPUMeter_updateValues(Meter* this, char* buffer, size_t size) {
static void CPUMeter_updateValues(Meter* this) {
int cpu = this->param;
if (cpu > this->pl->cpuCount) {
xSnprintf(buffer, size, "absent");
xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "absent");
for (uint8_t i = 0; i < this->curItems; i++)
this->values[i] = 0;
return;
Expand Down Expand Up @@ -91,7 +91,7 @@ static void CPUMeter_updateValues(Meter* this, char* buffer, size_t size) {
}
#endif

xSnprintf(buffer, size, "%s%s%s%s%s",
xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "%s%s%s%s%s",
cpuUsageBuffer,
(cpuUsageBuffer[0] && (cpuFrequencyBuffer[0] || cpuTemperatureBuffer[0])) ? " " : "",
cpuFrequencyBuffer,
Expand Down
4 changes: 2 additions & 2 deletions ClockMeter.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ static const int ClockMeter_attributes[] = {
CLOCK
};

static void ClockMeter_updateValues(Meter* this, char* buffer, size_t size) {
static void ClockMeter_updateValues(Meter* this) {
time_t t = time(NULL);
struct tm result;
const struct tm* lt = localtime_r(&t, &result);
this->values[0] = lt->tm_hour * 60 + lt->tm_min;
strftime(buffer, size, "%H:%M:%S", lt);
strftime(this->txtBuffer, sizeof(this->txtBuffer), "%H:%M:%S", lt);
}

const MeterClass ClockMeter_class = {
Expand Down
4 changes: 2 additions & 2 deletions DateMeter.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static const int DateMeter_attributes[] = {
DATE
};

static void DateMeter_updateValues(Meter* this, char* buffer, size_t size) {
static void DateMeter_updateValues(Meter* this) {
time_t t = time(NULL);
struct tm result;
const struct tm* lt = localtime_r(&t, &result);
Expand All @@ -30,7 +30,7 @@ static void DateMeter_updateValues(Meter* this, char* buffer, size_t size) {
} else {
this->total = 365;
}
strftime(buffer, size, "%F", lt);
strftime(this->txtBuffer, sizeof(this->txtBuffer), "%F", lt);
}

const MeterClass DateMeter_class = {
Expand Down
4 changes: 2 additions & 2 deletions DateTimeMeter.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static const int DateTimeMeter_attributes[] = {
DATETIME
};

static void DateTimeMeter_updateValues(Meter* this, char* buffer, size_t size) {
static void DateTimeMeter_updateValues(Meter* this) {
time_t t = time(NULL);
struct tm result;
const struct tm* lt = localtime_r(&t, &result);
Expand All @@ -30,7 +30,7 @@ static void DateTimeMeter_updateValues(Meter* this, char* buffer, size_t size) {
this->total = 365;
}
this->values[0] = lt->tm_yday;
strftime(buffer, size, "%F %H:%M:%S", lt);
strftime(this->txtBuffer, sizeof(this->txtBuffer), "%F %H:%M:%S", lt);
}

const MeterClass DateTimeMeter_class = {
Expand Down
6 changes: 3 additions & 3 deletions DiskIOMeter.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static uint32_t cached_read_diff;
static uint32_t cached_write_diff;
static double cached_utilisation_diff;

static void DiskIOMeter_updateValues(Meter* this, char* buffer, size_t len) {
static void DiskIOMeter_updateValues(Meter* this) {
static uint64_t cached_last_update;

struct timeval tv;
Expand All @@ -52,7 +52,7 @@ static void DiskIOMeter_updateValues(Meter* this, char* buffer, size_t len) {
hasData = Platform_getDiskIO(&data);
if (!hasData) {
this->values[0] = 0;
xSnprintf(buffer, len, "no data");
xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "no data");
return;
}

Expand Down Expand Up @@ -89,7 +89,7 @@ static void DiskIOMeter_updateValues(Meter* this, char* buffer, size_t len) {
char bufferRead[12], bufferWrite[12];
Meter_humanUnit(bufferRead, cached_read_diff, sizeof(bufferRead));
Meter_humanUnit(bufferWrite, cached_write_diff, sizeof(bufferWrite));
snprintf(buffer, len, "%sB %sB %.1f%%", bufferRead, bufferWrite, cached_utilisation_diff);
snprintf(this->txtBuffer, sizeof(this->txtBuffer), "%sB %sB %.1f%%", bufferRead, bufferWrite, cached_utilisation_diff);
}

static void DiskIOMeter_display(ATTR_UNUSED const Object* cast, RichString* out) {
Expand Down
4 changes: 2 additions & 2 deletions HostnameMeter.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ static const int HostnameMeter_attributes[] = {
HOSTNAME
};

static void HostnameMeter_updateValues(ATTR_UNUSED Meter* this, char* buffer, size_t size) {
Platform_getHostname(buffer, size);
static void HostnameMeter_updateValues(Meter* this) {
Platform_getHostname(this->txtBuffer, sizeof(this->txtBuffer));
}

const MeterClass HostnameMeter_class = {
Expand Down
8 changes: 4 additions & 4 deletions LoadAverageMeter.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static const int High_attributes[] = {
METER_VALUE_ERROR
};

static void LoadAverageMeter_updateValues(Meter* this, char* buffer, size_t size) {
static void LoadAverageMeter_updateValues(Meter* this) {
Platform_getLoadAverage(&this->values[0], &this->values[1], &this->values[2]);

// only show bar for 1min value
Expand All @@ -54,7 +54,7 @@ static void LoadAverageMeter_updateValues(Meter* this, char* buffer, size_t size
this->total = 2 * this->pl->cpuCount;
}

xSnprintf(buffer, size, "%.2f/%.2f/%.2f", this->values[0], this->values[1], this->values[2]);
xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "%.2f/%.2f/%.2f", this->values[0], this->values[1], this->values[2]);
}

static void LoadAverageMeter_display(const Object* cast, RichString* out) {
Expand All @@ -68,7 +68,7 @@ static void LoadAverageMeter_display(const Object* cast, RichString* out) {
RichString_appendAscii(out, CRT_colors[LOAD_AVERAGE_FIFTEEN], buffer);
}

static void LoadMeter_updateValues(Meter* this, char* buffer, size_t size) {
static void LoadMeter_updateValues(Meter* this) {
double five, fifteen;
Platform_getLoadAverage(&this->values[0], &five, &fifteen);

Expand All @@ -84,7 +84,7 @@ static void LoadMeter_updateValues(Meter* this, char* buffer, size_t size) {
this->total = 2 * this->pl->cpuCount;
}

xSnprintf(buffer, size, "%.2f", this->values[0]);
xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "%.2f", this->values[0]);
}

static void LoadMeter_display(const Object* cast, RichString* out) {
Expand Down
4 changes: 3 additions & 1 deletion MemoryMeter.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ static const int MemoryMeter_attributes[] = {
MEMORY_CACHE
};

static void MemoryMeter_updateValues(Meter* this, char* buffer, size_t size) {
static void MemoryMeter_updateValues(Meter* this) {
char* buffer = this->txtBuffer;
size_t size = sizeof(this->txtBuffer);
int written;

/* available memory is not supported on all platforms */
Expand Down
32 changes: 13 additions & 19 deletions Meter.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ void Meter_setCaption(Meter* this, const char* caption) {
free_and_xStrdup(&this->caption, caption);
}

static inline void Meter_displayBuffer(const Meter* this, const char* buffer, RichString* out) {
static inline void Meter_displayBuffer(const Meter* this, RichString* out) {
if (Object_displayFn(this)) {
Object_display(this, out);
} else {
RichString_writeWide(out, CRT_colors[Meter_attributes(this)[0]], buffer);
RichString_writeWide(out, CRT_colors[Meter_attributes(this)[0]], this->txtBuffer);
}
}

Expand Down Expand Up @@ -153,9 +153,8 @@ ListItem* Meter_toListItem(const Meter* this, bool moving) {

/* ---------- TextMeterMode ---------- */

static void TextMeterMode_draw(Meter* this, int x, int y, int w) {
char buffer[METER_BUFFER_LEN];
Meter_updateValues(this, buffer, sizeof(buffer));
static void TextMeterMode_draw(Meter* this, int x, int y, ATTR_UNUSED int w) {
Meter_updateValues(this);

attrset(CRT_colors[METER_TEXT]);
mvaddnstr(y, x, this->caption, w - 1);
Expand All @@ -168,7 +167,7 @@ static void TextMeterMode_draw(Meter* this, int x, int y, int w) {
return;

RichString_begin(out);
Meter_displayBuffer(this, buffer, &out);
Meter_displayBuffer(this, &out);
RichString_printoffnVal(out, y, x, 0, w - 1);
RichString_end(out);
}
Expand All @@ -178,8 +177,7 @@ static void TextMeterMode_draw(Meter* this, int x, int y, int w) {
static const char BarMeterMode_characters[] = "|#*@$%&.";

static void BarMeterMode_draw(Meter* this, int x, int y, int w) {
char buffer[METER_BUFFER_LEN];
Meter_updateValues(this, buffer, sizeof(buffer));
Meter_updateValues(this);

w -= 2;
attrset(CRT_colors[METER_TEXT]);
Expand All @@ -202,7 +200,7 @@ static void BarMeterMode_draw(Meter* this, int x, int y, int w) {
// Pad with maximal spaces and then calculate needed starting position offset
RichString_begin(bar);
RichString_appendChr(&bar, 0, ' ', w);
RichString_appendWide(&bar, 0, buffer);
RichString_appendWide(&bar, 0, this->txtBuffer);
int startPos = RichString_sizeVal(bar) - w;
if (startPos > w) {
// Text is too large for bar
Expand Down Expand Up @@ -297,7 +295,7 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
this->drawData = xCalloc(1, sizeof(GraphData));
}
GraphData* data = this->drawData;
const int nValues = METER_BUFFER_LEN;
const int nValues = METER_GRAPHDATA_SIZE;

const char* const* GraphMeterMode_dots;
int GraphMeterMode_pixPerRow;
Expand Down Expand Up @@ -328,8 +326,7 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
for (int i = 0; i < nValues - 1; i++)
data->values[i] = data->values[i + 1];

char buffer[METER_BUFFER_LEN];
Meter_updateValues(this, buffer, sizeof(buffer));
Meter_updateValues(this);

double value = 0.0;
for (uint8_t i = 0; i < this->curItems; i++)
Expand Down Expand Up @@ -397,11 +394,10 @@ static void LEDMeterMode_draw(Meter* this, int x, int y, int w) {
#endif
LEDMeterMode_digits = LEDMeterMode_digitsAscii;

char buffer[METER_BUFFER_LEN];
Meter_updateValues(this, buffer, sizeof(buffer));
Meter_updateValues(this);

RichString_begin(out);
Meter_displayBuffer(this, buffer, &out);
Meter_displayBuffer(this, &out);

int yText =
#ifdef HAVE_LIBNCURSESW
Expand Down Expand Up @@ -466,10 +462,8 @@ const MeterMode* const Meter_modes[] = {

/* Blank meter */

static void BlankMeter_updateValues(ATTR_UNUSED Meter* this, char* buffer, size_t size) {
if (size > 0) {
*buffer = 0;
}
static void BlankMeter_updateValues(Meter* this) {
this->txtBuffer[0] = '\0';
}

static void BlankMeter_display(ATTR_UNUSED const Object* cast, RichString* out) {
Expand Down
11 changes: 6 additions & 5 deletions Meter.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ in the source distribution for its full text.
#include "ProcessList.h"


#define METER_BUFFER_LEN 256
#define METER_TXTBUFFER_LEN 256
#define METER_GRAPHDATA_SIZE 256

#define METER_BUFFER_CHECK(buffer, size, written) \
do { \
Expand Down Expand Up @@ -49,7 +50,7 @@ typedef struct Meter_ Meter;
typedef void(*Meter_Init)(Meter*);
typedef void(*Meter_Done)(Meter*);
typedef void(*Meter_UpdateMode)(Meter*, int);
typedef void(*Meter_UpdateValues)(Meter*, char*, size_t);
typedef void(*Meter_UpdateValues)(Meter*);
typedef void(*Meter_Draw)(Meter*, int, int, int);

typedef struct MeterClass_ {
Expand Down Expand Up @@ -77,16 +78,15 @@ typedef struct MeterClass_ {
#define Meter_updateMode(this_, m_) As_Meter(this_)->updateMode((Meter*)(this_), m_)
#define Meter_drawFn(this_) As_Meter(this_)->draw
#define Meter_doneFn(this_) As_Meter(this_)->done
#define Meter_updateValues(this_, buf_, sz_) \
As_Meter(this_)->updateValues((Meter*)(this_), buf_, sz_)
#define Meter_updateValues(this_) As_Meter(this_)->updateValues((Meter*)(this_))
#define Meter_defaultMode(this_) As_Meter(this_)->defaultMode
#define Meter_attributes(this_) As_Meter(this_)->attributes
#define Meter_name(this_) As_Meter(this_)->name
#define Meter_uiName(this_) As_Meter(this_)->uiName

typedef struct GraphData_ {
struct timeval time;
double values[METER_BUFFER_LEN];
double values[METER_GRAPHDATA_SIZE];
} GraphData;

struct Meter_ {
Expand All @@ -102,6 +102,7 @@ struct Meter_ {
const ProcessList* pl;
uint8_t curItems;
const int* curAttributes;
char txtBuffer[METER_TXTBUFFER_LEN];
double* values;
double total;
void* meterData;
Expand Down
6 changes: 3 additions & 3 deletions NetworkIOMeter.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static uint32_t cached_rxp_diff;
static uint32_t cached_txb_diff;
static uint32_t cached_txp_diff;

static void NetworkIOMeter_updateValues(Meter* this, char* buffer, size_t len) {
static void NetworkIOMeter_updateValues(Meter* this) {
static uint64_t cached_last_update = 0;

struct timeval tv;
Expand All @@ -45,7 +45,7 @@ static void NetworkIOMeter_updateValues(Meter* this, char* buffer, size_t len) {
NetworkIOData data;
hasData = Platform_getNetworkIO(&data);
if (!hasData) {
xSnprintf(buffer, len, "no data");
xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "no data");
return;
}

Expand Down Expand Up @@ -95,7 +95,7 @@ static void NetworkIOMeter_updateValues(Meter* this, char* buffer, size_t len) {
char bufferBytesReceived[12], bufferBytesTransmitted[12];
Meter_humanUnit(bufferBytesReceived, cached_rxb_diff, sizeof(bufferBytesReceived));
Meter_humanUnit(bufferBytesTransmitted, cached_txb_diff, sizeof(bufferBytesTransmitted));
xSnprintf(buffer, len, "rx:%siB/s tx:%siB/s", bufferBytesReceived, bufferBytesTransmitted);
xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "rx:%siB/s tx:%siB/s", bufferBytesReceived, bufferBytesTransmitted);
}

static void NetworkIOMeter_display(ATTR_UNUSED const Object* cast, RichString* out) {
Expand Down
4 changes: 3 additions & 1 deletion SwapMeter.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ static const int SwapMeter_attributes[] = {
SWAP_CACHE
};

static void SwapMeter_updateValues(Meter* this, char* buffer, size_t size) {
static void SwapMeter_updateValues(Meter* this) {
char* buffer = this->txtBuffer;
size_t size = sizeof(this->txtBuffer);
int written;
Platform_setSwapValues(this);

Expand Down
4 changes: 2 additions & 2 deletions SysArchMeter.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ in the source distribution for its full text.

static const int SysArchMeter_attributes[] = {HOSTNAME};

static void SysArchMeter_updateValues(ATTR_UNUSED Meter* this, char* buffer, size_t size) {
static void SysArchMeter_updateValues(Meter* this) {
static char* string;

if (string == NULL)
Platform_getRelease(&string);

String_safeStrncpy(buffer, string, size);
String_safeStrncpy(this->txtBuffer, string, sizeof(this->txtBuffer));
}

const MeterClass SysArchMeter_class = {
Expand Down
Loading

0 comments on commit 2d1042a

Please sign in to comment.