1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-12-26 04:17:33 +00:00

fs_*: convert to generic object payload

This commit is contained in:
Phil Sutter 2009-10-04 16:53:21 +02:00
parent f31e5cefe7
commit 8d1640f30b
6 changed files with 67 additions and 59 deletions

View File

@ -563,7 +563,7 @@ int spaced_print(char *buf, int size, const char *format, int width, ...)
* *
* - i.e., unsigned values between 0 and 100 * - i.e., unsigned values between 0 and 100
* - respect the value of pad_percents */ * - respect the value of pad_percents */
static int percent_print(char *buf, int size, unsigned value) int percent_print(char *buf, int size, unsigned value)
{ {
return spaced_print(buf, size, "%u", pad_percents, value); return spaced_print(buf, size, "%u", pad_percents, value);
} }
@ -573,7 +573,7 @@ static int percent_print(char *buf, int size, unsigned value)
* The algorithm always divides by 1024, as unit-conversion of byte * The algorithm always divides by 1024, as unit-conversion of byte
* counts suggests. But for output length determination we need to * counts suggests. But for output length determination we need to
* compare with 1000 here, as we print in decimal form. */ * compare with 1000 here, as we print in decimal form. */
static void human_readable(long long num, char *buf, int size) void human_readable(long long num, char *buf, int size)
{ {
const char **suffix = suffixes; const char **suffix = suffixes;
float fnum; float fnum;
@ -1308,51 +1308,25 @@ void generate_text_internal(char *p, int p_max_size,
print_fs_bar(obj, 0, p, p_max_size); print_fs_bar(obj, 0, p, p_max_size);
} }
OBJ(fs_free) { OBJ(fs_free) {
if (obj->data.fs != NULL) { print_fs_free(obj, p, p_max_size);
human_readable(obj->data.fs->avail, p, 255);
}
} }
OBJ(fs_free_perc) { OBJ(fs_free_perc) {
if (obj->data.fs != NULL) { print_fs_perc(obj, 1, p, p_max_size);
int val = 0;
if (obj->data.fs->size) {
val = obj->data.fs->avail * 100 / obj->data.fs->size;
}
percent_print(p, p_max_size, val);
}
} }
OBJ(fs_size) { OBJ(fs_size) {
if (obj->data.fs != NULL) { print_fs_size(obj, p, p_max_size);
human_readable(obj->data.fs->size, p, 255);
}
} }
OBJ(fs_type) { OBJ(fs_type) {
if (obj->data.fs != NULL) print_fs_type(obj, p, p_max_size);
snprintf(p, p_max_size, "%s", obj->data.fs->type);
} }
OBJ(fs_used) { OBJ(fs_used) {
if (obj->data.fs != NULL) { print_fs_used(obj, p, p_max_size);
human_readable(obj->data.fs->size - obj->data.fs->free, p,
255);
}
} }
OBJ(fs_bar_free) { OBJ(fs_bar_free) {
print_fs_bar(obj, 1, p, p_max_size); print_fs_bar(obj, 1, p, p_max_size);
} }
OBJ(fs_used_perc) { OBJ(fs_used_perc) {
if (obj->data.fs != NULL) { print_fs_perc(obj, 0, p, p_max_size);
int val = 0;
if (obj->data.fs->size) {
val = obj->data.fs->free
* 100 /
obj->data.fs->size;
}
percent_print(p, p_max_size, 100 - val);
}
} }
OBJ(loadavg) { OBJ(loadavg) {
float *v = info.loadavg; float *v = info.loadavg;

View File

@ -177,6 +177,9 @@ long get_current_text_color(void);
void set_updatereset(int); void set_updatereset(int);
int get_updatereset(void); int get_updatereset(void);
int percent_print(char *, int, unsigned);
void human_readable(long long, char *, int);
struct conftree { struct conftree {
char* string; char* string;
struct conftree* horz_next; struct conftree* horz_next;

View File

@ -552,35 +552,17 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
END OBJ(fs_bar_free, &update_fs_stats) END OBJ(fs_bar_free, &update_fs_stats)
init_fs_bar(obj, arg); init_fs_bar(obj, arg);
END OBJ(fs_free, &update_fs_stats) END OBJ(fs_free, &update_fs_stats)
if (!arg) { init_fs(obj, arg);
arg = "/";
}
obj->data.fs = prepare_fs_stat(arg);
END OBJ(fs_used_perc, &update_fs_stats) END OBJ(fs_used_perc, &update_fs_stats)
if (!arg) { init_fs(obj, arg);
arg = "/";
}
obj->data.fs = prepare_fs_stat(arg);
END OBJ(fs_free_perc, &update_fs_stats) END OBJ(fs_free_perc, &update_fs_stats)
if (!arg) { init_fs(obj, arg);
arg = "/";
}
obj->data.fs = prepare_fs_stat(arg);
END OBJ(fs_size, &update_fs_stats) END OBJ(fs_size, &update_fs_stats)
if (!arg) { init_fs(obj, arg);
arg = "/";
}
obj->data.fs = prepare_fs_stat(arg);
END OBJ(fs_type, &update_fs_stats) END OBJ(fs_type, &update_fs_stats)
if (!arg) { init_fs(obj, arg);
arg = "/";
}
obj->data.fs = prepare_fs_stat(arg);
END OBJ(fs_used, &update_fs_stats) END OBJ(fs_used, &update_fs_stats)
if (!arg) { init_fs(obj, arg);
arg = "/";
}
obj->data.fs = prepare_fs_stat(arg);
END OBJ(hr, 0) END OBJ(hr, 0)
obj->data.i = arg ? atoi(arg) : 1; obj->data.i = arg ? atoi(arg) : 1;
END OBJ(nameserver, &update_dns_data) END OBJ(nameserver, &update_dns_data)

View File

@ -249,3 +249,45 @@ void print_fs_bar(struct text_object *obj, int be_free_bar, char *p, int p_max_s
new_bar_in_shell(p, p_max_size, (int)(100 * val), fb->w); new_bar_in_shell(p, p_max_size, (int)(100 * val), fb->w);
} }
} }
void init_fs(struct text_object *obj, const char *arg)
{
obj->data.opaque = prepare_fs_stat(arg ? arg : "/");
}
void print_fs_perc(struct text_object *obj, int be_free, char *p, int p_max_size)
{
struct fs_stat *fs = obj->data.opaque;
int val = 100;
if (!fs)
return;
if (fs->size)
val = fs->avail * 100 / fs->size;
if (!be_free)
val = 100 - val;
percent_print(p, p_max_size, val);
}
#define HUMAN_PRINT_FS_GENERATOR(name, expr) \
void print_fs_##name(struct text_object *obj, char *p, int p_max_size) \
{ \
struct fs_stat *fs = obj->data.opaque; \
if (fs) \
human_readable(expr, p, p_max_size); \
}
HUMAN_PRINT_FS_GENERATOR(free, fs->avail)
HUMAN_PRINT_FS_GENERATOR(size, fs->size)
HUMAN_PRINT_FS_GENERATOR(used, fs->size - fs->free)
void print_fs_type(struct text_object *obj, char *p, int p_max_size)
{
struct fs_stat *fs = obj->data.opaque;
if (fs)
snprintf(p, p_max_size, "%s", fs->type);
}

View File

@ -20,6 +20,14 @@ struct text_object;
void init_fs_bar(struct text_object *, const char *); void init_fs_bar(struct text_object *, const char *);
void print_fs_bar(struct text_object *, int, char *, int); void print_fs_bar(struct text_object *, int, char *, int);
void init_fs(struct text_object *, const char *);
void print_fs_perc(struct text_object *, int, char *, int);
void print_fs_free(struct text_object *, char *, int);
void print_fs_size(struct text_object *, char *, int);
void print_fs_used(struct text_object *, char *, int);
void print_fs_type(struct text_object *, char *, int);
void update_fs_stats(void); void update_fs_stats(void);
struct fs_stat *prepare_fs_stat(const char *path); struct fs_stat *prepare_fs_stat(const char *path);
void clear_fs_stats(void); void clear_fs_stats(void);

View File

@ -444,7 +444,6 @@ struct text_object {
long l; /* some other integer */ long l; /* some other integer */
unsigned int sensor; unsigned int sensor;
struct net_stat *net; struct net_stat *net;
struct fs_stat *fs;
struct diskio_stat *diskio; struct diskio_stat *diskio;
unsigned char loadavg[3]; unsigned char loadavg[3];
unsigned int cpu_index; unsigned int cpu_index;