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

Code before:
        Ok(segments) => segments,
        Err(error) => {
            log::warn!("Error in module `gradle`:\n{}", error);
            return None;
        }
    });

    Some(module)
}

fn parse_gradle_version_from_properties(wrapper_properties: &str) -> Option<String> {
    // example gradle.properties content
    /*
        distributionBase=GRADLE_USER_HOME
        distributionPath=wrapper/dists
        distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
        zipStoreBase=GRADLE_USER_HOME
        zipStorePath=wrapper/dists
    */
    let version = wrapper_properties
        .split_once("distributionUrl=")?
        .1
        .split_once("gradle-")?
        .1
        .split_once('-')?
        .0;
    Some(version.to_string())
}

/// Tries to find the gradle-wrapper.properties file.
fn find_wrapper_properties_file(directory: &Path, recursive: bool) -> Option<PathBuf> {
    let properties_path = directory.join("gradle/wrapper/gradle-wrapper.properties");
    if properties_path.exists() {
        return Some(properties_path);
    }

    if recursive {
        find_wrapper_properties_file(directory.parent()?, recursive)
    } else {
        None
    }
}

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

    use super::*;
    use crate::test::ModuleRenderer;
    use std::fs::File;
    use std::io::{self, Write};

Review comment (bug):
On a second look, this may start failing if `gradle-` shows up elsewhere in the URL, this should be a bit less brittle:
```suggestion
    let version = wrapper_properties
        .lines()
        .find(|line| line.starts_with("distributionUrl="))?
        .rsplit_once('/')?
        .1
        .strip_prefix("gradle-")?
        .split_once('-')?
        .0;
```

Code after:

    Some(module)
}

fn parse_gradle_version_from_properties(wrapper_properties: &str) -> Option<String> {
    // example gradle.properties content
    /*
        distributionBase=GRADLE_USER_HOME
        distributionPath=wrapper/dists
        distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
        zipStoreBase=GRADLE_USER_HOME
        zipStorePath=wrapper/dists
    */
    let version = wrapper_properties
        .lines()
        .find(|line| line.starts_with("distributionUrl="))?
        .rsplit_once('/')?
        .1
        .strip_prefix("gradle-")?
        .split_once('-')?
        .0;
    Some(version.to_string())
}

/// Tries to find the gradle-wrapper.properties file.
fn get_wrapper_properties_file(context: &Context, recursive: bool) -> Option<String> {
    let mut properties = None;
    if context
        .try_begin_scan()?
        .set_folders(&["gradle"])
        .is_match()
    {
        properties = utils::read_file(
            &context
                .current_dir
                .join("gradle/wrapper/gradle-wrapper.properties"),
        )
        .ok();
    };
    if recursive && properties.is_none() {
        for dir in context.current_dir.ancestors().skip(1) {
            properties =
                utils::read_file(&dir.join("gradle/wrapper/gradle-wrapper.properties")).ok();
            if properties.is_some() {
                break;
            }
        }
    }
    properties
}