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

Code before:
    }

    let mut module = context.new_module("meson");
    let config: MesonConfig = MesonConfig::try_load(module.config);

    let truncation_symbol = get_first_grapheme(config.truncation_symbol);
    let len = if config.truncation_length <= 0 {
        log::warn!(
            "\"truncation_length\" should be a positive value, found {}",
            config.truncation_length
        );
        std::usize::MAX
    } else {
        config.truncation_length as usize
    };

    let mut project_name_graphemes: Vec<&str> = project_env.graphemes(true).collect();

    let e = &mut (&mut project_name_graphemes);
    let e = &mut **e;
    let trunc_len = len.min(e.len());
    if trunc_len < e.len() {
        // The truncation symbol should only be added if we truncate
        e[trunc_len] = truncation_symbol;
        e.truncate(trunc_len + 1);
    }

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

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

Review comment (refactor):
This is fine, but I would prefer if you based this on the truncation code in the `hg_branch` module. Moving all truncation to a shared utility function would also be nice-to-have.

Code after:

    let mut module = context.new_module("meson");
    let config: MesonConfig = MesonConfig::try_load(module.config);

    let truncated_text = truncate_text(
        &project_env,
        config.truncation_length as usize,
        config.truncation_symbol,
    );

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

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

    Some(module)
}

#[cfg(test)]
mod tests {
    use crate::test::ModuleRenderer;
    use nu_ansi_term::Color;

    #[test]
    fn not_in_env() {
        let actual = ModuleRenderer::new("meson").collect();

        let expected = None;

        assert_eq!(expected, actual);
    }