More progress in Node section

This commit is contained in:
Matan Kushner 2019-04-11 19:31:30 -04:00
parent d5493d236d
commit c1f5a733c9
No known key found for this signature in database
GPG Key ID: 4B98C3A8949CA8A4
4 changed files with 50 additions and 15 deletions

View File

@ -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;
}

View File

@ -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,

View File

@ -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::*;

View File

@ -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))