summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMatan Kushner <hello@matchai.me>2019-04-12 13:07:41 -0400
committerMatan Kushner <hello@matchai.me>2019-04-12 13:07:41 -0400
commit99bdf27ecac655b54b03d7389b1b24970a1ec9ec (patch)
treeacf2b6db45712d768d0247dc42126bc44a89a0d9 /src
parent7ffadd37bc3feaae3b636f64169176c041f6684a (diff)
Parallelize nodejs file checks
Diffstat (limited to 'src')
-rw-r--r--src/main.rs1
-rw-r--r--src/modules/nodejs.rs14
2 files changed, 10 insertions, 5 deletions
diff --git a/src/main.rs b/src/main.rs
index b8f11cfc9..63c495820 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -4,6 +4,7 @@ extern crate clap;
extern crate ansi_term;
extern crate dirs;
extern crate git2;
+extern crate rayon;
mod modules;
mod print;
diff --git a/src/modules/nodejs.rs b/src/modules/nodejs.rs
index 87a1e4a25..932709f6d 100644
--- a/src/modules/nodejs.rs
+++ b/src/modules/nodejs.rs
@@ -1,12 +1,13 @@
use super::Segment;
use ansi_term::{Color, Style};
use clap::ArgMatches;
+use rayon::prelude::*;
use std::env;
use std::fs::{self, DirEntry};
use std::process::Command;
/// Creates a segment with the current Node.js version
-///
+///
/// 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
@@ -16,10 +17,13 @@ pub fn segment(_: &ArgMatches) -> Segment {
const SECTION_COLOR: Color = Color::Green;
let current_path = env::current_dir().expect("Unable to identify current directory");
- let files = fs::read_dir(&current_path).unwrap();
+ let files = fs::read_dir(&current_path)
+ .unwrap()
+ .filter_map(Result::ok)
+ .collect::<Vec<DirEntry>>();
// 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 = files.par_iter().any(has_js_files);
if !is_js_project {
return Segment::default();
}
@@ -38,7 +42,7 @@ pub fn segment(_: &ArgMatches) -> Segment {
}
}
-fn has_js_files(dir_entry: DirEntry) -> bool {
+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"
};
@@ -49,5 +53,5 @@ fn has_js_files(dir_entry: DirEntry) -> bool {
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)
+ is_js_file(dir_entry) || is_node_modules(dir_entry) || is_package_json(dir_entry)
}