Add memory usage to Information struct
This commit is contained in:
parent
5bfe887e3d
commit
c9ea1f11de
3 changed files with 51 additions and 8 deletions
|
|
@ -1,6 +1,6 @@
|
|||
use iced::{
|
||||
executor, system, Application, Column, Command, Container, Element, Length,
|
||||
Settings, Text,
|
||||
button, executor, system, Application, Button, Column, Command, Container,
|
||||
Element, Length, Settings, Text,
|
||||
};
|
||||
|
||||
use bytesize::ByteSize;
|
||||
|
|
@ -11,13 +11,17 @@ pub fn main() -> iced::Result {
|
|||
|
||||
enum Example {
|
||||
Loading,
|
||||
Loaded { information: system::Information },
|
||||
Loaded {
|
||||
information: system::Information,
|
||||
refresh_button: button::State,
|
||||
},
|
||||
Unsupported,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Clone, Debug)]
|
||||
enum Message {
|
||||
InformationReceived(Option<system::Information>),
|
||||
Refresh,
|
||||
}
|
||||
|
||||
impl Application for Example {
|
||||
|
|
@ -38,9 +42,16 @@ impl Application for Example {
|
|||
|
||||
fn update(&mut self, message: Message) -> Command<Message> {
|
||||
match message {
|
||||
Message::Refresh => {
|
||||
return system::information(Message::InformationReceived);
|
||||
}
|
||||
Message::InformationReceived(information) => {
|
||||
if let Some(information) = information {
|
||||
*self = Self::Loaded { information };
|
||||
let refresh_button = button::State::new();
|
||||
*self = Self::Loaded {
|
||||
information,
|
||||
refresh_button,
|
||||
};
|
||||
} else {
|
||||
*self = Self::Unsupported;
|
||||
}
|
||||
|
|
@ -53,7 +64,10 @@ impl Application for Example {
|
|||
fn view(&mut self) -> Element<Message> {
|
||||
let content: Element<Message> = match self {
|
||||
Example::Loading => Text::new("Loading...").size(40).into(),
|
||||
Example::Loaded { information } => {
|
||||
Example::Loaded {
|
||||
information,
|
||||
refresh_button,
|
||||
} => {
|
||||
let system_name = Text::new(format!(
|
||||
"System name: {}",
|
||||
information
|
||||
|
|
@ -102,6 +116,19 @@ impl Application for Example {
|
|||
)
|
||||
));
|
||||
|
||||
let memory_text = if let Some(memory_used) =
|
||||
information.memory_used
|
||||
{
|
||||
let memory_readable = ByteSize::kb(memory_used).to_string();
|
||||
|
||||
format!("{} kb ({})", memory_used, memory_readable)
|
||||
} else {
|
||||
String::from("None")
|
||||
};
|
||||
|
||||
let memory_used =
|
||||
Text::new(format!("Memory (used): {}", memory_text));
|
||||
|
||||
let graphics_adapter = Text::new(format!(
|
||||
"Graphics adapter: {}",
|
||||
information.graphics_adapter
|
||||
|
|
@ -119,8 +146,12 @@ impl Application for Example {
|
|||
cpu_brand.size(30).into(),
|
||||
cpu_cores.size(30).into(),
|
||||
memory_total.size(30).into(),
|
||||
memory_used.size(30).into(),
|
||||
graphics_adapter.size(30).into(),
|
||||
graphics_backend.size(30).into(),
|
||||
Button::new(refresh_button, Text::new("Refresh"))
|
||||
.on_press(Message::Refresh)
|
||||
.into(),
|
||||
])
|
||||
.spacing(10)
|
||||
.into()
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/// Contains informations about the system (e.g. system name, processor, memory, graphics adapter).
|
||||
#[derive(Debug)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Information {
|
||||
/// Contains the system name.
|
||||
pub system_name: Option<String>,
|
||||
|
|
@ -13,6 +13,8 @@ pub struct Information {
|
|||
pub cpu_cores: Option<usize>,
|
||||
/// Contains the total RAM size in KB.
|
||||
pub memory_total: u64,
|
||||
/// Contains the system used RAM size in KB.
|
||||
pub memory_used: Option<u64>,
|
||||
/// Contains the graphics backend.
|
||||
pub graphics_backend: String,
|
||||
/// Contains the graphics adapter.
|
||||
|
|
|
|||
|
|
@ -584,12 +584,21 @@ pub fn run_command<Message: 'static + std::fmt::Debug + Send, E: Executor>(
|
|||
system::Action::QueryInformation(tag) => {
|
||||
#[cfg(feature = "sysinfo")]
|
||||
let information = {
|
||||
use sysinfo::{ProcessorExt, System, SystemExt};
|
||||
use sysinfo::{
|
||||
ProcessExt, ProcessorExt, System, SystemExt,
|
||||
};
|
||||
let mut system = System::new_all();
|
||||
system.refresh_all();
|
||||
|
||||
let cpu = system.global_processor_info();
|
||||
|
||||
let memory_used = sysinfo::get_current_pid()
|
||||
.and_then(|pid| {
|
||||
system.process(pid).ok_or("Process not found")
|
||||
})
|
||||
.and_then(|process| Ok(process.memory()))
|
||||
.ok();
|
||||
|
||||
let information = system::Information {
|
||||
system_name: system.name(),
|
||||
system_kernel: system.kernel_version(),
|
||||
|
|
@ -597,6 +606,7 @@ pub fn run_command<Message: 'static + std::fmt::Debug + Send, E: Executor>(
|
|||
cpu_brand: cpu.brand().into(),
|
||||
cpu_cores: system.physical_core_count(),
|
||||
memory_total: system.total_memory(),
|
||||
memory_used,
|
||||
graphics_adapter: graphics_info.adapter.clone(),
|
||||
graphics_backend: graphics_info.backend.clone(),
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue