Save text buffer in Meter

This commit is contained in:
Christian Göttsche 2020-10-06 13:13:16 +02:00 committed by BenBE
parent 23c5b9ce3c
commit 2d1042adb3
23 changed files with 73 additions and 68 deletions

View File

@ -21,7 +21,7 @@ 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;
@ -29,7 +29,7 @@ static void BatteryMeter_updateValues(Meter* this, char* buffer, size_t len) {
if (isnan(percent)) {
this->values[0] = NAN;
xSnprintf(buffer, len, "N/A");
xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "N/A");
return;
}
@ -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 = {

View File

@ -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;
@ -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,

View File

@ -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 = {

View File

@ -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);
@ -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 = {

View File

@ -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);
@ -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 = {

View File

@ -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;
@ -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;
}
@ -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) {

View File

@ -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 = {

View File

@ -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
@ -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) {
@ -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);
@ -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) {

View File

@ -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 */

32
Meter.c
View File

@ -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);
}
}
@ -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);
@ -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);
}
@ -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]);
@ -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
@ -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;
@ -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++)
@ -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
@ -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) {

11
Meter.h
View File

@ -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 { \
@ -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_ {
@ -77,8 +78,7 @@ 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
@ -86,7 +86,7 @@ typedef struct MeterClass_ {
typedef struct GraphData_ {
struct timeval time;
double values[METER_BUFFER_LEN];
double values[METER_GRAPHDATA_SIZE];
} GraphData;
struct Meter_ {
@ -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;

View File

@ -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;
@ -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;
}
@ -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) {

View File

@ -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);

View File

@ -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 = {

View File

@ -23,7 +23,7 @@ static const int TasksMeter_attributes[] = {
TASKS_RUNNING
};
static void TasksMeter_updateValues(Meter* this, char* buffer, size_t len) {
static void TasksMeter_updateValues(Meter* this) {
const ProcessList* pl = this->pl;
this->values[0] = pl->kernelThreads;
this->values[1] = pl->userlandThreads;
@ -35,7 +35,7 @@ static void TasksMeter_updateValues(Meter* this, char* buffer, size_t len) {
if (pl->settings->hideKernelThreads) {
this->values[0] = 0;
}
xSnprintf(buffer, len, "%d/%d", (int) this->values[3], (int) this->total);
xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "%d/%d", (int) this->values[3], (int) this->total);
}
static void TasksMeter_display(const Object* cast, RichString* out) {

View File

@ -17,10 +17,10 @@ static const int UptimeMeter_attributes[] = {
UPTIME
};
static void UptimeMeter_updateValues(Meter* this, char* buffer, size_t len) {
static void UptimeMeter_updateValues(Meter* this) {
int totalseconds = Platform_getUptime();
if (totalseconds == -1) {
xSnprintf(buffer, len, "(unknown)");
xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "(unknown)");
return;
}
int seconds = totalseconds % 60;
@ -41,7 +41,7 @@ static void UptimeMeter_updateValues(Meter* this, char* buffer, size_t len) {
} else {
daysbuf[0] = '\0';
}
xSnprintf(buffer, len, "%s%02d:%02d:%02d", daysbuf, hours, minutes, seconds);
xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "%s%02d:%02d:%02d", daysbuf, hours, minutes, seconds);
}
const MeterClass UptimeMeter_class = {

View File

@ -31,9 +31,11 @@ static const char* const HugePageMeter_labels[] = {
" 1G:", " 2G:", " 4G:", " 8G:", " 16G:", " 32G:", " 64G:", " 128G:", " 256G:", " 512G:",
};
static void HugePageMeter_updateValues(Meter* this, char* buffer, size_t size) {
static void HugePageMeter_updateValues(Meter* this) {
assert(ARRAYSIZE(HugePageMeter_labels) == HTOP_HUGEPAGE_COUNT);
char* buffer = this->txtBuffer;
size_t size = sizeof(this->txtBuffer);
int written;
memory_t usedTotal = 0;
unsigned nextUsed = 0;

View File

@ -25,7 +25,7 @@ static const int PressureStallMeter_attributes[] = {
PRESSURE_STALL_THREEHUNDRED
};
static void PressureStallMeter_updateValues(Meter* this, char* buffer, size_t len) {
static void PressureStallMeter_updateValues(Meter* this) {
const char* file;
if (strstr(Meter_name(this), "CPU")) {
file = "cpu";
@ -47,7 +47,7 @@ static void PressureStallMeter_updateValues(Meter* this, char* buffer, size_t le
/* only print bar for ten (not sixty and threehundred), cause the sum is meaningless */
this->curItems = 1;
xSnprintf(buffer, len, "%s %s %5.2lf%% %5.2lf%% %5.2lf%%", some ? "some" : "full", file, this->values[0], this->values[1], this->values[2]);
xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "%s %s %5.2lf%% %5.2lf%% %5.2lf%%", some ? "some" : "full", file, this->values[0], this->values[1], this->values[2]);
}
static void PressureStallMeter_display(const Object* cast, RichString* out) {

View File

@ -69,11 +69,11 @@ static bool isSelinuxEnforcing(void) {
return !!enforce;
}
static void SELinuxMeter_updateValues(ATTR_UNUSED Meter* this, char* buffer, size_t len) {
static void SELinuxMeter_updateValues(ATTR_UNUSED Meter* this) {
enabled = isSelinuxEnabled();
enforcing = isSelinuxEnforcing();
xSnprintf(buffer, len, "%s%s", enabled ? "enabled" : "disabled", enabled ? (enforcing ? "; mode: enforcing" : "; mode: permissive") : "");
xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "%s%s", enabled ? "enabled" : "disabled", enabled ? (enforcing ? "; mode: enforcing" : "; mode: permissive") : "");
}
const MeterClass SELinuxMeter_class = {

View File

@ -262,7 +262,7 @@ static void updateViaExec(void) {
fclose(commandOutput);
}
static void SystemdMeter_updateValues(ATTR_UNUSED Meter* this, char* buffer, size_t size) {
static void SystemdMeter_updateValues(Meter* this) {
free(systemState);
systemState = NULL;
nFailedUnits = nInstalledJobs = nNames = nJobs = INVALID_VALUE;
@ -274,7 +274,7 @@ static void SystemdMeter_updateValues(ATTR_UNUSED Meter* this, char* buffer, siz
updateViaExec();
#endif /* !BUILD_STATIC || HAVE_LIBSYSTEMD */
xSnprintf(buffer, size, "%s", systemState ? systemState : "???");
xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "%s", systemState ? systemState : "???");
}
static int zeroDigitColor(unsigned int value) {

View File

@ -11,7 +11,9 @@ static const int ZramMeter_attributes[] = {
ZRAM
};
static void ZramMeter_updateValues(Meter* this, char* buffer, size_t size) {
static void ZramMeter_updateValues(Meter* this) {
char* buffer = this->txtBuffer;
size_t size = sizeof(this->txtBuffer);
int written;
Platform_setZramValues(this);

View File

@ -33,7 +33,9 @@ void ZfsArcMeter_readStats(Meter* this, const ZfsArcStats* stats) {
this->values[5] = stats->size;
}
static void ZfsArcMeter_updateValues(Meter* this, char* buffer, size_t size) {
static void ZfsArcMeter_updateValues(Meter* this) {
char* buffer = this->txtBuffer;
size_t size = sizeof(this->txtBuffer);
int written;
Platform_setZfsArcValues(this);

View File

@ -36,10 +36,10 @@ static void ZfsCompressedArcMeter_printRatioString(const Meter* this, char* buff
xSnprintf(buffer, size, "%.2f:1", this->total / this->values[0]);
}
static void ZfsCompressedArcMeter_updateValues(Meter* this, char* buffer, size_t size) {
static void ZfsCompressedArcMeter_updateValues(Meter* this) {
Platform_setZfsCompressedArcValues(this);
ZfsCompressedArcMeter_printRatioString(this, buffer, size);
ZfsCompressedArcMeter_printRatioString(this, this->txtBuffer, sizeof(this->txtBuffer));
}
static void ZfsCompressedArcMeter_display(const Object* cast, RichString* out) {