Skip to content

Commit

Permalink
Adding Disk module
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieurousseau committed Jun 21, 2020
1 parent edcfea6 commit 3cf5b9e
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*~
build/
uncrustify.cfg
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,11 @@ sudo ninja install
com.github.plugarut.wingpanel-monitor
```

To force reload, use `wingpanel -c`

```
wingpanel -c wingpanel-monitor
```

## Special Thanks
- [Nararyans R.I.](https://github.com/Fatih20) for the icon
7 changes: 6 additions & 1 deletion data/com.github.plugarut.wingpanel-monitor.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<summary>Display indicator in Wingpanel</summary>
<description></description>
</key>
<key type="b" name="icon-only">
<key type="b" name="icon-only">
<default>false</default>
<summary>Show only indicator icon in wingpanel</summary>
<description></description>
Expand All @@ -25,6 +25,11 @@
<summary>Show Network usage in Wingpanel</summary>
<description></description>
</key>
<key type="b" name="show-disk">
<default>false</default>
<summary>Show Disk usage in Wingpanel</summary>
<description></description>
</key>
<key type="b" name="show-graph">
<default>false</default>
<summary>Show graph for CPU/RAM in Wingpanel</summary>
Expand Down
2 changes: 2 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ shared_module(
'src/Widgets/IndicatorWidget.vala',
'src/Widgets/PopoverWidgetRow.vala',
'src/Widgets/NetworkWidget.vala',
'src/Widgets/DiskWidget.vala',
'src/Services/Cpu.vala',
'src/Services/Memory.vala',
'src/Services/Network.vala',
'src/Services/Disk.vala',
'src/Services/Utils.vala',
'src/Services/System.vala',
icons_gresource,
Expand Down
6 changes: 6 additions & 0 deletions src/Indicator.vala
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace WingpanelMonitor {
private CPU cpu_data;
private Memory memory_data;
private Network network_data;
private Disk disk_data;
private System system_data;
private Gdk.X11.Screen screen;

Expand All @@ -55,6 +56,7 @@ namespace WingpanelMonitor {
cpu_data = new CPU ();
memory_data = new Memory ();
network_data = new Network ();
disk_data = new Disk ();
system_data = new System ();
screen = Gdk.Screen.get_default () as Gdk.X11.Screen;

Expand Down Expand Up @@ -96,6 +98,8 @@ namespace WingpanelMonitor {
display_widget.update_memory (memory_data.percentage_used);
var net = network_data.get_bytes ();
display_widget.update_network (net[0], net[1]);
var disk = disk_data.get_bytes ();
display_widget.update_disk (disk[0], disk[1]);
display_widget.update_weather ();
update_popover_widget_data ();
return true;
Expand All @@ -111,6 +115,8 @@ namespace WingpanelMonitor {
popover_widget.update_swap (memory_data.used_swap, memory_data.total_swap);
var net = network_data.get_bytes ();
popover_widget.update_network (net[0], net[1]);
var disk = disk_data.get_bytes ();
popover_widget.update_disk (disk[0], disk[1]);
}

private void enable_weather_update () {
Expand Down
81 changes: 81 additions & 0 deletions src/Services/Disk.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*-
* Copyright (c) 2020 Tudor Plugaru (https://github.com/PlugaruT/wingpanel-monitor)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
* Authored by: Tudor Plugaru <plugaru.tudor@gmail.com>
*/
namespace WingpanelMonitor {
public class Disk : GLib.Object {
private int _bytes_write;
private ulong _bytes_write_old;
private bool control;

private int _bytes_read;
private ulong _bytes_read_old;

public Disk () {
_bytes_write = 0;
_bytes_write_old = 0;
_bytes_read = 0;
_bytes_read_old = 0;
control = false;
}

public int[] get_bytes () {
if (control == false) {
control = true;
update_bytes_total ();
} else {
control = false;
}
int[] ret;
ret = {_bytes_read, _bytes_write};
return ret;
}

private void update_bytes_total () {

ulong n_bytes_read = 0;
ulong n_bytes_write = 0;

try {
string content = null;
FileUtils.get_contents (@"/proc/diskstats", out content);
InputStream input_stream = new MemoryInputStream.from_data (content.data, GLib.g_free);
DataInputStream dis = new DataInputStream (input_stream);
string line;

while ((line = dis.read_line ()) != null) {
string[] reg_split = Regex.split_simple("[ ]+", line);
if(reg_split[1] == "8" && Regex.match_simple("sd[a-z]{1}$", reg_split[3])) {
n_bytes_read += ulong.parse(reg_split[6]);
n_bytes_write += ulong.parse(reg_split[10]);
}

}
} catch (Error e) {
warning("Could not retrieve disk data");
}

_bytes_read = (int)((n_bytes_read - _bytes_read_old) * 512);
_bytes_write = (int)((n_bytes_write - _bytes_write_old) * 512);
_bytes_read_old = n_bytes_read;
_bytes_write_old = n_bytes_write;
}

}
}
68 changes: 68 additions & 0 deletions src/Widgets/DiskWidget.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*-
* Copyright (c) 2020 Tudor Plugaru (https://github.com/PlugaruT/wingpanel-monitor)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
* Authored by: Tudor Plugaru <plugaru.tudor@gmail.com>
*/

namespace WingpanelMonitor {
public class DiskWidget : Gtk.Grid {
private Gtk.Revealer widget_revealer;
private Gtk.Label read_label;
private Gtk.Label write_label;

public bool display {
set { widget_revealer.reveal_child = value; }
get { return widget_revealer.get_reveal_child () ; }
}

construct {
orientation = Gtk.Orientation.HORIZONTAL;

read_label = new Gtk.Label ("N/A");
read_label.set_width_chars (8);
read_label.halign = Gtk.Align.START;
var read_label_context = read_label.get_style_context ();
read_label_context.add_class ("small-label");
read_label_context.add_class ("read");

write_label = new Gtk.Label ("N/A");
write_label.set_width_chars (8);
write_label.halign = Gtk.Align.END;
var write_label_context = write_label.get_style_context ();
write_label_context.add_class ("small-label");
write_label_context.add_class ("write");

var group = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0);
group.add (read_label);
group.add (write_label);

widget_revealer = new Gtk.Revealer ();
widget_revealer.transition_type = Gtk.RevealerTransitionType.SLIDE_RIGHT;
widget_revealer.reveal_child = true;

widget_revealer.add (group);

add (widget_revealer);
}

public void update_label_data (string read_speed, string write_speed) {
read_label.label = "" + read_speed;
write_label.label = "" + write_speed;
}
}
}
10 changes: 10 additions & 0 deletions src/Widgets/DisplayWidget.vala
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace WingpanelMonitor {
private IndicatorWidget weather_info;
private IndicatorWidget icon_only;
private NetworkWidget network_info;
private DiskWidget disk_info;

public unowned Settings settings { get; construct set; }

Expand All @@ -52,7 +53,9 @@ namespace WingpanelMonitor {

network_info = new NetworkWidget ();

disk_info = new DiskWidget ();

settings.bind ("show-disk", disk_info, "display", SettingsBindFlags.GET);
settings.bind ("show-cpu", cpu_info, "display", SettingsBindFlags.GET);
settings.bind ("show-ram", ram_info, "display", SettingsBindFlags.GET);
settings.bind ("show-network", network_info, "display", SettingsBindFlags.GET);
Expand All @@ -66,6 +69,7 @@ namespace WingpanelMonitor {
add (cpu_info);
add (ram_info);
add (workspace_info);
add (disk_info);
}

public void update_workspace (int val) {
Expand All @@ -86,6 +90,12 @@ namespace WingpanelMonitor {
network_info.update_label_data (up, down);
}

public void update_disk (int read, int write) {
string read_s = WingpanelMonitor.Utils.format_net_speed (read, true, false);
string write_s = WingpanelMonitor.Utils.format_net_speed (write, true, false);
disk_info.update_label_data (read_s, write_s);
}

public void update_weather () {
weather_info.label_value = settings.get_string ("weather-temperature");
weather_info.new_icon = settings.get_string ("weather-icon");
Expand Down
13 changes: 5 additions & 8 deletions src/Widgets/NetworkWidget.vala
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,22 @@ namespace WingpanelMonitor {
construct {
orientation = Gtk.Orientation.HORIZONTAL;

var icon = new Gtk.Image.from_icon_name ("up-down-symbolic", Gtk.IconSize.SMALL_TOOLBAR);

upload_label = new Gtk.Label ("N/A");
upload_label.set_width_chars (8);
upload_label.halign = Gtk.Align.START;
var upload_label_context = upload_label.get_style_context ();
upload_label_context.add_class ("small-label");
upload_label_context.add_class ("upload");
upload_label_context.add_class ("123");

download_label = new Gtk.Label ("N/A");
download_label.set_width_chars (8);
download_label.halign = Gtk.Align.END;
var down_label_context = download_label.get_style_context ();
down_label_context.add_class ("small-label");
down_label_context.add_class ("download");
down_label_context.add_class ("123");

var group = new Gtk.Grid ();
var group = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0);
group.add (upload_label);
group.add (icon);
group.add (download_label);

widget_revealer = new Gtk.Revealer ();
Expand All @@ -64,8 +61,8 @@ namespace WingpanelMonitor {
}

public void update_label_data (string up_speed, string down_speed) {
upload_label.label = up_speed;
download_label.label = down_speed;
upload_label.label = "" + up_speed;
download_label.label = "" + down_speed;
}
}
}
11 changes: 11 additions & 0 deletions src/Widgets/PopoverWidget.vala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ namespace WingpanelMonitor {
private PopoverWidgetRow uptime;
private PopoverWidgetRow network_down;
private PopoverWidgetRow network_up;
private PopoverWidgetRow disk_read;
private PopoverWidgetRow disk_write;
private PopoverWidgetRow ram;
private PopoverWidgetRow swap;

Expand All @@ -43,6 +45,8 @@ namespace WingpanelMonitor {
uptime = new PopoverWidgetRow ("Uptime", "0", 4);
network_down = new PopoverWidgetRow ("Network Down", "0", 4);
network_up = new PopoverWidgetRow ("Network Up", "0", 4);
disk_read = new PopoverWidgetRow ("Disk Read", "0", 4);
disk_write = new PopoverWidgetRow ("Disk Write", "0", 4);
ram = new PopoverWidgetRow ("RAM", "0", 4);
swap = new PopoverWidgetRow ("Swap", "0", 4);

Expand Down Expand Up @@ -71,6 +75,8 @@ namespace WingpanelMonitor {
add (uptime);
add (network_down);
add (network_up);
add (disk_read);
add (disk_write);
add (new Wingpanel.Widgets.Separator ());
add (hide_button);
add (settings_button);
Expand Down Expand Up @@ -112,5 +118,10 @@ namespace WingpanelMonitor {
network_down.label_value = Utils.format_net_speed (upload, true, false);
network_up.label_value = Utils.format_net_speed (download, true, false);
}

public void update_disk (int read, int write) {
disk_read.label_value = Utils.format_net_speed (read, true, false);
disk_write.label_value = Utils.format_net_speed (write, true, false);
}
}
}
4 changes: 4 additions & 0 deletions src/Widgets/Toggles.vala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace WingpanelMonitor {
private Wingpanel.Widgets.Switch cpu_switch;
private Wingpanel.Widgets.Switch ram_switch;
private Wingpanel.Widgets.Switch network_switch;
private Wingpanel.Widgets.Switch disk_switch;
private Wingpanel.Widgets.Switch workspace_switch;
private Wingpanel.Widgets.Switch weather_switch;
private Wingpanel.Widgets.Switch icon_only_switch;
Expand All @@ -42,6 +43,7 @@ namespace WingpanelMonitor {
cpu_switch = new Wingpanel.Widgets.Switch ("CPU usage", settings.get_boolean ("show-cpu"));
ram_switch = new Wingpanel.Widgets.Switch ("RAM usage", settings.get_boolean ("show-ram"));
network_switch = new Wingpanel.Widgets.Switch ("Network usage", settings.get_boolean ("show-network"));
disk_switch = new Wingpanel.Widgets.Switch ("Disk usage", settings.get_boolean ("show-disk"));
workspace_switch = new Wingpanel.Widgets.Switch (
"Workspace number", settings.get_boolean ("show-workspace")
);
Expand All @@ -56,6 +58,7 @@ namespace WingpanelMonitor {
settings.bind ("show-cpu", cpu_switch.get_switch (), "active", SettingsBindFlags.DEFAULT);
settings.bind ("show-ram", ram_switch.get_switch (), "active", SettingsBindFlags.DEFAULT);
settings.bind ("show-network", network_switch.get_switch (), "active", SettingsBindFlags.DEFAULT);
settings.bind ("show-disk", disk_switch.get_switch (), "active", SettingsBindFlags.DEFAULT);
settings.bind ("show-workspace", workspace_switch.get_switch (), "active", SettingsBindFlags.DEFAULT);
settings.bind ("show-weather", weather_switch.get_switch (), "active", SettingsBindFlags.DEFAULT);
settings.bind ("icon-only", icon_only_switch.get_switch (), "active", SettingsBindFlags.DEFAULT);
Expand All @@ -73,6 +76,7 @@ namespace WingpanelMonitor {
add (cpu_switch);
add (ram_switch);
add (network_switch);
add (disk_switch);
add (workspace_switch);
add (weather_switch);
add (weather_refresh_spin);
Expand Down

0 comments on commit 3cf5b9e

Please sign in to comment.