Repository: starship/starship
File: src/modules/localip.rs (Rust)

Code before:

use crate::config::RootModuleConfig;
use crate::configs::localip::LocalipConfig;
use crate::formatter::StringFormatter;

/// Creates a module with the ipv4 address of the local machine.
///
/// The `local_ipaddress` crate is used to determine the local IP address of your machine.
/// An accurate and fast way, especially if there are multiple IP addresses available,
/// is to connect a UDP socket and then reading its local endpoint.
///
/// Will display the ip if all of the following criteria are met:
///     - localip.disabled is absent or false
///     - localip.ssh_only is false OR the user is currently connected as an SSH session (`$SSH_CONNECTION`)
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
    let mut module = context.new_module("localip");
    let config: LocalipConfig = LocalipConfig::try_load(module.config);

    let ssh_connection = context.get_env("SSH_CONNECTION");
    if config.ssh_only && ssh_connection.is_none() {
        return None;
    }

    let localip = local_ipaddress::get().unwrap();
    if localip.is_empty() {
        log::warn!("unable to determine local ipv4 address");
        return None;
    }

    let parsed = StringFormatter::new(config.format).and_then(|formatter| {
        formatter
            .map_style(|variable| match variable {
                "style" => Some(Ok(config.style)),
                _ => None,
            })
            .map(|variable| match variable {
                "localip" => Some(Ok(&localip)),
                _ => None,
            })
            .parse(None, Some(context))
    });

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

    Some(module)

Review comment (suggestion):
I don't 100% remember the semantics we have for this kind of stuff, but I think `log::warn!` will warn once per prompt draw, so anyone working with their internet disconnected will see this warning every time starship does a draw. It might be better to demote this to an `info!` log so that it doesn't spam when working offline.

Code after:

use crate::config::RootModuleConfig;
use crate::configs::localip::LocalipConfig;
use crate::formatter::StringFormatter;

/// Creates a module with the ipv4 address of the local machine.
///
/// The `local_ipaddress` crate is used to determine the local IP address of your machine.
/// An accurate and fast way, especially if there are multiple IP addresses available,
/// is to connect a UDP socket and then reading its local endpoint.
///
/// Will display the ip if all of the following criteria are met:
///     - localip.disabled is absent or false
///     - localip.ssh_only is false OR the user is currently connected as an SSH session (`$SSH_CONNECTION`)
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
    let mut module = context.new_module("localip");
    let config: LocalipConfig = LocalipConfig::try_load(module.config);

    let ssh_connection = context.get_env("SSH_CONNECTION");
    if config.ssh_only && ssh_connection.is_none() {
        return None;
    }

    let localip = local_ipaddress::get().unwrap_or_default();
    if localip.is_empty() {
        log::warn!("unable to determine local ipv4 address");
        return None;
    }

    let parsed = StringFormatter::new(config.format).and_then(|formatter| {
        formatter
            .map_style(|variable| match variable {
                "style" => Some(Ok(config.style)),
                _ => None,
            })
            .map(|variable| match variable {
                "localipv4" => Some(Ok(&localip)),
                _ => None,
            })
            .parse(None, Some(context))
    });

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

    Some(module)