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::{
|
use iced::{
|
||||||
executor, system, Application, Column, Command, Container, Element, Length,
|
button, executor, system, Application, Button, Column, Command, Container,
|
||||||
Settings, Text,
|
Element, Length, Settings, Text,
|
||||||
};
|
};
|
||||||
|
|
||||||
use bytesize::ByteSize;
|
use bytesize::ByteSize;
|
||||||
|
|
@ -11,13 +11,17 @@ pub fn main() -> iced::Result {
|
||||||
|
|
||||||
enum Example {
|
enum Example {
|
||||||
Loading,
|
Loading,
|
||||||
Loaded { information: system::Information },
|
Loaded {
|
||||||
|
information: system::Information,
|
||||||
|
refresh_button: button::State,
|
||||||
|
},
|
||||||
Unsupported,
|
Unsupported,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Clone, Debug)]
|
||||||
enum Message {
|
enum Message {
|
||||||
InformationReceived(Option<system::Information>),
|
InformationReceived(Option<system::Information>),
|
||||||
|
Refresh,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Application for Example {
|
impl Application for Example {
|
||||||
|
|
@ -38,9 +42,16 @@ impl Application for Example {
|
||||||
|
|
||||||
fn update(&mut self, message: Message) -> Command<Message> {
|
fn update(&mut self, message: Message) -> Command<Message> {
|
||||||
match message {
|
match message {
|
||||||
|
Message::Refresh => {
|
||||||
|
return system::information(Message::InformationReceived);
|
||||||
|
}
|
||||||
Message::InformationReceived(information) => {
|
Message::InformationReceived(information) => {
|
||||||
if let Some(information) = information {
|
if let Some(information) = information {
|
||||||
*self = Self::Loaded { information };
|
let refresh_button = button::State::new();
|
||||||
|
*self = Self::Loaded {
|
||||||
|
information,
|
||||||
|
refresh_button,
|
||||||
|
};
|
||||||
} else {
|
} else {
|
||||||
*self = Self::Unsupported;
|
*self = Self::Unsupported;
|
||||||
}
|
}
|
||||||
|
|
@ -53,7 +64,10 @@ impl Application for Example {
|
||||||
fn view(&mut self) -> Element<Message> {
|
fn view(&mut self) -> Element<Message> {
|
||||||
let content: Element<Message> = match self {
|
let content: Element<Message> = match self {
|
||||||
Example::Loading => Text::new("Loading...").size(40).into(),
|
Example::Loading => Text::new("Loading...").size(40).into(),
|
||||||
Example::Loaded { information } => {
|
Example::Loaded {
|
||||||
|
information,
|
||||||
|
refresh_button,
|
||||||
|
} => {
|
||||||
let system_name = Text::new(format!(
|
let system_name = Text::new(format!(
|
||||||
"System name: {}",
|
"System name: {}",
|
||||||
information
|
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!(
|
let graphics_adapter = Text::new(format!(
|
||||||
"Graphics adapter: {}",
|
"Graphics adapter: {}",
|
||||||
information.graphics_adapter
|
information.graphics_adapter
|
||||||
|
|
@ -119,8 +146,12 @@ impl Application for Example {
|
||||||
cpu_brand.size(30).into(),
|
cpu_brand.size(30).into(),
|
||||||
cpu_cores.size(30).into(),
|
cpu_cores.size(30).into(),
|
||||||
memory_total.size(30).into(),
|
memory_total.size(30).into(),
|
||||||
|
memory_used.size(30).into(),
|
||||||
graphics_adapter.size(30).into(),
|
graphics_adapter.size(30).into(),
|
||||||
graphics_backend.size(30).into(),
|
graphics_backend.size(30).into(),
|
||||||
|
Button::new(refresh_button, Text::new("Refresh"))
|
||||||
|
.on_press(Message::Refresh)
|
||||||
|
.into(),
|
||||||
])
|
])
|
||||||
.spacing(10)
|
.spacing(10)
|
||||||
.into()
|
.into()
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/// Contains informations about the system (e.g. system name, processor, memory, graphics adapter).
|
/// Contains informations about the system (e.g. system name, processor, memory, graphics adapter).
|
||||||
#[derive(Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Information {
|
pub struct Information {
|
||||||
/// Contains the system name.
|
/// Contains the system name.
|
||||||
pub system_name: Option<String>,
|
pub system_name: Option<String>,
|
||||||
|
|
@ -13,6 +13,8 @@ pub struct Information {
|
||||||
pub cpu_cores: Option<usize>,
|
pub cpu_cores: Option<usize>,
|
||||||
/// Contains the total RAM size in KB.
|
/// Contains the total RAM size in KB.
|
||||||
pub memory_total: u64,
|
pub memory_total: u64,
|
||||||
|
/// Contains the system used RAM size in KB.
|
||||||
|
pub memory_used: Option<u64>,
|
||||||
/// Contains the graphics backend.
|
/// Contains the graphics backend.
|
||||||
pub graphics_backend: String,
|
pub graphics_backend: String,
|
||||||
/// Contains the graphics adapter.
|
/// Contains the graphics adapter.
|
||||||
|
|
|
||||||
|
|
@ -584,12 +584,21 @@ pub fn run_command<Message: 'static + std::fmt::Debug + Send, E: Executor>(
|
||||||
system::Action::QueryInformation(tag) => {
|
system::Action::QueryInformation(tag) => {
|
||||||
#[cfg(feature = "sysinfo")]
|
#[cfg(feature = "sysinfo")]
|
||||||
let information = {
|
let information = {
|
||||||
use sysinfo::{ProcessorExt, System, SystemExt};
|
use sysinfo::{
|
||||||
|
ProcessExt, ProcessorExt, System, SystemExt,
|
||||||
|
};
|
||||||
let mut system = System::new_all();
|
let mut system = System::new_all();
|
||||||
system.refresh_all();
|
system.refresh_all();
|
||||||
|
|
||||||
let cpu = system.global_processor_info();
|
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 {
|
let information = system::Information {
|
||||||
system_name: system.name(),
|
system_name: system.name(),
|
||||||
system_kernel: system.kernel_version(),
|
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_brand: cpu.brand().into(),
|
||||||
cpu_cores: system.physical_core_count(),
|
cpu_cores: system.physical_core_count(),
|
||||||
memory_total: system.total_memory(),
|
memory_total: system.total_memory(),
|
||||||
|
memory_used,
|
||||||
graphics_adapter: graphics_info.adapter.clone(),
|
graphics_adapter: graphics_info.adapter.clone(),
|
||||||
graphics_backend: graphics_info.backend.clone(),
|
graphics_backend: graphics_info.backend.clone(),
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue