More progress in Node section
This commit is contained in:
parent
d5493d236d
commit
c1f5a733c9
|
@ -1,18 +1,18 @@
|
||||||
use super::Segment;
|
use super::Segment;
|
||||||
use std::env;
|
|
||||||
use std::path::PathBuf;
|
|
||||||
use ansi_term::{Color, Style};
|
use ansi_term::{Color, Style};
|
||||||
use clap::ArgMatches;
|
use clap::ArgMatches;
|
||||||
use dirs;
|
use dirs;
|
||||||
use git2::Repository;
|
use git2::Repository;
|
||||||
|
use std::env;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
/// Creates a segment with the current directory
|
/// Creates a segment with the current directory
|
||||||
///
|
///
|
||||||
/// Will perform path contraction and truncation.
|
/// Will perform path contraction and truncation.
|
||||||
/// **Contraction**
|
/// **Contraction**
|
||||||
/// - Paths begining with the home directory will be contracted to `~`
|
/// - Paths begining with the home directory will be contracted to `~`
|
||||||
/// - Paths containing a git repo will contract to begin at the repo root
|
/// - Paths containing a git repo will contract to begin at the repo root
|
||||||
///
|
///
|
||||||
/// **Truncation**
|
/// **Truncation**
|
||||||
/// Paths will be limited in length to `3` path components by default.
|
/// Paths will be limited in length to `3` path components by default.
|
||||||
pub fn segment(_: &ArgMatches) -> Segment {
|
pub fn segment(_: &ArgMatches) -> Segment {
|
||||||
|
@ -21,8 +21,7 @@ pub fn segment(_: &ArgMatches) -> Segment {
|
||||||
const HOME_SYMBOL: &str = "~";
|
const HOME_SYMBOL: &str = "~";
|
||||||
|
|
||||||
// TODO: Currently gets the physical directory. Get the logical directory.
|
// TODO: Currently gets the physical directory. Get the logical directory.
|
||||||
let current_path = env::current_dir()
|
let current_path = env::current_dir().expect("Unable to identify current directory");
|
||||||
.expect("Unable to identify current directory");
|
|
||||||
|
|
||||||
let dir_string;
|
let dir_string;
|
||||||
if let Ok(repo) = git2::Repository::discover(¤t_path) {
|
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
|
/// 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) {
|
if !full_path.starts_with(top_level_path) {
|
||||||
return full_path.to_str().unwrap().to_string();
|
return full_path.to_str().unwrap().to_string();
|
||||||
}
|
}
|
||||||
|
@ -96,7 +99,9 @@ fn truncate(dir_string: String, length: usize) -> String {
|
||||||
return dir_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 {
|
if components.len() < length {
|
||||||
return dir_string;
|
return dir_string;
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,9 @@ mod nodejs;
|
||||||
use ansi_term::Style;
|
use ansi_term::Style;
|
||||||
use clap::ArgMatches;
|
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 struct Segment {
|
||||||
pub style: Style,
|
pub style: Style,
|
||||||
pub value: String,
|
pub value: String,
|
||||||
|
|
|
@ -1,17 +1,27 @@
|
||||||
use super::Segment;
|
use super::Segment;
|
||||||
use std::process::Command;
|
|
||||||
use ansi_term::{Color, Style};
|
use ansi_term::{Color, Style};
|
||||||
use clap::ArgMatches;
|
use clap::ArgMatches;
|
||||||
|
use std::env;
|
||||||
|
use std::fs::{self, DirEntry};
|
||||||
|
use std::process::Command;
|
||||||
|
|
||||||
/// Creates a segment with the current Node.js version
|
/// 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 NODE_CHAR: &str = "⬢ ";
|
||||||
const SECTION_COLOR: Color = Color::Green;
|
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() {
|
let version = match Command::new("node").arg("--version").output() {
|
||||||
Ok(output) => String::from_utf8(output.stdout).unwrap(),
|
Ok(output) => String::from_utf8(output.stdout).unwrap().trim().to_string(),
|
||||||
Err(e) => {
|
Err(_) => {
|
||||||
println!("{:?}", e);
|
|
||||||
return Segment::default();
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use std::io::{self, Write};
|
|
||||||
use clap::ArgMatches;
|
use clap::ArgMatches;
|
||||||
|
use std::io::{self, Write};
|
||||||
|
|
||||||
use crate::modules;
|
use crate::modules;
|
||||||
use crate::modules::Segment;
|
use crate::modules::Segment;
|
||||||
|
@ -7,12 +7,16 @@ use crate::modules::Segment;
|
||||||
pub fn prompt(args: ArgMatches) {
|
pub fn prompt(args: ArgMatches) {
|
||||||
let default_prompt = vec!["directory", "node", "line_break", "character"];
|
let default_prompt = vec!["directory", "node", "line_break", "character"];
|
||||||
|
|
||||||
|
// TODO:
|
||||||
|
// - List files in directory
|
||||||
|
// - Index binaries in PATH
|
||||||
|
|
||||||
let stdout = io::stdout();
|
let stdout = io::stdout();
|
||||||
let mut handle = stdout.lock();
|
let mut handle = stdout.lock();
|
||||||
|
|
||||||
// Write a new line before the prompt
|
// Write a new line before the prompt
|
||||||
write!(handle, "{}", "\n").unwrap();
|
write!(handle, "{}", "\n").unwrap();
|
||||||
|
|
||||||
default_prompt
|
default_prompt
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|module| modules::handle(module, &args))
|
.map(|module| modules::handle(module, &args))
|
||||||
|
|
Loading…
Reference in New Issue