diff options
author | Burt P <pburt0@gmail.com> | 2019-07-06 14:33:30 -0500 |
---|---|---|
committer | Leandro A. F. Pereira <leandro@hardinfo.org> | 2019-07-06 13:08:54 -0700 |
commit | 8bc6f1d869eaf14b2340176318d3cf6b45eff3cb (patch) | |
tree | bf3af15276f06f451c6dfcfd3bca7eb2142a6559 /hardinfo | |
parent | 1d22dffebc3ef27b0fd3cf6fc7f0a47124ca5e5d (diff) |
struct info: add field sort option to InfoGroup
Fields are sorted on flatten.
Signed-off-by: Burt P <pburt0@gmail.com>
Diffstat (limited to 'hardinfo')
-rw-r--r-- | hardinfo/info.c | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/hardinfo/info.c b/hardinfo/info.c index 7e49fd73..ad197392 100644 --- a/hardinfo/info.c +++ b/hardinfo/info.c @@ -40,7 +40,7 @@ void info_group_add_fieldsv(struct InfoGroup *group, va_list ap) while (1) { struct InfoField field = va_arg(ap, struct InfoField); - if (field.magic == INFO_LAST_MARKER) + if (!field.name) break; g_array_append_val(group->fields, field); } @@ -138,6 +138,25 @@ void info_set_reload_interval(struct Info *info, int setting) info->reload_interval = setting; } +int _info_field_sort_name_ascending(const struct InfoField *a, const struct InfoField *b) { + return g_strcmp0(a->name, b->name); +} +int _info_field_sort_name_descending(const struct InfoField *a, const struct InfoField *b) { + return g_strcmp0(b->name, a->name); +} +int _info_field_sort_value_ascending(const struct InfoField *a, const struct InfoField *b) { + return g_strcmp0(a->value, b->value); +} +int _info_field_sort_value_descending(const struct InfoField *a, const struct InfoField *b) { + return g_strcmp0(b->value, a->value); +} +int _info_field_sort_tag_ascending(const struct InfoField *a, const struct InfoField *b) { + return g_strcmp0(a->tag, b->tag); +} +int _info_field_sort_tag_descending(const struct InfoField *a, const struct InfoField *b) { + return g_strcmp0(b->tag, a->tag); +} + static void flatten_group(GString *output, const struct InfoGroup *group, guint group_count) { guint i; @@ -145,6 +164,29 @@ static void flatten_group(GString *output, const struct InfoGroup *group, guint if (group->name != NULL) g_string_append_printf(output, "[%s]\n", group->name); + switch(group->sort) { + case INFO_GROUP_SORT_NAME_ASCENDING: + g_array_sort(group->fields, (GCompareFunc)_info_field_sort_name_ascending); + break; + case INFO_GROUP_SORT_NAME_DESCENDING: + g_array_sort(group->fields, (GCompareFunc)_info_field_sort_name_descending); + break; + case INFO_GROUP_SORT_VALUE_ASCENDING: + g_array_sort(group->fields, (GCompareFunc)_info_field_sort_value_ascending); + break; + case INFO_GROUP_SORT_VALUE_DESCENDING: + g_array_sort(group->fields, (GCompareFunc)_info_field_sort_value_descending); + break; + case INFO_GROUP_SORT_TAG_ASCENDING: + g_array_sort(group->fields, (GCompareFunc)_info_field_sort_tag_ascending); + break; + case INFO_GROUP_SORT_TAG_DESCENDING: + g_array_sort(group->fields, (GCompareFunc)_info_field_sort_tag_descending); + break; + default: + break; + } + if (group->fields) { for (i = 0; i < group->fields->len; i++) { struct InfoField field; |