diff --git a/src/common.c b/src/common.c index cfd806f2..765182ff 100644 --- a/src/common.c +++ b/src/common.c @@ -743,3 +743,20 @@ void print_battery_short(struct text_object *obj, char *p, int p_max_size) get_battery_short_status(p, p_max_size, obj->data.s); } #endif /* !__OpenBSD__ */ + +void print_to_bytes(struct text_object *obj, char *p, int p_max_size) +{ + char buf[max_user_text]; + long long bytes; + char unit[16]; // 16 because we can also have long names (like mega-bytes) + + generate_text_internal(buf, max_user_text, *obj->sub, &info); + if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){ + if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes); + else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024); + else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024); + else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024); + else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024); + } + snprintf(p, p_max_size, "%s", buf); +} diff --git a/src/common.h b/src/common.h index a4f86e78..1ea9f26a 100644 --- a/src/common.h +++ b/src/common.h @@ -125,4 +125,6 @@ uint8_t battery_percentage(struct text_object *); void print_battery_short(struct text_object *, char *, int); #endif /* !__OpenBSD__ */ +void print_to_bytes(struct text_object *, char *, int); + #endif /* _COMMON_H */ diff --git a/src/conky.c b/src/conky.c index 47cbcf10..b02e20ab 100644 --- a/src/conky.c +++ b/src/conky.c @@ -1166,21 +1166,6 @@ void generate_text_internal(char *p, int p_max_size, } } } - OBJ(to_bytes) { - char buf[max_user_text]; - long long bytes; - char unit[16]; // 16 because we can also have long names (like mega-bytes) - - generate_text_internal(buf, max_user_text, *obj->sub, cur); - if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){ - if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes); - else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024); - else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024); - else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024); - else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024); - } - snprintf(p, p_max_size, "%s", buf); - } break; } #undef DO_JUMP diff --git a/src/core.c b/src/core.c index f17669dd..a8749a20 100644 --- a/src/core.c +++ b/src/core.c @@ -1461,6 +1461,7 @@ struct text_object *construct_text_object(const char *s, const char *arg, long END OBJ_ARG(to_bytes, 0, "to_bytes needs a argument") obj->sub = malloc(sizeof(struct text_object)); extract_variable_text_internal(obj->sub, arg); + obj->callbacks.print = &print_to_bytes; END OBJ(scroll, 0) #ifdef X11 /* allocate a follower to reset any color changes */