pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
    let mut module = context.new_module("memory_usage");
    let config = MemoryConfig::try_load(module.config);

    // TODO: Update when v1.0 printing refactor is implemented to only
    // print escapes in a prompt context.
    let pct_sign = match context.shell {
        Shell::Zsh => "%%", // % is an escape in zsh, see PROMPT in `man zshmisc`
        _ => "%",
    };

    // As we default to disabled=true, we have to check here after loading our config module,
    // before it was only checking against whatever is in the config starship.toml
    if config.disabled {
        return None;
    }

    let system = sysinfo::System::new_with_specifics(RefreshKind::new().with_memory());
    let used_memory_kib = system.get_used_memory();
    let total_memory_kib = system.get_total_memory();
    let ram_used = (used_memory_kib as f64 / total_memory_kib as f64) * 100.;
    let ram_pct = format_pct(ram_used, pct_sign);

    let threshold = config.threshold;
    if ram_used.round() < threshold as f64 {
        return None;
    }

    let ram = format_usage_total(used_memory_kib, total_memory_kib);
    let total_swap_kib = system.get_total_swap();
    let used_swap_kib = system.get_used_swap();
    let percent_swap_used = (used_swap_kib as f64 / total_swap_kib as f64) * 100.;
    let swap_pct = format_pct(percent_swap_used, pct_sign);
    let swap = format_usage_total(used_swap_kib, total_swap_kib);

    let parsed = StringFormatter::new(config.format).and_then(|formatter| {
        formatter
            .map_meta(|var, _| match var {
                "symbol" => Some(config.symbol),
                _ => None,
            })
            .map_style(|variable| match variable {
                "style" => Some(Ok(config.style)),
                _ => None,
            })
            .map(|variable| match variable {
                "ram" => Some(Ok(&ram)),
                "ram_pct" => Some(Ok(&ram_pct)),
                // swap only shown if there is swap on the system
                "swap" if total_swap_kib > 0 => Some(Ok(&swap)),
                "swap_pct" if total_swap_kib > 0 => Some(Ok(&swap_pct)),
                _ => None,
            })
            .parse(None)
    });

    module.set_segments(match parsed {
        Ok(segments) => segments,
        Err(error) => {
            log::warn!("Error in module `memory_usage`:\n{}", error);
            return None;
        }
    });

    Some(module)
}
/// Creates a module with system memory usage information
Creates a module with system memory usage information
Creates a module with system memory usage information
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
    let mut module = context.new_module("memory_usage");
    let config = MemoryConfig::try_load(module.config);
    // TODO: Update when v1.0 printing refactor is implemented to only
    // print escapes in a prompt context.
    let pct_sign = match context.shell {
        Shell::Zsh => "%%", // % is an escape in zsh, see PROMPT in `man zshmisc`
        _ => "%",
    };
    // As we default to disabled=true, we have to check here after loading our config module,
    // before it was only checking against whatever is in the config starship.toml
    if config.disabled {
        return None;
    }
    let system = sysinfo::System::new_with_specifics(RefreshKind::new().with_memory());
    let used_memory_kib = system.get_used_memory();
    let total_memory_kib = system.get_total_memory();
    let ram_used = (used_memory_kib as f64 / total_memory_kib as f64) * 100.;
    let ram_pct = format_pct(ram_used, pct_sign);
    let threshold = config.threshold;
    if ram_used.round() < threshold as f64 {
        return None;
    }
    let ram = format_usage_total(used_memory_kib, total_memory_kib);
    let total_swap_kib = system.get_total_swap();
    let used_swap_kib = system.get_used_swap();
    let percent_swap_used = (used_swap_kib as f64 / total_swap_kib as f64) * 100.;
    let swap_pct = format_pct(percent_swap_used, pct_sign);
    let swap = format_usage_total(used_swap_kib, total_swap_kib);
    let parsed = StringFormatter::new(config.format).and_then(|formatter| {
        formatter
            .map_meta(|var, _| match var {
                "symbol" => Some(config.symbol),
                _ => None,
            })
            .map_style(|variable| match variable {
                "style" => Some(Ok(config.style)),
                _ => None,
            })
            .map(|variable| match variable {
                "ram" => Some(Ok(&ram)),
                "ram_pct" => Some(Ok(&ram_pct)),
                // swap only shown if there is swap on the system
                "swap" if total_swap_kib > 0 => Some(Ok(&swap)),
                "swap_pct" if total_swap_kib > 0 => Some(Ok(&swap_pct)),
                _ => None,
            })
            .parse(None)
    });
    module.set_segments(match parsed {
        Ok(segments) => segments,
        Err(error) => {
            log::warn!("Error in module `memory_usage`:\n{}", error);
            return None;
        }
    });
    Some(module)
}
pub fn module < ' a > ( context : & ' a Context ) -> Option < Module < ' a > > { let mut module = context . new_module ( " " ) ; let config = MemoryConfig :: try_load ( module . config ) ; // TODO: Update when v1.0 printing refactor is implemented to only // print escapes in a prompt context. let pct_sign = match context . shell { Shell :: Zsh => " " , // % is an escape in zsh, see PROMPT in `man zshmisc` _ => " " , } ; // As we default to disabled=true, we have to check here after loading our config module, // before it was only checking against whatever is in the config starship.toml if config . disabled { return None ; } let system = sysinfo :: System :: new_with_specifics ( RefreshKind :: new ( ) . with_memory ( ) ) ; let used_memory_kib = system . get_used_memory ( ) ; let total_memory_kib = system . get_total_memory ( ) ; let ram_used = ( used_memory_kib as f64 / total_memory_kib as f64 ) * 100. ; let ram_pct = format_pct ( ram_used , pct_sign ) ; let threshold = config . threshold ; if ram_used . round ( ) < threshold as f64 { return None ; } let ram = format_usage_total ( used_memory_kib , total_memory_kib ) ; let total_swap_kib = system . get_total_swap ( ) ; let used_swap_kib = system . get_used_swap ( ) ; let percent_swap_used = ( used_swap_kib as f64 / total_swap_kib as f64 ) * 100. ; let swap_pct = format_pct ( percent_swap_used , pct_sign ) ; let swap = format_usage_total ( used_swap_kib , total_swap_kib ) ; let parsed = StringFormatter :: new ( config . format ) . and_then ( | formatter | { formatter . map_meta ( | var , _ | match var { " " => Some ( config . symbol ) , _ => None , } ) . map_style ( | variable | match variable { " " => Some ( Ok ( config . style ) ) , _ => None , } ) . map ( | variable | match variable { " " => Some ( Ok ( & ram ) ) , " " => Some ( Ok ( & ram_pct ) ) , // swap only shown if there is swap on the system " " if total_swap_kib > 0 => Some ( Ok ( & swap ) ) , " " if total_swap_kib > 0 => Some ( Ok ( & swap_pct ) ) , _ => None , } ) . parse ( None ) } ) ; module . set_segments ( match parsed { Ok ( segments ) => segments , Err ( error ) => { log :: warn ! ( " \n " error ) ; return None ; } } ) ; Some ( module ) }
Creates a module with system memory usage information
Creates a module with system memory usage information
context
&'a Context