diff --git a/src/modules/directory.rs b/src/modules/directory.rs index 20dcae3d..bb957708 100644 --- a/src/modules/directory.rs +++ b/src/modules/directory.rs @@ -1,18 +1,18 @@ use super::Segment; -use std::env; -use std::path::PathBuf; use ansi_term::{Color, Style}; use clap::ArgMatches; use dirs; use git2::Repository; +use std::env; +use std::path::PathBuf; /// Creates a segment with the current directory -/// +/// /// Will perform path contraction and truncation. /// **Contraction** /// - Paths begining with the home directory will be contracted to `~` /// - Paths containing a git repo will contract to begin at the repo root -/// +/// /// **Truncation** /// Paths will be limited in length to `3` path components by default. pub fn segment(_: &ArgMatches) -> Segment { @@ -21,8 +21,7 @@ pub fn segment(_: &ArgMatches) -> Segment { const HOME_SYMBOL: &str = "~"; // TODO: Currently gets the physical directory. Get the logical directory. - let current_path = env::current_dir() - .expect("Unable to identify current directory"); + let current_path = env::current_dir().expect("Unable to identify current directory"); let dir_string; if let Ok(repo) = git2::Repository::discover(¤t_path) { @@ -66,7 +65,11 @@ fn get_repo_root(repo: Repository) -> PathBuf { } /// Contract the root component of a path -fn contract_path(full_path: &PathBuf, top_level_path: &PathBuf, top_level_replacement: &str) -> String { +fn contract_path( + full_path: &PathBuf, + top_level_path: &PathBuf, + top_level_replacement: &str, +) -> String { if !full_path.starts_with(top_level_path) { return full_path.to_str().unwrap().to_string(); } @@ -96,7 +99,9 @@ fn truncate(dir_string: String, length: usize) -> String { return dir_string; } - let components = dir_string.split(std::path::MAIN_SEPARATOR).collect::>(); + let components = dir_string + .split(std::path::MAIN_SEPARATOR) + .collect::>(); if components.len() < length { return dir_string; } diff --git a/src/modules/mod.rs b/src/modules/mod.rs index a51904dd..5fa37593 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -6,6 +6,9 @@ mod nodejs; use ansi_term::Style; use clap::ArgMatches; +// pub static current_dir: PathBuf = env::current_dir().expect("Unable to identify current directory"); +// TODO: Currently gets the physical directory. Get the logical directory. + pub struct Segment { pub style: Style, pub value: String, diff --git a/src/modules/nodejs.rs b/src/modules/nodejs.rs index 16e76841..3c362f5d 100644 --- a/src/modules/nodejs.rs +++ b/src/modules/nodejs.rs @@ -1,17 +1,27 @@ use super::Segment; -use std::process::Command; use ansi_term::{Color, Style}; use clap::ArgMatches; +use std::env; +use std::fs::{self, DirEntry}; +use std::process::Command; /// Creates a segment with the current Node.js version -pub fn segment(args: &ArgMatches) -> Segment { +pub fn segment(_: &ArgMatches) -> Segment { const NODE_CHAR: &str = "⬢ "; const SECTION_COLOR: Color = Color::Green; + let current_path = env::current_dir().expect("Unable to identify current directory"); + let files = fs::read_dir(¤t_path).unwrap(); + + let is_js_project = files.filter_map(Result::ok).any(has_js_files); + + if is_js_project { + return Segment::default(); + } + let version = match Command::new("node").arg("--version").output() { - Ok(output) => String::from_utf8(output.stdout).unwrap(), - Err(e) => { - println!("{:?}", e); + Ok(output) => String::from_utf8(output.stdout).unwrap().trim().to_string(), + Err(_) => { return Segment::default(); } }; @@ -23,6 +33,19 @@ pub fn segment(args: &ArgMatches) -> Segment { } } +fn has_js_files(dir_entry: DirEntry) -> bool { + let is_js_file = + |d: &DirEntry| d.path().is_file() && d.path().extension().unwrap_or_default() == "js"; + let is_node_modules = |d: &DirEntry| { + d.path().is_dir() && d.path().file_name().unwrap_or_default() == "node_modules" + }; + let is_package_json = |d: &DirEntry| { + d.path().is_file() && d.path().file_name().unwrap_or_default() == "package.json" + }; + + is_js_file(&dir_entry) || is_node_modules(&dir_entry) || is_package_json(&dir_entry) +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/print.rs b/src/print.rs index 4251a720..4371c5ec 100644 --- a/src/print.rs +++ b/src/print.rs @@ -1,5 +1,5 @@ -use std::io::{self, Write}; use clap::ArgMatches; +use std::io::{self, Write}; use crate::modules; use crate::modules::Segment; @@ -7,12 +7,16 @@ use crate::modules::Segment; pub fn prompt(args: ArgMatches) { let default_prompt = vec!["directory", "node", "line_break", "character"]; + // TODO: + // - List files in directory + // - Index binaries in PATH + let stdout = io::stdout(); let mut handle = stdout.lock(); // Write a new line before the prompt write!(handle, "{}", "\n").unwrap(); - + default_prompt .into_iter() .map(|module| modules::handle(module, &args))