Skip to content

Commit

Permalink
Merge pull request #6 from MoAlyousef/main
Browse files Browse the repository at this point in the history
0.1.8
  • Loading branch information
MoAlyousef committed May 13, 2022
2 parents 712cd4f + 69458a1 commit 9c7cf62
Show file tree
Hide file tree
Showing 14 changed files with 1,140 additions and 206 deletions.
951 changes: 948 additions & 3 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sysinfo-gui"
version = "0.1.7"
version = "0.1.8"
edition = "2021"
repository = "https://github.com/MoAlyousef/sysinfo-gui"
keywords = ["cross-platform", "monitoring", "gui"]
Expand All @@ -15,6 +15,7 @@ sysinfo = { version = "0.23.10", default-features = false }
fltk = "1.3"
fltk-grid = "0.1"
parking_lot = "0.12"
dark-light = "0.2.2"

[profile.release]
opt-level = 3
Expand Down
34 changes: 19 additions & 15 deletions src/gui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ pub struct App {
a: app::App,
r: app::Receiver<Message>,
scroll: group::Scroll,
view: Box<dyn View>,
}

impl App {
pub fn new(view: impl View + 'static) -> Self {
pub fn new() -> Self {
std::panic::set_hook(Box::new(|info| {
if let Some(s) = info.payload().downcast_ref::<&str>() {
// we shamefully use those to end spawned threads
Expand Down Expand Up @@ -102,9 +101,6 @@ impl App {
s.parent().unwrap().redraw();
}
});

view.view(Message::General);

scroll.end();
win.end();
win.show();
Expand All @@ -113,24 +109,32 @@ impl App {
w.hide();
}
});
Self {
a,
r,
scroll,
view: Box::new(view),
}
Self { a, r, scroll }
}
pub fn run(mut self) {
pub fn run(mut self, view: impl View + 'static) {
self.scroll.begin();
let cb = view.view(Message::General);
Self::dispatch(cb, view.sleep_duration());
self.scroll.end();
while self.a.wait() {
if let Some(msg) = self.r.recv() {
self.scroll.clear();
self.scroll.begin();

self.view.view(msg);

let cb = view.view(msg);
Self::dispatch(cb, view.sleep_duration());
self.scroll.end();
app::redraw();
}
}
}
fn dispatch(cb: Option<Box<dyn FnMut() + Send>>, sleep: u64) {
if let Some(mut cb) = cb {
std::thread::spawn({
move || loop {
cb();
std::thread::sleep(std::time::Duration::from_millis(sleep));
}
});
}
}
}
6 changes: 3 additions & 3 deletions src/gui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ pub mod message;
pub mod styles;
pub mod widgets;

use fltk::group::Pack;

pub trait View {
fn view(&self, msg: message::Message) -> Pack;
fn view(&self, msg: message::Message) -> Option<Box<dyn FnMut() + Send>>;
fn sleep_duration(&self) -> u64;
fn light_mode(&self) -> bool;
}
4 changes: 2 additions & 2 deletions src/gui/styles/svgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub const NET: &str = r#"<svg xmlns="http://www.w3.org/2000/svg" width="16" heig
<path d="M7.646 11.854a.5.5 0 0 0 .708 0l3-3a.5.5 0 0 0-.708-.708L8.5 10.293V1.5a.5.5 0 0 0-1 0v8.793L5.354 8.146a.5.5 0 1 0-.708.708l3 3z"/>
</svg>"#;

pub const DESC: &str = r#"<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Capa_1" x="0px" y="0px" viewBox="0 0 301.219 301.219" style="enable-background:new 0 0 301.219 301.219;" xml:space="preserve">
pub const DESC: &str = r#"<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Capa_1" x="0px" y="0px" viewBox="0 0 301.219 301.219" style="enable-background:new 0 0 301.219 301.219;" xml:space="preserve" fill='blueviolet'>
<g>
<path d="M149.365,262.736H10c-5.523,0-10,4.477-10,10v10c0,5.523,4.477,10,10,10h139.365c5.522,0,10-4.477,10-10v-10 C159.365,267.213,154.888,262.736,149.365,262.736z"/>
<path d="M10,229.736h120.586c5.522,0,10-4.477,10-10v-10c0-5.523-4.478-10-10-10H10c-5.523,0-10,4.477-10,10v10 C0,225.259,4.477,229.736,10,229.736z"/>
Expand All @@ -41,7 +41,7 @@ pub const DESC: &str = r#"<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="h
<path d="M298.29,216.877l-7.07-7.071c-1.875-1.875-4.419-2.929-7.071-2.929c-2.652,0-5.195,1.054-7.071,2.929l-34.394,34.393 V18.736c0-5.523-4.477-10-10-10h-10c-5.522,0-10,4.477-10,10v225.462l-34.394-34.393c-1.876-1.876-4.419-2.929-7.071-2.929 c-2.652,0-5.196,1.054-7.071,2.929l-7.07,7.071c-3.905,3.905-3.905,10.237,0,14.142l63.535,63.536 c1.876,1.875,4.419,2.929,7.071,2.929c2.652,0,5.195-1.054,7.071-2.929l63.535-63.536 C302.195,227.113,302.195,220.782,298.29,216.877z"/>
</g></svg>"#;

pub const ASC: &str = r#"<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Capa_1" x="0px" y="0px" viewBox="0 0 301.219 301.219" style="enable-background:new 0 0 301.219 301.219;" xml:space="preserve">
pub const ASC: &str = r#"<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Capa_1" x="0px" y="0px" viewBox="0 0 301.219 301.219" style="enable-background:new 0 0 301.219 301.219;" xml:space="preserve" fill='blueviolet'>
<g>
<path d="M159.365,23.736v-10c0-5.523-4.477-10-10-10H10c-5.523,0-10,4.477-10,10v10c0,5.523,4.477,10,10,10h139.365 C154.888,33.736,159.365,29.259,159.365,23.736z"/>
<path d="M130.586,66.736H10c-5.523,0-10,4.477-10,10v10c0,5.523,4.477,10,10,10h120.586c5.523,0,10-4.477,10-10v-10 C140.586,71.213,136.109,66.736,130.586,66.736z"/>
Expand Down
3 changes: 1 addition & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ mod gui;
mod view;

fn main() {
let a = gui::app::App::new(view::MyView::default());
a.run();
gui::app::App::new().run(view::MyView::default());
}
67 changes: 28 additions & 39 deletions src/view/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::gui::widgets::Card;
use fltk::{enums::*, prelude::*, *};
use parking_lot::Mutex;
use std::collections::VecDeque;
use std::sync::{atomic::Ordering, Arc};
use std::sync::Arc;
use sysinfo::ProcessorExt;
use sysinfo::System;
use sysinfo::SystemExt;
Expand Down Expand Up @@ -66,7 +66,7 @@ mod cpu_color {
}
}

pub fn proc(view: &MyView) -> group::Pack {
pub fn proc(view: &MyView) -> Option<Box<dyn FnMut() + Send>> {
let mut sys = view.system.lock();
sys.refresh_cpu();
let first = sys.processors().first().unwrap();
Expand Down Expand Up @@ -131,45 +131,34 @@ pub fn proc(view: &MyView) -> group::Pack {
grp.end();
let charts = Arc::new(Mutex::new(charts));
let sys = Arc::new(Mutex::new(System::new_all()));

let sleep = view.sleep.clone();
std::thread::spawn({
let charts = charts;
move || {
let mut v = vec![];
for _ in 0..num_cpus {
let mut d = VecDeque::new();
for _ in 0..20 {
d.push_back(0.);
}
v.push(d);
let mut v = vec![];
for _ in 0..num_cpus {
let mut d = VecDeque::new();
for _ in 0..20 {
d.push_back(0.);
}
v.push(d);
}
let cb = move || {
if let Some(mut sys) = sys.try_lock() {
sys.refresh_cpu();
for (i, proc) in sys.processors().iter().enumerate() {
v[i].push_back(proc.cpu_usage() as f64);
v[i].pop_front();
}

loop {
if let Some(mut sys) = sys.try_lock() {
sys.refresh_cpu();
for (i, proc) in sys.processors().iter().enumerate() {
v[i].push_back(proc.cpu_usage() as f64);
v[i].pop_front();
}
for (count, c) in (*charts.lock()).iter_mut().enumerate() {
for i in 1..20 {
let last = if let Some(val) = v[count].get(i) {
*val
} else {
0.
};
c.replace((i - 1) as i32, last, "", cpu_color::by_index(count as u8));
}
}
app::awake();
app::redraw();
for (count, c) in (*charts.lock()).iter_mut().enumerate() {
for i in 1..20 {
let last = if let Some(val) = v[count].get(i) {
*val
} else {
0.
};
c.replace((i - 1) as i32, last, "", cpu_color::by_index(count as u8));
}
std::thread::sleep(std::time::Duration::from_millis(
sleep.load(Ordering::Relaxed),
));
}
app::awake();
app::redraw();
}
});
grp
};
Some(Box::new(cb))
}
4 changes: 2 additions & 2 deletions src/view/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use fltk::{prelude::*, *};
use sysinfo::DiskExt;
use sysinfo::SystemExt;

pub fn disks(view: &MyView) -> group::Pack {
pub fn disks(view: &MyView) -> Option<Box<dyn FnMut() + Send>> {
let mut sys = view.system.lock();
sys.refresh_disks();
frame::Frame::new(60, 60, 0, 0, None);
Expand Down Expand Up @@ -49,5 +49,5 @@ pub fn disks(view: &MyView) -> group::Pack {
hpack.end();
}
grp.end();
grp
None
}
73 changes: 33 additions & 40 deletions src/view/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use crate::{
};
use fltk::{enums::*, prelude::*, *};
use parking_lot::Mutex;
use std::sync::{atomic::Ordering, Arc};
use std::sync::Arc;
use sysinfo::{DiskExt, NetworkExt, NetworksExt, ProcessExt, System, SystemExt};

pub fn general(view: &MyView) -> group::Pack {
pub fn general(view: &MyView) -> Option<Box<dyn FnMut() + Send>> {
let mut sys = view.system.lock();
sys.refresh_all();
let mem = (sys.used_memory() as f64 / sys.total_memory() as f64) * 100.;
Expand Down Expand Up @@ -123,45 +123,38 @@ pub fn general(view: &MyView) -> group::Pack {
drop(sys);
let dials = Arc::new(Mutex::new(dials));
let sys = Arc::new(Mutex::new(System::new_all()));

let sleep = view.sleep.clone();
std::thread::spawn({
move || loop {
if let Some(mut sys) = sys.try_lock() {
sys.refresh_all();
let mem = (sys.used_memory() as f64 / sys.total_memory() as f64) * 100.;
let mut total_space = 0;
let mut avail_space = 0;
for disk in sys.disks() {
total_space += disk.total_space();
avail_space += disk.available_space();
}
let used_space =
((total_space - avail_space) as f64 * 100. / total_space as f64) as i32;
let mut cpu_usage = 0.;
for process in sys.processes().values() {
cpu_usage += process.cpu_usage();
}
let mut total_recv = 0;
let mut total_transm = 0;
for comp in sys.networks().iter() {
total_recv += comp.1.total_received();
total_transm += comp.1.total_transmitted();
}
dials.lock()[0].set_value(cpu_usage as i32);
dials.lock()[1].set_value(mem as i32);
dials.lock()[2].set_value(used_space);
download.set_label(&format!("{:.02} MiB", total_recv as f64 / 2_f64.powf(20.)));
upload.set_label(&format!(
"{:.02} Mib",
total_transm as f64 / 2_f64.powf(20.)
));
app::awake();
let cb = move || {
if let Some(mut sys) = sys.try_lock() {
sys.refresh_all();
let mem = (sys.used_memory() as f64 / sys.total_memory() as f64) * 100.;
let mut total_space = 0;
let mut avail_space = 0;
for disk in sys.disks() {
total_space += disk.total_space();
avail_space += disk.available_space();
}
let used_space =
((total_space - avail_space) as f64 * 100. / total_space as f64) as i32;
let mut cpu_usage = 0.;
for process in sys.processes().values() {
cpu_usage += process.cpu_usage();
}
let mut total_recv = 0;
let mut total_transm = 0;
for comp in sys.networks().iter() {
total_recv += comp.1.total_received();
total_transm += comp.1.total_transmitted();
}
std::thread::sleep(std::time::Duration::from_millis(
sleep.load(Ordering::Relaxed),
dials.lock()[0].set_value(cpu_usage as i32);
dials.lock()[1].set_value(mem as i32);
dials.lock()[2].set_value(used_space);
download.set_label(&format!("{:.02} MiB", total_recv as f64 / 2_f64.powf(20.)));
upload.set_label(&format!(
"{:.02} Mib",
total_transm as f64 / 2_f64.powf(20.)
));
app::awake();
}
});
grp
};
Some(Box::new(cb))
}
45 changes: 19 additions & 26 deletions src/view/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use crate::{
};
use fltk::{prelude::*, *};
use parking_lot::Mutex;
use std::sync::{atomic::Ordering, Arc};
use std::sync::Arc;
use sysinfo::System;
use sysinfo::SystemExt;

pub fn memory(view: &MyView) -> group::Pack {
pub fn memory(view: &MyView) -> Option<Box<dyn FnMut() + Send>> {
let mut sys = view.system.lock();
sys.refresh_memory();
frame::Frame::new(60, 60, 0, 0, None);
Expand Down Expand Up @@ -87,30 +87,23 @@ pub fn memory(view: &MyView) -> group::Pack {
grp.end();
let dials = Arc::new(Mutex::new(dials));
let sys = Arc::new(Mutex::new(System::new_all()));
let sleep = view.sleep.clone();
std::thread::spawn({
move || loop {
if let Some(mut sys) = sys.try_lock() {
sys.refresh_memory();
dials.lock()[0].set_value(
(sys.used_memory() as f64 / sys.total_memory() as f64 * 100.) as i32,
);
used_mem.set_label(&format!(
"Used: {:.02} GiB",
sys.used_memory() as f64 / 2_f64.powf(20.)
));
dials.lock()[1]
.set_value((sys.used_swap() as f64 / sys.total_swap() as f64 * 100.) as i32);
used_swap.set_label(&format!(
"Used: {:.02} GiB",
sys.used_swap() as f64 / 2_f64.powf(20.)
));
app::awake();
}
std::thread::sleep(std::time::Duration::from_millis(
sleep.load(Ordering::Relaxed),
let cb = move || {
if let Some(mut sys) = sys.try_lock() {
sys.refresh_memory();
dials.lock()[0]
.set_value((sys.used_memory() as f64 / sys.total_memory() as f64 * 100.) as i32);
used_mem.set_label(&format!(
"Used: {:.02} GiB",
sys.used_memory() as f64 / 2_f64.powf(20.)
));
dials.lock()[1]
.set_value((sys.used_swap() as f64 / sys.total_swap() as f64 * 100.) as i32);
used_swap.set_label(&format!(
"Used: {:.02} GiB",
sys.used_swap() as f64 / 2_f64.powf(20.)
));
app::awake();
}
});
grp
};
Some(Box::new(cb))
}
Loading

0 comments on commit 9c7cf62

Please sign in to comment.