1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-12-27 04:32:55 +00:00

make use_spacer an enum

It was a static int before, holding the (already defined) enum's value,
which doesn't make sense. Also it's used only inside conky.c. Since
enums can't be assigned other values than what are defined, we can skip
checking for illegal spacer value at each invocation of spaced_print,
and then also drop the function name argument, which apparently didn't
make sense at all, because use_spacer is a global option.
This commit is contained in:
Phil Sutter 2008-12-16 02:56:26 +01:00
parent 0720a4fbc9
commit d744f9bfbb
2 changed files with 51 additions and 59 deletions

View File

@ -118,7 +118,11 @@ static char *tmpstring1, *tmpstring2;
/* variables holding various config settings */ /* variables holding various config settings */
int short_units; int short_units;
int cpu_separate; int cpu_separate;
static int use_spacer; enum {
NO_SPACER = 0,
LEFT_SPACER,
RIGHT_SPACER
} use_spacer;
int top_cpu, top_mem; int top_cpu, top_mem;
#define TO_X 1 #define TO_X 1
#define TO_STDOUT 2 #define TO_STDOUT 2
@ -1068,11 +1072,10 @@ static void convert_escapes(char *buf)
/* Prints anything normally printed with snprintf according to the current value /* Prints anything normally printed with snprintf according to the current value
* of use_spacer. Actually slightly more flexible than snprintf, as you can * of use_spacer. Actually slightly more flexible than snprintf, as you can
* safely specify the destination buffer as one of your inputs. */ * safely specify the destination buffer as one of your inputs. */
static int spaced_print(char *, int, const char *, int, const char *, ...) static int spaced_print(char *, int, const char *, int, ...)
__attribute__((format(printf, 3, 6))); __attribute__((format(printf, 3, 5)));
static int spaced_print(char *buf, int size, const char *format, int width, static int spaced_print(char *buf, int size, const char *format, int width, ...) {
const char *func_name, ...) {
int len; int len;
va_list argp; va_list argp;
char *tempbuf; char *tempbuf;
@ -1083,7 +1086,7 @@ static int spaced_print(char *buf, int size, const char *format, int width,
tempbuf = malloc(size * sizeof(char)); tempbuf = malloc(size * sizeof(char));
// Passes the varargs along to vsnprintf // Passes the varargs along to vsnprintf
va_start(argp, func_name); va_start(argp, width);
vsnprintf(tempbuf, size, format, argp); vsnprintf(tempbuf, size, format, argp);
va_end(argp); va_end(argp);
@ -1097,18 +1100,13 @@ static int spaced_print(char *buf, int size, const char *format, int width,
case RIGHT_SPACER: case RIGHT_SPACER:
len = snprintf(buf, size, "%-*s", width - 1, tempbuf); len = snprintf(buf, size, "%-*s", width - 1, tempbuf);
break; break;
default:
CRIT_ERR("%s encountered invalid use_spacer value (%d)", func_name,
use_spacer);
} }
free(tempbuf); free(tempbuf);
return len; return len;
} }
/* converts from bytes to human readable format (k, M, G, T) */ /* converts from bytes to human readable format (k, M, G, T) */
static void human_readable(long long num, char *buf, int size, const char *func_name) static void human_readable(long long num, char *buf, int size)
{ {
const char **suffix = suffixes; const char **suffix = suffixes;
float fnum; float fnum;
@ -1117,10 +1115,9 @@ static void human_readable(long long num, char *buf, int size, const char *func_
if (num < 1024LL && num > -1024LL) { if (num < 1024LL && num > -1024LL) {
if (short_units) { if (short_units) {
spaced_print(buf, size, "%lld%c", SHORT_WIDTH, func_name, num, spaced_print(buf, size, "%lld%c", SHORT_WIDTH, num, **suffix);
**suffix);
} else { } else {
spaced_print(buf, size, "%lld%s", WIDTH, func_name, num, *suffix); spaced_print(buf, size, "%lld%s", WIDTH, num, *suffix);
} }
return; return;
} }
@ -1140,10 +1137,10 @@ static void human_readable(long long num, char *buf, int size, const char *func_
break; break;
} }
if (short_units) { if (short_units) {
len = spaced_print(buf, size, "%.*f%c", SHORT_WIDTH, func_name, len = spaced_print(buf, size, "%.*f%c", SHORT_WIDTH,
precision, fnum, **suffix); precision, fnum, **suffix);
} else { } else {
len = spaced_print(buf, size, "%.*f%s", WIDTH, func_name, precision, len = spaced_print(buf, size, "%.*f%s", WIDTH, precision,
fnum, *suffix); fnum, *suffix);
} }
} while (len >= (short_units ? SHORT_WIDTH : WIDTH) || len >= size); } while (len >= (short_units ? SHORT_WIDTH : WIDTH) || len >= size);
@ -3814,22 +3811,20 @@ static void generate_text_internal(char *p, int p_max_size,
snprintf(p, p_max_size, "%s", obj->data.net->ap); snprintf(p, p_max_size, "%s", obj->data.net->ap);
} }
OBJ(wireless_link_qual) { OBJ(wireless_link_qual) {
spaced_print(p, p_max_size, "%d", 4, "wireless_link_qual", spaced_print(p, p_max_size, "%d", 4,
obj->data.net->link_qual); obj->data.net->link_qual);
} }
OBJ(wireless_link_qual_max) { OBJ(wireless_link_qual_max) {
spaced_print(p, p_max_size, "%d", 4, spaced_print(p, p_max_size, "%d", 4,
"wireless_link_qual_max", obj->data.net->link_qual_max); obj->data.net->link_qual_max);
} }
OBJ(wireless_link_qual_perc) { OBJ(wireless_link_qual_perc) {
if (obj->data.net->link_qual_max > 0) { if (obj->data.net->link_qual_max > 0) {
spaced_print(p, p_max_size, "%.0f", 5, spaced_print(p, p_max_size, "%.0f", 5,
"wireless_link_qual_perc",
(double) obj->data.net->link_qual / (double) obj->data.net->link_qual /
obj->data.net->link_qual_max * 100); obj->data.net->link_qual_max * 100);
} else { } else {
spaced_print(p, p_max_size, "unk", 5, spaced_print(p, p_max_size, "unk", 5);
"wireless_link_qual_perc");
} }
} }
OBJ(wireless_link_bar) { OBJ(wireless_link_bar) {
@ -3860,7 +3855,7 @@ static void generate_text_internal(char *p, int p_max_size,
get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME); get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
} }
OBJ(battery_percent) { OBJ(battery_percent) {
spaced_print(p, p_max_size, "%*d", 4, "battery_percent", spaced_print(p, p_max_size, "%*d", 4,
pad_percents, get_battery_perct(obj->data.s)); pad_percents, get_battery_perct(obj->data.s));
} }
OBJ(battery_bar) { OBJ(battery_bar) {
@ -3869,10 +3864,10 @@ static void generate_text_internal(char *p, int p_max_size,
#endif /* __OpenBSD__ */ #endif /* __OpenBSD__ */
OBJ(buffers) { OBJ(buffers) {
human_readable(cur->buffers * 1024, p, 255, "buffers"); human_readable(cur->buffers * 1024, p, 255);
} }
OBJ(cached) { OBJ(cached) {
human_readable(cur->cached * 1024, p, 255, "buffers"); human_readable(cur->cached * 1024, p, 255);
} }
OBJ(cpu) { OBJ(cpu) {
if (obj->data.cpu_index > info.cpu_count) { if (obj->data.cpu_index > info.cpu_count) {
@ -3880,7 +3875,7 @@ static void generate_text_internal(char *p, int p_max_size,
obj->data.cpu_index, info.cpu_count); obj->data.cpu_index, info.cpu_count);
CRIT_ERR("attempting to use more CPUs than you have!"); CRIT_ERR("attempting to use more CPUs than you have!");
} }
spaced_print(p, p_max_size, "%*d", 4, "cpu", pad_percents, spaced_print(p, p_max_size, "%*d", 4, pad_percents,
round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100.0)); round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100.0));
} }
OBJ(cpubar) { OBJ(cpubar) {
@ -4085,15 +4080,15 @@ static void generate_text_internal(char *p, int p_max_size,
* (or get rid of it??) */ * (or get rid of it??) */
OBJ(diskio) { OBJ(diskio) {
human_readable((obj->data.diskio->current / update_interval) * 1024LL, human_readable((obj->data.diskio->current / update_interval) * 1024LL,
p, p_max_size, "diskio"); p, p_max_size);
} }
OBJ(diskio_write) { OBJ(diskio_write) {
human_readable((obj->data.diskio->current / update_interval) * 1024LL, human_readable((obj->data.diskio->current / update_interval) * 1024LL,
p, p_max_size, "diskio_write"); p, p_max_size);
} }
OBJ(diskio_read) { OBJ(diskio_read) {
human_readable((obj->data.diskio->current / update_interval) * 1024LL, human_readable((obj->data.diskio->current / update_interval) * 1024LL,
p, p_max_size, "diskio_read"); p, p_max_size);
} }
OBJ(diskiograph) { OBJ(diskiograph) {
new_graph(p, obj->a, obj->b, obj->c, obj->d, new_graph(p, obj->a, obj->b, obj->c, obj->d,
@ -4108,11 +4103,11 @@ static void generate_text_internal(char *p, int p_max_size,
obj->data.diskio->current_write, obj->e, 1, obj->showaslog); obj->data.diskio->current_write, obj->e, 1, obj->showaslog);
} }
OBJ(downspeed) { OBJ(downspeed) {
spaced_print(p, p_max_size, "%d", 6, "downspeed", spaced_print(p, p_max_size, "%d", 6,
round_to_int(obj->data.net->recv_speed / 1024)); round_to_int(obj->data.net->recv_speed / 1024));
} }
OBJ(downspeedf) { OBJ(downspeedf) {
spaced_print(p, p_max_size, "%.1f", 8, "downspeedf", spaced_print(p, p_max_size, "%.1f", 8,
obj->data.net->recv_speed / 1024.0); obj->data.net->recv_speed / 1024.0);
} }
OBJ(downspeedgraph) { OBJ(downspeedgraph) {
@ -4375,13 +4370,13 @@ static void generate_text_internal(char *p, int p_max_size,
} }
OBJ(fs_free) { OBJ(fs_free) {
if (obj->data.fs != NULL) { if (obj->data.fs != NULL) {
human_readable(obj->data.fs->avail, p, 255, "fs_free"); human_readable(obj->data.fs->avail, p, 255);
} }
} }
OBJ(fs_free_perc) { OBJ(fs_free_perc) {
if (obj->data.fs != NULL) { if (obj->data.fs != NULL) {
if (obj->data.fs->size) { if (obj->data.fs->size) {
spaced_print(p, p_max_size, "%*d", 4, "fs_free_perc", spaced_print(p, p_max_size, "%*d", 4,
pad_percents, (int) ((obj->data.fs->avail * 100) / pad_percents, (int) ((obj->data.fs->avail * 100) /
obj->data.fs->size)); obj->data.fs->size));
} else { } else {
@ -4391,7 +4386,7 @@ static void generate_text_internal(char *p, int p_max_size,
} }
OBJ(fs_size) { OBJ(fs_size) {
if (obj->data.fs != NULL) { if (obj->data.fs != NULL) {
human_readable(obj->data.fs->size, p, 255, "fs_size"); human_readable(obj->data.fs->size, p, 255);
} }
} }
OBJ(fs_type) { OBJ(fs_type) {
@ -4401,8 +4396,7 @@ static void generate_text_internal(char *p, int p_max_size,
OBJ(fs_used) { OBJ(fs_used) {
if (obj->data.fs != NULL) { if (obj->data.fs != NULL) {
human_readable(obj->data.fs->size - (obj->data.fs->free human_readable(obj->data.fs->size - (obj->data.fs->free
? obj->data.fs->free : obj->data.fs->avail), p, 255, ? obj->data.fs->free : obj->data.fs->avail), p, 255);
"fs_used");
} }
} }
OBJ(fs_bar_free) { OBJ(fs_bar_free) {
@ -4419,7 +4413,7 @@ static void generate_text_internal(char *p, int p_max_size,
OBJ(fs_used_perc) { OBJ(fs_used_perc) {
if (obj->data.fs != NULL) { if (obj->data.fs != NULL) {
if (obj->data.fs->size) { if (obj->data.fs->size) {
spaced_print(p, 4, "%*d", 4, "fs_used_perc", spaced_print(p, 4, "%*d", 4,
pad_percents, 100 - ((int) ((obj->data.fs->avail * 100) / pad_percents, 100 - ((int) ((obj->data.fs->avail * 100) /
obj->data.fs->size))); obj->data.fs->size)));
} else { } else {
@ -4666,20 +4660,20 @@ static void generate_text_internal(char *p, int p_max_size,
/* memory stuff */ /* memory stuff */
OBJ(mem) { OBJ(mem) {
human_readable(cur->mem * 1024, p, 255, "mem"); human_readable(cur->mem * 1024, p, 255);
} }
OBJ(memeasyfree) { OBJ(memeasyfree) {
human_readable(cur->memeasyfree * 1024, p, 255, "memeasyfree"); human_readable(cur->memeasyfree * 1024, p, 255);
} }
OBJ(memfree) { OBJ(memfree) {
human_readable(cur->memfree * 1024, p, 255, "memfree"); human_readable(cur->memfree * 1024, p, 255);
} }
OBJ(memmax) { OBJ(memmax) {
human_readable(cur->memmax * 1024, p, 255, "memmax"); human_readable(cur->memmax * 1024, p, 255);
} }
OBJ(memperc) { OBJ(memperc) {
if (cur->memmax) { if (cur->memmax) {
spaced_print(p, p_max_size, "%*llu", 4, "memperc", spaced_print(p, p_max_size, "%*llu", 4,
pad_percents, cur->mem * 100 / cur->memmax); pad_percents, cur->mem * 100 / cur->memmax);
} }
} }
@ -4746,10 +4740,10 @@ static void generate_text_internal(char *p, int p_max_size,
new_outline(p, obj->data.l); new_outline(p, obj->data.l);
} }
OBJ(processes) { OBJ(processes) {
spaced_print(p, p_max_size, "%hu", 5, "processes", cur->procs); spaced_print(p, p_max_size, "%hu", 5, cur->procs);
} }
OBJ(running_processes) { OBJ(running_processes) {
spaced_print(p, p_max_size, "%hu", 3, "running_processes", cur->run_procs); spaced_print(p, p_max_size, "%hu", 3, cur->run_procs);
} }
OBJ(text) { OBJ(text) {
snprintf(p, p_max_size, "%s", obj->data.s); snprintf(p, p_max_size, "%s", obj->data.s);
@ -4761,16 +4755,16 @@ static void generate_text_internal(char *p, int p_max_size,
new_stippled_hr(p, obj->data.pair.a, obj->data.pair.b); new_stippled_hr(p, obj->data.pair.a, obj->data.pair.b);
} }
OBJ(swap) { OBJ(swap) {
human_readable(cur->swap * 1024, p, 255, "swap"); human_readable(cur->swap * 1024, p, 255);
} }
OBJ(swapmax) { OBJ(swapmax) {
human_readable(cur->swapmax * 1024, p, 255, "swapmax"); human_readable(cur->swapmax * 1024, p, 255);
} }
OBJ(swapperc) { OBJ(swapperc) {
if (cur->swapmax == 0) { if (cur->swapmax == 0) {
strncpy(p, "No swap", p_max_size); strncpy(p, "No swap", p_max_size);
} else { } else {
spaced_print(p, p_max_size, "%*llu", 4, "swapperc", spaced_print(p, p_max_size, "%*llu", 4,
pad_percents, cur->swap * 100 / cur->swapmax); pad_percents, cur->swap * 100 / cur->swapmax);
} }
} }
@ -4818,20 +4812,20 @@ static void generate_text_internal(char *p, int p_max_size,
// Needless to free oldTZ since getenv gives ptr to static data // Needless to free oldTZ since getenv gives ptr to static data
} }
OBJ(totaldown) { OBJ(totaldown) {
human_readable(obj->data.net->recv, p, 255, "totaldown"); human_readable(obj->data.net->recv, p, 255);
} }
OBJ(totalup) { OBJ(totalup) {
human_readable(obj->data.net->trans, p, 255, "totalup"); human_readable(obj->data.net->trans, p, 255);
} }
OBJ(updates) { OBJ(updates) {
snprintf(p, p_max_size, "%d", total_updates); snprintf(p, p_max_size, "%d", total_updates);
} }
OBJ(upspeed) { OBJ(upspeed) {
spaced_print(p, p_max_size, "%d", 6, "upspeed", spaced_print(p, p_max_size, "%d", 6,
round_to_int(obj->data.net->trans_speed / 1024)); round_to_int(obj->data.net->trans_speed / 1024));
} }
OBJ(upspeedf) { OBJ(upspeedf) {
spaced_print(p, p_max_size, "%.1f", 8, "upspeedf", spaced_print(p, p_max_size, "%.1f", 8,
obj->data.net->trans_speed / 1024.0); obj->data.net->trans_speed / 1024.0);
} }
OBJ(upspeedgraph) { OBJ(upspeedgraph) {
@ -4940,7 +4934,7 @@ static void generate_text_internal(char *p, int p_max_size,
format_media_player_time(p, p_max_size, cur->mpd.length); format_media_player_time(p, p_max_size, cur->mpd.length);
} }
OBJ(mpd_percent) { OBJ(mpd_percent) {
spaced_print(p, p_max_size, "%*d", 4, "mpd_percent", spaced_print(p, p_max_size, "%*d", 4,
pad_percents, (int) (cur->mpd.progress * 100)); pad_percents, (int) (cur->mpd.progress * 100));
} }
OBJ(mpd_bar) { OBJ(mpd_bar) {
@ -5198,11 +5192,11 @@ static void generate_text_internal(char *p, int p_max_size,
break; break;
case TOP_MEM_RES: case TOP_MEM_RES:
human_readable(cur->cpu[obj->data.top.num]->rss, human_readable(cur->cpu[obj->data.top.num]->rss,
p, 255, "top_rss"); p, 255);
break; break;
case TOP_MEM_VSIZE: case TOP_MEM_VSIZE:
human_readable(cur->cpu[obj->data.top.num]->vsize, human_readable(cur->cpu[obj->data.top.num]->vsize,
p, 255, "top_rss"); p, 255);
break; break;
default: default:
ERR("Unhandled top data type: %d\n", ERR("Unhandled top data type: %d\n",
@ -5242,11 +5236,11 @@ static void generate_text_internal(char *p, int p_max_size,
break; break;
case TOP_MEM_RES: case TOP_MEM_RES:
human_readable(cur->cpu[obj->data.top.num]->rss, human_readable(cur->cpu[obj->data.top.num]->rss,
p, 255, "top_rss"); p, 255);
break; break;
case TOP_MEM_VSIZE: case TOP_MEM_VSIZE:
human_readable(cur->cpu[obj->data.top.num]->vsize, human_readable(cur->cpu[obj->data.top.num]->vsize,
p, 255, "top_rss"); p, 255);
break; break;
default: default:
ERR("Unhandled top data type: %d\n", ERR("Unhandled top data type: %d\n",
@ -5490,7 +5484,7 @@ head:
if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) { if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
val = smapi_bat_installed(idx) ? val = smapi_bat_installed(idx) ?
smapi_get_bat_int(idx, "remaining_percent") : 0; smapi_get_bat_int(idx, "remaining_percent") : 0;
spaced_print(p, p_max_size, "%*d", 4, "smapi_bat_perc", pad_percents, val); spaced_print(p, p_max_size, "%*d", 4, pad_percents, val);
} else } else
ERR("argument to smapi_bat_perc must be an integer"); ERR("argument to smapi_bat_perc must be an integer");
} }

View File

@ -286,8 +286,6 @@ enum {
/* defined in conky.c, needed by top.c */ /* defined in conky.c, needed by top.c */
extern int top_cpu, top_mem; extern int top_cpu, top_mem;
enum spacer_opts { NO_SPACER = 0, LEFT_SPACER, RIGHT_SPACER };
/* defined in conky.c, needed by top.c */ /* defined in conky.c, needed by top.c */
extern int cpu_separate; extern int cpu_separate;