1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-12-29 05:12:41 +00:00

convert to_bytes to callbacks.print

This commit is contained in:
Phil Sutter 2009-11-29 12:47:11 +01:00
parent 8da8b26ddf
commit dc0af635d6
4 changed files with 20 additions and 15 deletions

View File

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

View File

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

View File

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

View File

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