Skip to content

Commit

Permalink
struct info: add field sort option to InfoGroup
Browse files Browse the repository at this point in the history
Fields are sorted on flatten.

Signed-off-by: Burt P <[email protected]>
  • Loading branch information
bp0 authored and lpereira committed Jul 6, 2019
1 parent 1d22dff commit 8bc6f1d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
44 changes: 43 additions & 1 deletion hardinfo/info.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -138,13 +138,55 @@ 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;

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;
Expand Down
18 changes: 14 additions & 4 deletions includes/info.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@
#include <stdarg.h>
#include <glib.h>

enum {
INFO_GROUP_SORT_NONE,
INFO_GROUP_SORT_NAME_ASCENDING,
INFO_GROUP_SORT_NAME_DESCENDING,
INFO_GROUP_SORT_VALUE_ASCENDING,
INFO_GROUP_SORT_VALUE_DESCENDING,
INFO_GROUP_SORT_TAG_ASCENDING,
INFO_GROUP_SORT_TAG_DESCENDING,
};

struct Info {
GArray *groups;

Expand All @@ -37,6 +47,7 @@ struct Info {

struct InfoGroup {
const gchar *name;
int sort;

GArray *fields;

Expand All @@ -55,7 +66,7 @@ struct InfoField {
gboolean report_details; /* show moreinfo() in report (flag:!) */

gboolean free_value_on_flatten;
int magic;
int some_member_with_default_value;
};

struct Info *info_new(void);
Expand All @@ -69,14 +80,13 @@ void info_group_add_fieldsv(struct InfoGroup *group, va_list ap);
struct InfoField info_field_printf(const gchar *name, const gchar *format, ...)
__attribute__((format(printf, 2, 3)));

#define INFO_LAST_MARKER 102
#define info_field_full(...) (struct InfoField) { \
.magic = 3, \
.some_member_with_default_value = 3, \
__VA_ARGS__ \
}
#define info_field(n, v) info_field_full(.name = (n), .value = (v))
#define info_field_update(n, ui) info_field_full(.name = (n), .value = "...", .update_interval = (ui))
#define info_field_last() (struct InfoField){.magic = INFO_LAST_MARKER}
#define info_field_last() (struct InfoField) {}

static inline struct InfoField info_field_with_icon(struct InfoField field, const gchar *icon)
{
Expand Down
1 change: 1 addition & 0 deletions modules/computer.c
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,7 @@ gchar *callback_security(void)
if (dir) {
struct InfoGroup *vulns = info_add_group(info, _("CPU Vulnerabilities"),
info_field_last());
vulns->sort = INFO_GROUP_SORT_NAME_ASCENDING;
const gchar *vuln;

while ((vuln = g_dir_read_name(dir))) {
Expand Down

0 comments on commit 8bc6f1d

Please sign in to comment.