pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
    const HOME_SYMBOL: &str = "~";

    let mut module = context.new_module("directory");
    let config: DirectoryConfig = DirectoryConfig::try_load(module.config);

    module.set_style(config.style);

    // Using environment PWD is the standard approach for determining logical path
    // If this is None for any reason, we fall back to reading the os-provided path
    let physical_current_dir = if config.use_logical_path {
        match std::env::var("PWD") {
            Ok(x) => Some(PathBuf::from(x)),
            Err(e) => {
                log::debug!("Error getting PWD environment variable: {}", e);
                None
            }
        }
    } else {
        match std::env::current_dir() {
            Ok(x) => Some(x),
            Err(e) => {
                log::debug!("Error getting physical current directory: {}", e);
                None
            }
        }
    };
    let current_dir = Path::new(
        physical_current_dir
            .as_ref()
            .unwrap_or_else(|| &context.current_dir),
    );

    let home_dir = dirs::home_dir().unwrap();
    log::debug!("Current directory: {:?}", current_dir);

    let repo = &context.get_repo().ok()?;

    let dir_string = match &repo.root {
        Some(repo_root) if config.truncate_to_repo && (repo_root != &home_dir) => {
            log::debug!("Repo root: {:?}", repo_root);
            // Contract the path to the git repo root
            contract_repo_path(current_dir, repo_root)
                .unwrap_or_else(|| contract_path(current_dir, &home_dir, HOME_SYMBOL))
        }
        // Contract the path to the home directory
        _ => contract_path(current_dir, &home_dir, HOME_SYMBOL),
    };
    log::debug!("Dir string: {}", dir_string);

    let substituted_dir = substitute_path(dir_string, &config.substitutions);

    // Truncate the dir string to the maximum number of path components
    let truncated_dir_string = truncate(substituted_dir, config.truncation_length as usize);

    // Substitutions could have changed the prefix, so don't allow them and
    // fish-style path contraction together
    if config.fish_style_pwd_dir_length > 0 && config.substitutions.is_empty() {
        // If user is using fish style path, we need to add the segment first
        let contracted_home_dir = contract_path(&current_dir, &home_dir, HOME_SYMBOL);
        let fish_style_dir = to_fish_style(
            config.fish_style_pwd_dir_length as usize,
            contracted_home_dir,
            &truncated_dir_string,
        );

        module.create_segment(
            "path",
            &SegmentConfig {
                value: &fish_style_dir,
                style: None,
            },
        );
    }

    module.create_segment(
        "path",
        &SegmentConfig {
            value: &truncated_dir_string,
            style: None,
        },
    );

    module.get_prefix().set_value(config.prefix);

    Some(module)
}
/// Creates a module with the current directory
///
/// Will perform path contraction, substitution, and truncation.
/// **Contraction**
///     - Paths beginning with the home directory or with a git repo right
/// inside the home directory will be contracted to `~`
///     - Paths containing a git repo will contract to begin at the repo root
///
/// **Substitution**
/// Paths will undergo user-provided substitutions of substrings
///
/// **Truncation**
/// Paths will be limited in length to `3` path components by default.
Creates a module with the current directory
Will perform path contraction, substitution, and truncation.
Contraction
Paths beginning with the home directory or with a git repo right
inside the home directory will be contracted to `~`
Paths containing a git repo will contract to begin at the repo root

Substitution
Paths will undergo user-provided substitutions of substrings

Truncation
Paths will be limited in length to `3` path components by default.
Creates a module with the current directory Will perform path contraction substitution and truncation . Contraction Paths beginning with the home directory or with a git repo right inside the home directory will be contracted to ` ~ ` Paths containing a git repo will contract to begin at the repo root Substitution Paths will undergo user - provided substitutions of substrings Truncation Paths will be limited in length to ` 3 ` path components by default .
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
    const HOME_SYMBOL: &str = "~";
    let mut module = context.new_module("directory");
    let config: DirectoryConfig = DirectoryConfig::try_load(module.config);
    module.set_style(config.style);
    // Using environment PWD is the standard approach for determining logical path
    // If this is None for any reason, we fall back to reading the os-provided path
    let physical_current_dir = if config.use_logical_path {
        match std::env::var("PWD") {
            Ok(x) => Some(PathBuf::from(x)),
            Err(e) => {
                log::debug!("Error getting PWD environment variable: {}", e);
                None
            }
        }
    } else {
        match std::env::current_dir() {
            Ok(x) => Some(x),
            Err(e) => {
                log::debug!("Error getting physical current directory: {}", e);
                None
            }
        }
    };
    let current_dir = Path::new(
        physical_current_dir
            .as_ref()
            .unwrap_or_else(|| &context.current_dir),
    );
    let home_dir = dirs::home_dir().unwrap();
    log::debug!("Current directory: {:?}", current_dir);
    let repo = &context.get_repo().ok()?;
    let dir_string = match &repo.root {
        Some(repo_root) if config.truncate_to_repo && (repo_root != &home_dir) => {
            log::debug!("Repo root: {:?}", repo_root);
            // Contract the path to the git repo root
            contract_repo_path(current_dir, repo_root)
                .unwrap_or_else(|| contract_path(current_dir, &home_dir, HOME_SYMBOL))
        }
        // Contract the path to the home directory
        _ => contract_path(current_dir, &home_dir, HOME_SYMBOL),
    };
    log::debug!("Dir string: {}", dir_string);
    let substituted_dir = substitute_path(dir_string, &config.substitutions);
    // Truncate the dir string to the maximum number of path components
    let truncated_dir_string = truncate(substituted_dir, config.truncation_length as usize);
    // Substitutions could have changed the prefix, so don't allow them and
    // fish-style path contraction together
    if config.fish_style_pwd_dir_length > 0 && config.substitutions.is_empty() {
        // If user is using fish style path, we need to add the segment first
        let contracted_home_dir = contract_path(&current_dir, &home_dir, HOME_SYMBOL);
        let fish_style_dir = to_fish_style(
            config.fish_style_pwd_dir_length as usize,
            contracted_home_dir,
            &truncated_dir_string,
        );
        module.create_segment(
            "path",
            &SegmentConfig {
                value: &fish_style_dir,
                style: None,
            },
        );
    }
    module.create_segment(
        "path",
        &SegmentConfig {
            value: &truncated_dir_string,
            style: None,
        },
    );
    module.get_prefix().set_value(config.prefix);
    Some(module)
}
pub fn module < ' a > ( context : & ' a Context ) -> Option < Module < ' a > > { const HOME_SYMBOL : & str = " " ; let mut module = context . new_module ( " " ) ; let config : DirectoryConfig = DirectoryConfig :: try_load ( module . config ) ; module . set_style ( config . style ) ; // Using environment PWD is the standard approach for determining logical path // If this is None for any reason, we fall back to reading the os-provided path let physical_current_dir = if config . use_logical_path { match std :: env :: var ( " " ) { Ok ( x ) => Some ( PathBuf :: from ( x ) ) , Err ( e ) => { log :: debug ! ( " " e ) ; None } } } else { match std :: env :: current_dir ( ) { Ok ( x ) => Some ( x ) , Err ( e ) => { log :: debug ! ( " " e ) ; None } } } ; let current_dir = Path :: new ( physical_current_dir . as_ref ( ) . unwrap_or_else ( | | & context . current_dir ) , ) ; let home_dir = dirs :: home_dir ( ) . unwrap ( ) ; log :: debug ! ( " " current_dir ) ; let repo = & context . get_repo ( ) . ok ( ) ? ; let dir_string = match & repo . root { Some ( repo_root ) if config . truncate_to_repo && ( repo_root != & home_dir ) => { log :: debug ! ( " " repo_root ) ; // Contract the path to the git repo root contract_repo_path ( current_dir , repo_root ) . unwrap_or_else ( | | contract_path ( current_dir , & home_dir , HOME_SYMBOL ) ) } // Contract the path to the home directory _ => contract_path ( current_dir , & home_dir , HOME_SYMBOL ) , } ; log :: debug ! ( " " dir_string ) ; let substituted_dir = substitute_path ( dir_string , & config . substitutions ) ; // Truncate the dir string to the maximum number of path components let truncated_dir_string = truncate ( substituted_dir , config . truncation_length as usize ) ; // Substitutions could have changed the prefix, so don't allow them and // fish-style path contraction together if config . fish_style_pwd_dir_length > 0 && config . substitutions . is_empty ( ) { // If user is using fish style path, we need to add the segment first let contracted_home_dir = contract_path ( & current_dir , & home_dir , HOME_SYMBOL ) ; let fish_style_dir = to_fish_style ( config . fish_style_pwd_dir_length as usize , contracted_home_dir , & truncated_dir_string , ) ; module . create_segment ( " " , & SegmentConfig { value : & fish_style_dir , style : None , } , ) ; } module . create_segment ( " " , & SegmentConfig { value : & truncated_dir_string , style : None , } , ) ; module . get_prefix ( ) . set_value ( config . prefix ) ; Some ( module ) }
Creates a module with the current directory
Will perform path contraction, substitution, and truncation.
Creates a module with the current directory Will perform path contraction substitution and truncation .
context
&'a Context