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

Code before:
use super::{Context, Module};
use std::ffi::OsString;

use crate::config::ModuleConfig;
use crate::configs::hostname::HostnameConfig;
use crate::formatter::StringFormatter;

/// Creates a module with the system hostname
///
/// Will display the hostname if all of the following criteria are met:
///     - `hostname.disabled` is absent or false
///     - `hostname.ssh_only` is false OR the user is currently connected as an SSH session (`$SSH_CONNECTION`)
///     - `hostname.ssh_only` is false AND `hostname.detect_env_vars` is either empty or contains a defined environment variable
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
    let mut module = context.new_module("hostname");
    let config: HostnameConfig = HostnameConfig::try_load(module.config);

    let ssh_connection = context.get_env("SSH_CONNECTION");

    if !((!config.ssh_only || ssh_connection.is_some())
        && context.detect_env_vars(&config.detect_env_vars))
    {
        return None;
    }

    let os_hostname: OsString = gethostname::gethostname();

    let host = match os_hostname.into_string() {
        Ok(host) => host,
        Err(bad) => {
            log::warn!("hostname is not valid UTF!\n{:?}", bad);
            return None;
        }
    };

    //rustc doesn't let you do an "if" and an "if let" in the same if statement
    // if this changes in the future this can become a lot cleaner
    let host = if !config.trim_at.is_empty() {
        if let Some(index) = host.find(config.trim_at) {
            host.split_at(index).0
        } else {
            host.as_ref()
        }
    } else {
        host.as_ref()
    };

Review comment (suggestion):
I think we should reduce the amount of negation here.
```suggestion
    if (config.ssh_only && ssh_connection.is_none())
        || !context.detect_env_vars(&config.detect_env_vars))
```

Code after:
use super::{Context, Module};
use std::ffi::OsString;

use crate::config::ModuleConfig;
use crate::configs::hostname::HostnameConfig;
use crate::formatter::StringFormatter;

/// Creates a module with the system hostname
///
/// Will display the hostname if all of the following criteria are met:
///     - `hostname.disabled` is absent or false
///     - `hostname.ssh_only` is false OR the user is currently connected as an SSH session (`$SSH_CONNECTION`)
///     - `hostname.ssh_only` is false AND `hostname.detect_env_vars` is either empty or contains a defined environment variable
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
    let mut module = context.new_module("hostname");
    let config: HostnameConfig = HostnameConfig::try_load(module.config);

    let ssh_connection = context.get_env("SSH_CONNECTION");

    if (config.ssh_only && ssh_connection.is_none())
        || !context.detect_env_vars(&config.detect_env_vars)
    {
        return None;
    }

    let os_hostname: OsString = gethostname::gethostname();

    let host = match os_hostname.into_string() {
        Ok(host) => host,
        Err(bad) => {
            log::warn!("hostname is not valid UTF!\n{:?}", bad);
            return None;
        }
    };

    //rustc doesn't let you do an "if" and an "if let" in the same if statement
    // if this changes in the future this can become a lot cleaner
    let host = if !config.trim_at.is_empty() {
        if let Some(index) = host.find(config.trim_at) {
            host.split_at(index).0
        } else {
            host.as_ref()
        }
    } else {
        host.as_ref()
    };