@@ -3,33 +3,34 @@ use crate::{
33 colors:: { Color , Style } ,
44 icons:: Icon ,
55} ;
6- use std:: fmt;
6+ use std:: {
7+ fmt,
8+ time:: { Duration , Instant } ,
9+ } ;
710use sysinfo:: { MemoryRefreshKind , RefreshKind , System } ;
811
912pub struct Memory {
1013 usage : f32 ,
1114 minimum_digits : usize ,
1215 decimal_places : usize ,
16+ system : System ,
1317}
1418
1519impl Memory {
1620 pub fn new ( minimum_digits : usize , decimal_places : usize ) -> Self {
21+ let specifics = RefreshKind :: new ( ) . with_memory ( MemoryRefreshKind :: everything ( ) ) ;
22+ let mut system = System :: new_with_specifics ( specifics) ;
23+ system. refresh_memory ( ) ;
24+
25+ let usage = ( system. used_memory ( ) as f32 / system. total_memory ( ) as f32 ) * 100.0 ;
26+
1727 Self {
18- usage : Self :: total_usage ( ) ,
28+ usage,
1929 minimum_digits,
2030 decimal_places,
31+ system,
2132 }
2233 }
23-
24- fn total_usage ( ) -> f32 {
25- let mut sys = System :: new_with_specifics (
26- RefreshKind :: new ( ) . with_memory ( MemoryRefreshKind :: everything ( ) ) ,
27- ) ;
28-
29- sys. refresh_memory ( ) ;
30-
31- ( sys. used_memory ( ) as f32 / sys. total_memory ( ) as f32 ) * 100.0
32- }
3334}
3435
3536impl fmt:: Display for Memory {
@@ -52,4 +53,14 @@ impl ToModule for Memory {
5253 fn icon ( & self ) -> Option < Icon > {
5354 Some ( Icon :: DoubleServer )
5455 }
56+
57+ fn update ( & mut self ) {
58+ let system = & mut self . system ;
59+ system. refresh_memory ( ) ;
60+ self . usage = ( system. used_memory ( ) as f32 / system. total_memory ( ) as f32 ) * 100.0 ;
61+ }
62+
63+ fn next_render_time ( & self ) -> Option < Instant > {
64+ Some ( Instant :: now ( ) + Duration :: from_secs_f32 ( 7.5 ) )
65+ }
5566}
0 commit comments