summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/disks.cpp4
-rw-r--r--src/load-graph.cpp32
-rw-r--r--src/memmaps.cpp2
-rw-r--r--src/procproperties.cpp2
-rw-r--r--src/proctable.cpp4
-rw-r--r--src/sysinfo.cpp4
-rw-r--r--src/util.cpp201
-rw-r--r--src/util.h27
8 files changed, 126 insertions, 150 deletions
diff --git a/src/disks.cpp b/src/disks.cpp
index 302098a..3498cae 100644
--- a/src/disks.cpp
+++ b/src/disks.cpp
@@ -417,7 +417,7 @@ create_disk_view(ProcData *procdata)
case DISK_AVAIL:
g_object_set(cell, "xalign", 1.0f, NULL);
gtk_tree_view_column_set_cell_data_func(col, cell,
- &procman::size_cell_data_func,
+ &procman::storage_size_cell_data_func,
GUINT_TO_POINTER(i),
NULL);
break;
@@ -437,7 +437,7 @@ create_disk_view(ProcData *procdata)
g_object_set(cell, "xalign", 1.0f, NULL);
gtk_tree_view_column_pack_start(col, cell, FALSE);
gtk_tree_view_column_set_cell_data_func(col, cell,
- &procman::size_cell_data_func,
+ &procman::storage_size_cell_data_func,
GUINT_TO_POINTER(DISK_USED),
NULL);
diff --git a/src/load-graph.cpp b/src/load-graph.cpp
index b24f977..b1b2e33 100644
--- a/src/load-graph.cpp
+++ b/src/load-graph.cpp
@@ -69,7 +69,7 @@ static void draw_background(LoadGraph *graph) {
cairo_t *cr;
guint i;
unsigned num_bars;
- char *caption;
+ g_autofree gchar *caption;
PangoLayout* layout;
PangoFontDescription* font_desc;
PangoRectangle extents;
@@ -135,8 +135,8 @@ static void draw_background(LoadGraph *graph) {
if (graph->type == LOAD_GRAPH_NET) {
// operation orders matters so it's 0 if i == num_bars
guint64 rate = graph->net.max - (i * graph->net.max / num_bars);
- const std::string caption(procman::format_network_rate(rate, graph->net.max));
- pango_layout_set_text (layout, caption.c_str(), -1);
+ caption = g_format_size_full (rate, ProcData::get_instance()->config.network_in_bits ? G_FORMAT_SIZE_BITS : G_FORMAT_SIZE_DEFAULT);
+ pango_layout_set_text (layout, caption, -1);
pango_layout_get_extents (layout, NULL, &extents);
cairo_move_to (cr, graph->indent - 1.0 * extents.width / PANGO_SCALE + 20, y - 1.0 * extents.height / PANGO_SCALE / 2);
pango_cairo_show_layout (cr, layout);
@@ -147,7 +147,6 @@ static void draw_background(LoadGraph *graph) {
pango_layout_get_extents (layout, NULL, &extents);
cairo_move_to (cr, graph->indent - 1.0 * extents.width / PANGO_SCALE + 20, y - 1.0 * extents.height / PANGO_SCALE / 2);
pango_cairo_show_layout (cr, layout);
- g_free (caption);
}
if (i==0 || i==num_bars)
@@ -182,7 +181,6 @@ static void draw_background(LoadGraph *graph) {
cairo_move_to (cr, ((ceil(x) + 0.5) + graph->rmargin + graph->indent) - (1.0 * extents.width / PANGO_SCALE/2), graph->draw_height - 1.0 * extents.height / PANGO_SCALE);
gdk_cairo_set_source_rgba (cr, &fg);
pango_cairo_show_layout (cr, layout);
- g_free (caption);
}
g_object_unref(layout);
cairo_stroke (cr);
@@ -344,8 +342,8 @@ namespace
char* total_text;
char* text;
- used_text = procman::format_size(used);
- total_text = procman::format_size(total);
+ used_text = g_format_size_full(used, G_FORMAT_SIZE_IEC_UNITS);
+ total_text = g_format_size_full(total, G_FORMAT_SIZE_IEC_UNITS);
if (total == 0) {
text = g_strdup(_("not available"));
} else {
@@ -586,12 +584,24 @@ get_net (LoadGraph *graph)
net_scale(graph, din, dout);
+ bool network_in_bits = ProcData::get_instance()->config.network_in_bits;
+ g_autofree gchar *str=NULL, *formatted_str=NULL;
+
+ str = g_format_size_full (din, network_in_bits ? G_FORMAT_SIZE_BITS : G_FORMAT_SIZE_DEFAULT);
+ formatted_str = g_strdup_printf(_("%s/s"), str);
+ gtk_label_set_text (GTK_LABEL (graph->labels.net_in), formatted_str);
+
+ str = g_format_size_full (in, network_in_bits ? G_FORMAT_SIZE_BITS : G_FORMAT_SIZE_DEFAULT);
+ formatted_str = g_strdup_printf(_("%s/s"), str);
+ gtk_label_set_text (GTK_LABEL (graph->labels.net_in_total), formatted_str);
- gtk_label_set_text (GTK_LABEL (graph->labels.net_in), procman::format_network_rate(din).c_str());
- gtk_label_set_text (GTK_LABEL (graph->labels.net_in_total), procman::format_network(in).c_str());
+ str = g_format_size_full (dout, network_in_bits ? G_FORMAT_SIZE_BITS : G_FORMAT_SIZE_DEFAULT);
+ formatted_str = g_strdup_printf(_("%s/s"), str);
+ gtk_label_set_text (GTK_LABEL (graph->labels.net_out), formatted_str);
- gtk_label_set_text (GTK_LABEL (graph->labels.net_out), procman::format_network_rate(dout).c_str());
- gtk_label_set_text (GTK_LABEL (graph->labels.net_out_total), procman::format_network(out).c_str());
+ str = g_format_size_full (out, network_in_bits ? G_FORMAT_SIZE_BITS : G_FORMAT_SIZE_DEFAULT);
+ formatted_str = g_strdup_printf(_("%s/s"), str);
+ gtk_label_set_text (GTK_LABEL (graph->labels.net_out_total), formatted_str);
}
diff --git a/src/memmaps.cpp b/src/memmaps.cpp
index 6f8a25d..12bd808 100644
--- a/src/memmaps.cpp
+++ b/src/memmaps.cpp
@@ -366,7 +366,7 @@ create_memmapsdata (ProcData *procdata)
case MMAP_COL_SHARED_DIRTY:
case MMAP_COL_VMSZ:
gtk_tree_view_column_set_cell_data_func(col, cell,
- &procman::size_cell_data_func,
+ &procman::memory_size_cell_data_func,
GUINT_TO_POINTER(i),
NULL);
diff --git a/src/procproperties.cpp b/src/procproperties.cpp
index 6af225a..1570bdb 100644
--- a/src/procproperties.cpp
+++ b/src/procproperties.cpp
@@ -98,7 +98,7 @@ format_memsize(guint64 size)
if (size == 0)
return g_strdup(_("N/A"));
else
- return procman::format_size(size);
+ return g_format_size_full(size, G_FORMAT_SIZE_IEC_UNITS);
}
static void
diff --git a/src/proctable.cpp b/src/proctable.cpp
index f2eb3b5..2e0a4ac 100644
--- a/src/proctable.cpp
+++ b/src/proctable.cpp
@@ -362,7 +362,7 @@ proctable_new (ProcData * const procdata)
switch (i) {
case COL_MEMXSERVER:
gtk_tree_view_column_set_cell_data_func(col, cell,
- &procman::size_cell_data_func,
+ &procman::memory_size_cell_data_func,
GUINT_TO_POINTER(i),
NULL);
break;
@@ -372,7 +372,7 @@ proctable_new (ProcData * const procdata)
case COL_MEM:
case COL_MEMWRITABLE:
gtk_tree_view_column_set_cell_data_func(col, cell,
- &procman::size_na_cell_data_func,
+ &procman::memory_size_na_cell_data_func,
GUINT_TO_POINTER(i),
NULL);
break;
diff --git a/src/sysinfo.cpp b/src/sysinfo.cpp
index 4ef988e..594f61f 100644
--- a/src/sysinfo.cpp
+++ b/src/sysinfo.cpp
@@ -915,7 +915,7 @@ procman_create_sysinfo_view(void)
hardware_table = add_section(GTK_BOX(vbox), markup, 1, 2, NULL);
g_free(markup);
- markup = procman::format_size(data->memory_bytes);
+ markup = g_format_size_full(data->memory_bytes, G_FORMAT_SIZE_IEC_UNITS);
add_row(GTK_GRID(hardware_table), _("Memory:"), markup, 0);
g_free(markup);
@@ -932,7 +932,7 @@ procman_create_sysinfo_view(void)
disk_space_table = add_section(GTK_BOX(vbox), markup, 1, 2, NULL);
g_free(markup);
- markup = procman::format_size(data->free_space_bytes);
+ markup = g_format_size(data->free_space_bytes);
add_row(GTK_GRID(disk_space_table), _("Available disk space:"), markup, 0);
g_free(markup);
diff --git a/src/util.cpp b/src/util.cpp
index 174654f..a6f46af 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -133,83 +133,6 @@ procman_make_label_for_mmaps_or_ofiles(const char *format,
return label;
}
-
-
-/**
- * procman::format_size:
- * @size:
- *
- * Formats the file size passed in @bytes in a way that is easy for
- * the user to read. Gives the size in bytes, kibibytes, mebibytes or
- * gibibytes, choosing whatever is appropriate.
- *
- * Returns: a newly allocated string with the size ready to be shown.
- **/
-
-gchar*
-procman::format_size(guint64 size, guint64 max_size, bool want_bits)
-{
-
- enum {
- K_INDEX,
- M_INDEX,
- G_INDEX,
- T_INDEX
- };
-
- struct Format {
- guint64 factor;
- const char* string;
- };
-
- const Format all_formats[2][4] = {
- { { G_GUINT64_CONSTANT(1) << 10, N_("%.1f KiB") },
- { G_GUINT64_CONSTANT(1) << 20, N_("%.1f MiB") },
- { G_GUINT64_CONSTANT(1) << 30, N_("%.1f GiB") },
- { G_GUINT64_CONSTANT(1) << 40, N_("%.1f TiB") } },
- { { G_GUINT64_CONSTANT(1000), N_("%.3g kbit") },
- { G_GUINT64_CONSTANT(1000000), N_("%.3g Mbit") },
- { G_GUINT64_CONSTANT(1000000000), N_("%.3g Gbit") },
- { G_GUINT64_CONSTANT(1000000000000), N_("%.3g Tbit") } }
- };
-
- const Format (&formats)[4] = all_formats[want_bits ? 1 : 0];
-
- if (want_bits) {
- size *= 8;
- max_size *= 8;
- }
-
- if (max_size == 0)
- max_size = size;
-
- if (max_size < formats[K_INDEX].factor) {
- const char *format = (want_bits
- ? dngettext(GETTEXT_PACKAGE, "%u bit", "%u bits", (guint) size)
- : dngettext(GETTEXT_PACKAGE, "%u byte", "%u bytes",(guint) size));
- return g_strdup_printf (format, (guint) size);
- } else {
- guint64 factor;
- const char* format = NULL;
-
- if (max_size < formats[M_INDEX].factor) {
- factor = formats[K_INDEX].factor;
- format = formats[K_INDEX].string;
- } else if (max_size < formats[G_INDEX].factor) {
- factor = formats[M_INDEX].factor;
- format = formats[M_INDEX].string;
- } else if (max_size < formats[T_INDEX].factor) {
- factor = formats[G_INDEX].factor;
- format = formats[G_INDEX].string;
- } else {
- factor = formats[T_INDEX].factor;
- format = formats[T_INDEX].string;
- }
-
- return g_strdup_printf(_(format), size / (double)factor);
- }
-}
-
gchar *
procman::get_nice_level (gint nice)
{
@@ -355,9 +278,9 @@ procman_debug_real(const char *file, int line, const char *func,
namespace procman
{
- void size_cell_data_func(GtkTreeViewColumn *, GtkCellRenderer *renderer,
- GtkTreeModel *model, GtkTreeIter *iter,
- gpointer user_data)
+ void memory_size_cell_data_func(GtkTreeViewColumn *col, GtkCellRenderer *renderer,
+ GtkTreeModel *model, GtkTreeIter *iter,
+ gpointer user_data)
{
const guint index = GPOINTER_TO_UINT(user_data);
@@ -381,20 +304,19 @@ namespace procman
g_value_unset(&value);
- char *str = procman::format_size(size);
+ char *str = g_format_size_full(size, G_FORMAT_SIZE_IEC_UNITS);
g_object_set(renderer, "text", str, NULL);
g_free(str);
}
-
/*
Same as above but handles size == 0 as not available
*/
- void size_na_cell_data_func(GtkTreeViewColumn *, GtkCellRenderer *renderer,
- GtkTreeModel *model, GtkTreeIter *iter,
- gpointer user_data)
+ void memory_size_na_cell_data_func(GtkTreeViewColumn *col, GtkCellRenderer *renderer,
+ GtkTreeModel *model, GtkTreeIter *iter,
+ gpointer user_data)
{
- const guint index = GPOINTER_TO_UINT(user_data);
+ const guint index = GPOINTER_TO_UINT(user_data);
guint64 size;
GValue value = { 0 };
@@ -414,15 +336,87 @@ namespace procman
g_assert_not_reached();
}
+ g_value_unset(&value);
+
+ if (size == 0) {
+ char *str = g_strdup_printf ("<i>%s</i>", _("N/A"));
+ g_object_set(renderer, "markup", str, NULL);
+ g_free(str);
+ }
+ else {
+ char *str = g_format_size_full(size, G_FORMAT_SIZE_IEC_UNITS);
+ g_object_set(renderer, "text", str, NULL);
+ g_free(str);
+ }
+ }
+
+ void storage_size_cell_data_func(GtkTreeViewColumn *col, GtkCellRenderer *renderer,
+ GtkTreeModel *model, GtkTreeIter *iter,
+ gpointer user_data)
+ {
+ const guint index = GPOINTER_TO_UINT(user_data);
+
+ guint64 size;
+ GValue value = { 0 };
+
+ gtk_tree_model_get_value(model, iter, index, &value);
+
+ switch (G_VALUE_TYPE(&value)) {
+ case G_TYPE_ULONG:
+ size = g_value_get_ulong(&value);
+ break;
+
+ case G_TYPE_UINT64:
+ size = g_value_get_uint64(&value);
+ break;
+
+ default:
+ g_assert_not_reached();
+ }
+
g_value_unset(&value);
+ char *str = g_format_size(size);
+ g_object_set(renderer, "text", str, NULL);
+ g_free(str);
+ }
+
+ /*
+ Same as above but handles size == 0 as not available
+ */
+ void storage_size_na_cell_data_func(GtkTreeViewColumn *col, GtkCellRenderer *renderer,
+ GtkTreeModel *model, GtkTreeIter *iter,
+ gpointer user_data)
+ {
+ const guint index = GPOINTER_TO_UINT(user_data);
+
+ guint64 size;
+ GValue value = { 0 };
+
+ gtk_tree_model_get_value(model, iter, index, &value);
+
+ switch (G_VALUE_TYPE(&value)) {
+ case G_TYPE_ULONG:
+ size = g_value_get_ulong(&value);
+ break;
+
+ case G_TYPE_UINT64:
+ size = g_value_get_uint64(&value);
+ break;
+
+ default:
+ g_assert_not_reached();
+ }
+
+ g_value_unset(&value);
+
if (size == 0) {
char *str = g_strdup_printf ("<i>%s</i>", _("N/A"));
g_object_set(renderer, "markup", str, NULL);
g_free(str);
}
- else {
- char *str = procman::format_size(size);
+ else {
+ char *str = g_format_size(size);
g_object_set(renderer, "text", str, NULL);
g_free(str);
}
@@ -574,33 +568,4 @@ namespace procman
g_free(current_value);
}
-
-
- std::string format_rate(guint64 rate, guint64 max_rate, bool want_bits)
- {
- char* bytes = procman::format_size(rate, max_rate, want_bits);
- // xgettext: rate, 10MiB/s or 10Mbit/s
- std::string formatted_rate(make_string(g_strdup_printf(_("%s/s"), bytes)));
- g_free(bytes);
- return formatted_rate;
- }
-
-
- std::string format_network(guint64 rate, guint64 max_rate)
- {
- char* bytes = procman::format_size(rate, max_rate, ProcData::get_instance()->config.network_in_bits);
- std::string formatted(bytes);
- g_free(bytes);
- return formatted;
- }
-
-
- std::string format_network_rate(guint64 rate, guint64 max_rate)
- {
- return procman::format_rate(rate, max_rate, ProcData::get_instance()->config.network_in_bits);
- }
-
-}
-
-
-
+} \ No newline at end of file
diff --git a/src/util.h b/src/util.h
index 40934ae..9b1b498 100644
--- a/src/util.h
+++ b/src/util.h
@@ -46,13 +46,21 @@ namespace procman
{
gchar* format_duration_for_display(unsigned centiseconds);
- void size_cell_data_func(GtkTreeViewColumn *col, GtkCellRenderer *renderer,
- GtkTreeModel *model, GtkTreeIter *iter,
- gpointer user_data);
+ void memory_size_cell_data_func(GtkTreeViewColumn *col, GtkCellRenderer *renderer,
+ GtkTreeModel *model, GtkTreeIter *iter,
+ gpointer user_data);
+
+ void memory_size_na_cell_data_func(GtkTreeViewColumn *col, GtkCellRenderer *renderer,
+ GtkTreeModel *model, GtkTreeIter *iter,
+ gpointer user_data);
+
+ void storage_size_cell_data_func(GtkTreeViewColumn *col, GtkCellRenderer *renderer,
+ GtkTreeModel *model, GtkTreeIter *iter,
+ gpointer user_data);
- void size_na_cell_data_func(GtkTreeViewColumn *col, GtkCellRenderer *renderer,
- GtkTreeModel *model, GtkTreeIter *iter,
- gpointer user_data);
+ void storage_size_na_cell_data_func(GtkTreeViewColumn *col, GtkCellRenderer *renderer,
+ GtkTreeModel *model, GtkTreeIter *iter,
+ gpointer user_data);
void duration_cell_data_func(GtkTreeViewColumn *col, GtkCellRenderer *renderer,
GtkTreeModel *model, GtkTreeIter *iter,
@@ -113,14 +121,7 @@ namespace procman
tree_store_update<const char>(model, iter, column, new_value);
}
- gchar* format_size(guint64 size, guint64 max = 0, bool want_bits = false);
-
gchar* get_nice_level (gint nice);
-
- std::string format_rate(guint64 rate, guint64 max_rate = 0, bool want_bits = false);
-
- std::string format_network(guint64 rate, guint64 max_rate = 0);
- std::string format_network_rate(guint64 rate, guint64 max_rate = 0);
}