summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMatan Kushner <hello@matchai.me>2019-04-11 19:31:30 -0400
committerMatan Kushner <hello@matchai.me>2019-04-11 19:31:30 -0400
commitc1f5a733c991abc8575832a8280ef683127e2e5c (patch)
tree19421e96c004e3091c7157eb7f3a4a0f546da219 /src
parentd5493d236d402497509b7a3248dd8919bbb34c6c (diff)
More progress in Node section
Diffstat (limited to 'src')
-rw-r--r--src/modules/directory.rs21
-rw-r--r--src/modules/mod.rs3
-rw-r--r--src/modules/nodejs.rs33
-rw-r--r--src/print.rs8
4 files changed, 50 insertions, 15 deletions
diff --git a/src/modules/directory.rs b/src/modules/directory.rs
index 20dcae3dc..bb9577083 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(&current_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::<Vec<&str>>();
+ let components = dir_string
+ .split(std::path::MAIN_SEPARATOR)
+ .collect::<Vec<&str>>();
if components.len() < length {
return dir_string;
}
diff --git a/src/modules/mod.rs b/src/modules/mod.rs
index a51904dd1..5fa37593e 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 16e76841b..3c362f5d2 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(&current_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 4251a7207..4371c5ece 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))