Skip to content

Commit 6f15030

Browse files
danirabbitstsdc
andauthored
Use GLib function for formatting size (#433)
* Use GLib function for formatting size * use format size in indicator --------- Co-authored-by: Stanisław <[email protected]>
1 parent 91343e8 commit 6f15030

File tree

8 files changed

+23
-79
lines changed

8 files changed

+23
-79
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
meson valac sassc git
3434
- name: Build
3535
run: |
36-
meson setup build
36+
meson setup -Dindicator-wingpanel=enabled build
3737
meson compile -C build
3838
# Tests disabled since it starts to test live-chart and some tests fail there
3939
# meson test -C build --print-errorlogs
@@ -48,4 +48,4 @@ jobs:
4848
steps:
4949
- uses: actions/checkout@v4
5050
- name: Lint
51-
run: io.elementary.vala-lint -d .
51+
run: io.elementary.vala-lint -d .

src/Indicator/Widgets/IndicatorWidget.vala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ public class Monitor.IndicatorWidget : Gtk.Box {
3434
}
3535
}
3636

37-
public int state_bandwidth {
37+
public uint64 state_bandwidth {
3838
set {
39-
label.label = ("%s").printf (Utils.HumanUnitFormatter.string_bytes_to_human (value.to_string (), true));
39+
label.label = format_size (value);
4040
}
4141
}
4242

src/Utils.vala

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -44,65 +44,6 @@ public class Monitor.Utils.Strings {
4444

4545
}
4646

47-
/**
48-
* Static helper class for unit formatting
49-
* Author: Laurent Callarec @lcallarec
50-
*/
51-
public class Monitor.Utils.HumanUnitFormatter {
52-
const string[] SIZE_UNITS = { "B", "KiB", "MiB", "GiB", "TiB" };
53-
const string[] PACKED_SIZE_UNITS = { "B", "K", "M", "G", "T" };
54-
const double KFACTOR = 1024;
55-
56-
/**
57-
* format a string of bytes to an human readable format with units
58-
*/
59-
public static string string_bytes_to_human (string bytes, bool packed = false) {
60-
string[] units;
61-
62-
if (packed)
63-
units = HumanUnitFormatter.PACKED_SIZE_UNITS;
64-
else
65-
units = HumanUnitFormatter.SIZE_UNITS;
66-
67-
double current_size = double.parse (bytes);
68-
string current_size_formatted = bytes.to_string () + units[0];
69-
70-
for (int i = 0; i <= units.length; i++) {
71-
if (current_size < HumanUnitFormatter.KFACTOR) {
72-
return GLib.Math.round (current_size).to_string () + units[i];
73-
}
74-
current_size = current_size / HumanUnitFormatter.KFACTOR;
75-
}
76-
77-
return current_size_formatted;
78-
}
79-
80-
public static string double_bytes_to_human (double bytes) {
81-
string units = _("B");
82-
83-
// convert to MiB if needed
84-
if (bytes > 1024.0) {
85-
bytes /= 1024.0;
86-
units = _("KiB");
87-
}
88-
89-
// convert to GiB if needed
90-
if (bytes > 1024.0) {
91-
bytes /= 1024.0;
92-
units = _("MiB");
93-
}
94-
95-
if (bytes > 1024.0) {
96-
bytes /= 1024.0;
97-
units = _("GiB");
98-
}
99-
100-
return "%.1f %s".printf (bytes, units);
101-
}
102-
103-
}
104-
105-
10647
public class Monitor.Utils.Colors : Object {
10748

10849
public const string STRAWBERRY_100 = "#ff8c82";

src/Views/ProcessView/ProcessInfoView/ProcessInfoIOStats.vala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ public class Monitor.ProcessInfoIOStats : Gtk.Grid {
6161
}
6262

6363
public void update (Process process) {
64-
write_bytes_label.set_text (Utils.HumanUnitFormatter.double_bytes_to_human (process.io.write_bytes));
65-
read_bytes_label.set_text (Utils.HumanUnitFormatter.double_bytes_to_human (process.io.read_bytes));
66-
cancelled_write_bytes_label.set_text (Utils.HumanUnitFormatter.double_bytes_to_human (process.io.cancelled_write_bytes));
64+
write_bytes_label.set_text (format_size ((uint64) process.io.write_bytes, IEC_UNITS));
65+
read_bytes_label.set_text (format_size ((uint64) process.io.read_bytes, IEC_UNITS));
66+
cancelled_write_bytes_label.set_text (format_size ((uint64) process.io.cancelled_write_bytes, IEC_UNITS));
6767
}
6868

6969
private Gtk.Label create_label (string text) {

src/Views/SystemView/SystemMemoryView.vala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ public class Monitor.SystemMemoryView : Monitor.WidgetResource {
6262
// memory_chart.update (3, memory.shared_percentage + memory.buffer_percentage + memory.cached_percentage);
6363
// memory_chart.update (3, memory.shared_percentage + memory.buffer_percentage + memory.cached_percentage + memory.locked_percentage);
6464

65-
memory_total_label.set_text (("%s").printf (Utils.HumanUnitFormatter.double_bytes_to_human (memory.total)));
66-
memory_used_label.set_text (("%s").printf (Utils.HumanUnitFormatter.double_bytes_to_human (memory.used)));
67-
memory_buffered_label.set_text (("%s").printf (Utils.HumanUnitFormatter.double_bytes_to_human (memory.buffer)));
68-
memory_cached_label.set_text (("%s").printf (Utils.HumanUnitFormatter.double_bytes_to_human (memory.cached)));
69-
memory_locked_label.set_text (("%s").printf (Utils.HumanUnitFormatter.double_bytes_to_human (memory.locked)));
65+
memory_total_label.set_text (format_size ((uint64) memory.total, IEC_UNITS));
66+
memory_used_label.set_text (format_size ((uint64) memory.used, IEC_UNITS));
67+
memory_buffered_label.set_text (format_size ((uint64) memory.buffer, IEC_UNITS));
68+
memory_cached_label.set_text (format_size ((uint64) memory.cached, IEC_UNITS));
69+
memory_locked_label.set_text (format_size ((uint64) memory.locked, IEC_UNITS));
7070

71-
memory_shared_label.set_text (("%s").printf (Utils.HumanUnitFormatter.double_bytes_to_human (memory.shared)));
71+
memory_shared_label.set_text (format_size ((uint64) memory.shared, IEC_UNITS));
7272
}
7373

7474
}

src/Views/SystemView/SystemNetworkView.vala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public class Monitor.SystemNetworkView : Gtk.Grid {
5252
double up_bytes = network.bytes_out;
5353
double down_bytes = network.bytes_in;
5454
if (up_bytes >= 0 && down_bytes >= 0) {
55-
network_download_label.set_text (("%s/s").printf (Utils.HumanUnitFormatter.string_bytes_to_human (down_bytes.to_string ())));
56-
network_upload_label.set_text (("%s/s").printf (Utils.HumanUnitFormatter.string_bytes_to_human (up_bytes.to_string ())));
55+
network_download_label.set_text (("%s/s").printf (format_size ((uint64) down_bytes, IEC_UNITS)));
56+
network_upload_label.set_text (("%s/s").printf (format_size ((uint64) up_bytes, IEC_UNITS)));
5757
network_chart.update (0, up_bytes);
5858
network_chart.update (1, down_bytes);
5959
}

src/Views/SystemView/SystemStorageView.vala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ public class Monitor.SystemStorageView : Gtk.Grid {
8080
drive_name_label.margin_bottom = 0;
8181
drive_name_label.halign = Gtk.Align.START;
8282

83-
string size_string = Utils.HumanUnitFormatter.double_bytes_to_human (size);
84-
string used_string = Utils.HumanUnitFormatter.double_bytes_to_human ((size - free));
83+
string size_string = format_size ((uint64) size, IEC_UNITS);
84+
string used_string = format_size ((uint64) (size - free), IEC_UNITS);
8585

8686
string drive_block_name_and_size_string = "%s 𐄁 %s / %s".printf (device, used_string, size_string);
8787

@@ -123,8 +123,8 @@ public class Monitor.SystemStorageView : Gtk.Grid {
123123
double up_bytes = storage.bytes_read;
124124
double down_bytes = storage.bytes_write;
125125
if (up_bytes >= 0 && down_bytes >= 0) {
126-
storage_write_label.set_text (("%s/s").printf (Utils.HumanUnitFormatter.string_bytes_to_human (down_bytes.to_string ())));
127-
storage_read_label.set_text (("%s/s").printf (Utils.HumanUnitFormatter.string_bytes_to_human (up_bytes.to_string ())));
126+
storage_write_label.set_text (("%s/s").printf (format_size ((uint64) down_bytes, IEC_UNITS)));
127+
storage_read_label.set_text (("%s/s").printf (format_size ((uint64) up_bytes, IEC_UNITS)));
128128
storage_chart.update (0, up_bytes);
129129
storage_chart.update (1, down_bytes);
130130
}

src/Widgets/Statusbar/Statusbar.vala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ public class Monitor.Statusbar : Gtk.ActionBar {
8787
string cpu_tooltip_text = ("%.2f %s").printf (sysres.cpu_frequency, _("GHz"));
8888
cpu_usage_label.tooltip_text = cpu_tooltip_text;
8989

90-
string memory_tooltip_text = ("%s / %s").printf (Utils.HumanUnitFormatter.double_bytes_to_human (sysres.memory_used), Utils.HumanUnitFormatter.double_bytes_to_human (sysres.memory_total));
90+
string memory_tooltip_text = ("%s / %s").printf (
91+
format_size ((uint64) sysres.memory_used, IEC_UNITS),
92+
format_size ((uint64) sysres.memory_total, IEC_UNITS)
93+
);
9194
memory_usage_label.tooltip_text = memory_tooltip_text;
9295

9396
// The total amount of the swap is 0 when it is unavailable

0 commit comments

Comments
 (0)