diff options
author | Matan Kushner <hello@matchai.me> | 2019-04-23 14:51:08 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-23 14:51:08 -0400 |
commit | bb2bcd604b13d15f7dcb7f7fc1d1cc2ae12dda8d (patch) | |
tree | cd5d7f5099c334bad18f77b092881536c20f33b0 /src/modules | |
parent | 33d8beda2d8856ab2fc6673fb54c4b0963b7cc5a (diff) |
Share dir_files between segments through Context (#16)
Diffstat (limited to 'src/modules')
-rw-r--r-- | src/modules/directory.rs | 4 | ||||
-rw-r--r-- | src/modules/nodejs.rs | 62 | ||||
-rw-r--r-- | src/modules/rust.rs | 28 |
3 files changed, 46 insertions, 48 deletions
diff --git a/src/modules/directory.rs b/src/modules/directory.rs index 265b90a3c..83e5613bd 100644 --- a/src/modules/directory.rs +++ b/src/modules/directory.rs @@ -17,7 +17,7 @@ use crate::context::Context; pub fn segment(context: &Context) -> Option<Segment> { const HOME_SYMBOL: &str = "~"; const DIR_TRUNCATION_LENGTH: usize = 3; - const SECTION_COLOR: Color = Color::Cyan; + const SEGMENT_COLOR: Color = Color::Cyan; let mut segment = Segment::new("dir"); let current_dir = &context.current_dir; @@ -41,7 +41,7 @@ pub fn segment(context: &Context) -> Option<Segment> { segment .set_value(truncated_dir_string) - .set_style(SECTION_COLOR.bold()); + .set_style(SEGMENT_COLOR.bold()); Some(segment) } diff --git a/src/modules/nodejs.rs b/src/modules/nodejs.rs index aede7ba63..d8431038a 100644 --- a/src/modules/nodejs.rs +++ b/src/modules/nodejs.rs @@ -1,5 +1,5 @@ use ansi_term::Color; -use std::fs::{self, DirEntry}; +use std::path::PathBuf; use std::process::Command; use super::Segment; @@ -9,46 +9,46 @@ use crate::context::Context; /// /// Will display the Node.js version if any of the following criteria are met: /// - Current directory contains a `.js` file -/// - Current directory contains a `node_modules` directory /// - Current directory contains a `package.json` file +/// - Current directory contains a `node_modules` directory pub fn segment(context: &Context) -> Option<Segment> { - const NODE_CHAR: &str = "⬢"; - const SECTION_COLOR: Color = Color::Green; - - let mut segment = Segment::new("node"); - let current_dir = &context.current_dir; - let files = fs::read_dir(current_dir).unwrap(); - - // Early return if there are no JS project files - let is_js_project = files.filter_map(Result::ok).any(has_js_files); + let is_js_project = context.dir_files.iter().any(has_js_files); if !is_js_project { return None; } - match Command::new("node").arg("--version").output() { - Ok(output) => { - let version = String::from_utf8(output.stdout).unwrap(); - segment.set_value(format!("{} {}", NODE_CHAR, version.trim())) - } - Err(_) => { - return None; - } - }; + match get_node_version() { + Some(node_version) => { + const NODE_CHAR: &str = "⬢"; + const SEGMENT_COLOR: Color = Color::Green; + + let mut segment = Segment::new("node"); + segment.set_style(SEGMENT_COLOR); - segment.set_style(SECTION_COLOR); - Some(segment) + let formatted_version = node_version.trim(); + segment.set_value(format!("{} {}", NODE_CHAR, formatted_version)); + + Some(segment) + } + None => None, + } } -fn has_js_files(dir_entry: DirEntry) -> bool { - let is_js_file = |d: &DirEntry| -> bool { - d.path().is_file() && d.path().extension().unwrap_or_default() == "js" - }; - let is_node_modules = |d: &DirEntry| -> bool { - d.path().is_dir() && d.path().file_name().unwrap_or_default() == "node_modules" - }; - let is_package_json = |d: &DirEntry| -> bool { - d.path().is_file() && d.path().file_name().unwrap_or_default() == "package.json" +fn has_js_files(dir_entry: &PathBuf) -> bool { + let is_js_file = + |d: &PathBuf| -> bool { d.is_file() && d.extension().unwrap_or_default() == "js" }; + let is_node_modules = + |d: &PathBuf| -> bool { d.is_dir() && d.file_name().unwrap_or_default() == "node_modules" }; + let is_package_json = |d: &PathBuf| -> bool { + d.is_file() && d.file_name().unwrap_or_default() == "package.json" }; is_js_file(&dir_entry) || is_node_modules(&dir_entry) || is_package_json(&dir_entry) } + +fn get_node_version() -> Option<String> { + match Command::new("node").arg("--version").output() { + Ok(output) => Some(String::from_utf8(output.stdout).unwrap()), + Err(_) => None, + } +} diff --git a/src/modules/rust.rs b/src/modules/rust.rs index d316815e3..28a4a83f5 100644 --- a/src/modules/rust.rs +++ b/src/modules/rust.rs @@ -1,30 +1,30 @@ use super::Segment; use crate::context::Context; use ansi_term::Color; -use std::fs::{self, DirEntry}; +use std::path::PathBuf; use std::process::Command; /// Creates a segment with the current Rust version /// /// Will display the Rust version if any of the following criteria are met: -/// - Current directory contains a `.rs` or 'Cargo.toml' file +/// - Current directory contains a `.rs` file +/// - Current directory contains a `Cargo.toml` file pub fn segment(context: &Context) -> Option<Segment> { - let files = fs::read_dir(&context.current_dir).unwrap(); - let is_rs_project = files.filter_map(Result::ok).any(has_rs_files); + let is_rs_project = context.dir_files.iter().any(has_rs_files); if !is_rs_project { return None; } match get_rust_version() { Some(rust_version) => { - const RUST_LOGO: &str = "🦀"; - const SECTION_COLOR: Color = Color::Red; + const RUST_CHAR: &str = "🦀"; + const SEGMENT_COLOR: Color = Color::Red; let mut segment = Segment::new("rust"); - segment.set_style(SECTION_COLOR); + segment.set_style(SEGMENT_COLOR); let formatted_version = format_rustc_version(rust_version); - segment.set_value(format!("{} {}", RUST_LOGO, formatted_version)); + segment.set_value(format!("{} {}", RUST_CHAR, formatted_version)); Some(segment) } @@ -32,13 +32,11 @@ pub fn segment(context: &Context) -> Option<Segment> { } } -fn has_rs_files(dir_entry: DirEntry) -> bool { - let is_rs_file = |d: &DirEntry| -> bool { - d.path().is_file() && d.path().extension().unwrap_or_default() == "rs" - }; - let is_cargo_toml = |d: &DirEntry| -> bool { - d.path().is_file() && d.path().file_name().unwrap_or_default() == "Cargo.toml" - }; +fn has_rs_files(dir_entry: &PathBuf) -> bool { + let is_rs_file = + |d: &PathBuf| -> bool { d.is_file() && d.extension().unwrap_or_default() == "rs" }; + let is_cargo_toml = + |d: &PathBuf| -> bool { d.is_file() && d.file_name().unwrap_or_default() == "Cargo.toml" }; is_rs_file(&dir_entry) || is_cargo_toml(&dir_entry) } |