aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBurt P <pburt0@gmail.com>2019-07-06 14:33:30 -0500
committerLeandro A. F. Pereira <leandro@hardinfo.org>2019-07-06 13:08:54 -0700
commit8bc6f1d869eaf14b2340176318d3cf6b45eff3cb (patch)
treebf3af15276f06f451c6dfcfd3bca7eb2142a6559
parent1d22dffebc3ef27b0fd3cf6fc7f0a47124ca5e5d (diff)
struct info: add field sort option to InfoGroup
Fields are sorted on flatten. Signed-off-by: Burt P <pburt0@gmail.com>
-rw-r--r--hardinfo/info.c44
-rw-r--r--includes/info.h18
-rw-r--r--modules/computer.c1
3 files changed, 58 insertions, 5 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;
diff --git a/includes/info.h b/includes/info.h
index 4e86c146..f2d442f4 100644
--- a/includes/info.h
+++ b/includes/info.h
@@ -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;
@@ -37,6 +47,7 @@ struct Info {
struct InfoGroup {
const gchar *name;
+ int sort;
GArray *fields;
@@ -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);
@@ -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)
{
diff --git a/modules/computer.c b/modules/computer.c
index 3e13ff10..5db291b2 100644
--- a/modules/computer.c
+++ b/modules/computer.c
@@ -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))) {