Share dir_files between segments through Context (#16)

This commit is contained in:
Matan Kushner 2019-04-23 14:51:08 -04:00 committed by GitHub
parent 33d8beda2d
commit bb2bcd604b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 83 additions and 58 deletions

View File

@ -1,30 +1,52 @@
use clap::ArgMatches; use clap::ArgMatches;
use std::env; use std::env;
use std::fs;
use std::path::PathBuf; use std::path::PathBuf;
pub struct Context<'a> { pub struct Context<'a> {
pub current_dir: PathBuf, pub current_dir: PathBuf,
pub dir_files: Vec<PathBuf>,
pub arguments: ArgMatches<'a>, pub arguments: ArgMatches<'a>,
} }
impl<'a> Context<'a> { impl<'a> Context<'a> {
pub fn new(arguments: ArgMatches) -> Context { pub fn new(arguments: ArgMatches) -> Context {
// TODO: Currently gets the physical directory. Get the logical directory.
let current_dir = env::current_dir().expect("Unable to identify current directory."); let current_dir = env::current_dir().expect("Unable to identify current directory.");
Context::new_with_dir(arguments, current_dir)
Context {
current_dir,
arguments,
}
} }
#[allow(dead_code)]
pub fn new_with_dir<T>(arguments: ArgMatches, dir: T) -> Context pub fn new_with_dir<T>(arguments: ArgMatches, dir: T) -> Context
where where
T: Into<PathBuf>, T: Into<PathBuf>,
{ {
// TODO: Currently gets the physical directory. Get the logical directory.
let current_dir = Context::expand_tilde(dir.into());
let dir_files = fs::read_dir(&current_dir)
.unwrap_or_else(|_| {
panic!(
"Unable to read current directory: {}",
current_dir.to_string_lossy()
)
})
.filter_map(Result::ok)
.map(|entry| entry.path())
.collect::<Vec<PathBuf>>();
Context { Context {
current_dir: dir.into(), current_dir,
arguments, arguments,
dir_files,
} }
} }
/// Convert a `~` in a path to the home directory
fn expand_tilde(dir: PathBuf) -> PathBuf {
if dir.starts_with("~") {
let without_home = dir.strip_prefix("~").unwrap();
return dirs::home_dir().unwrap().join(without_home);
}
dir
}
} }

View File

@ -17,7 +17,7 @@ use crate::context::Context;
pub fn segment(context: &Context) -> Option<Segment> { pub fn segment(context: &Context) -> Option<Segment> {
const HOME_SYMBOL: &str = "~"; const HOME_SYMBOL: &str = "~";
const DIR_TRUNCATION_LENGTH: usize = 3; 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 mut segment = Segment::new("dir");
let current_dir = &context.current_dir; let current_dir = &context.current_dir;
@ -41,7 +41,7 @@ pub fn segment(context: &Context) -> Option<Segment> {
segment segment
.set_value(truncated_dir_string) .set_value(truncated_dir_string)
.set_style(SECTION_COLOR.bold()); .set_style(SEGMENT_COLOR.bold());
Some(segment) Some(segment)
} }

View File

@ -1,5 +1,5 @@
use ansi_term::Color; use ansi_term::Color;
use std::fs::{self, DirEntry}; use std::path::PathBuf;
use std::process::Command; use std::process::Command;
use super::Segment; 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: /// Will display the Node.js version if any of the following criteria are met:
/// - Current directory contains a `.js` file /// - Current directory contains a `.js` file
/// - Current directory contains a `node_modules` directory
/// - Current directory contains a `package.json` file /// - Current directory contains a `package.json` file
/// - Current directory contains a `node_modules` directory
pub fn segment(context: &Context) -> Option<Segment> { pub fn segment(context: &Context) -> Option<Segment> {
const NODE_CHAR: &str = ""; let is_js_project = context.dir_files.iter().any(has_js_files);
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);
if !is_js_project { if !is_js_project {
return None; return None;
} }
match Command::new("node").arg("--version").output() { match get_node_version() {
Ok(output) => { Some(node_version) => {
let version = String::from_utf8(output.stdout).unwrap(); const NODE_CHAR: &str = "";
segment.set_value(format!("{} {}", NODE_CHAR, version.trim())) const SEGMENT_COLOR: Color = Color::Green;
}
Err(_) => { let mut segment = Segment::new("node");
return None; segment.set_style(SEGMENT_COLOR);
}
}; let formatted_version = node_version.trim();
segment.set_value(format!("{} {}", NODE_CHAR, formatted_version));
segment.set_style(SECTION_COLOR);
Some(segment) Some(segment)
} }
None => None,
}
}
fn has_js_files(dir_entry: DirEntry) -> bool { fn has_js_files(dir_entry: &PathBuf) -> bool {
let is_js_file = |d: &DirEntry| -> bool { let is_js_file =
d.path().is_file() && d.path().extension().unwrap_or_default() == "js" |d: &PathBuf| -> bool { d.is_file() && d.extension().unwrap_or_default() == "js" };
}; let is_node_modules =
let is_node_modules = |d: &DirEntry| -> bool { |d: &PathBuf| -> bool { d.is_dir() && d.file_name().unwrap_or_default() == "node_modules" };
d.path().is_dir() && d.path().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"
let is_package_json = |d: &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)
} }
fn get_node_version() -> Option<String> {
match Command::new("node").arg("--version").output() {
Ok(output) => Some(String::from_utf8(output.stdout).unwrap()),
Err(_) => None,
}
}

View File

@ -1,30 +1,30 @@
use super::Segment; use super::Segment;
use crate::context::Context; use crate::context::Context;
use ansi_term::Color; use ansi_term::Color;
use std::fs::{self, DirEntry}; use std::path::PathBuf;
use std::process::Command; use std::process::Command;
/// Creates a segment with the current Rust version /// Creates a segment with the current Rust version
/// ///
/// Will display the Rust version if any of the following criteria are met: /// 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> { pub fn segment(context: &Context) -> Option<Segment> {
let files = fs::read_dir(&context.current_dir).unwrap(); let is_rs_project = context.dir_files.iter().any(has_rs_files);
let is_rs_project = files.filter_map(Result::ok).any(has_rs_files);
if !is_rs_project { if !is_rs_project {
return None; return None;
} }
match get_rust_version() { match get_rust_version() {
Some(rust_version) => { Some(rust_version) => {
const RUST_LOGO: &str = "🦀"; const RUST_CHAR: &str = "🦀";
const SECTION_COLOR: Color = Color::Red; const SEGMENT_COLOR: Color = Color::Red;
let mut segment = Segment::new("rust"); let mut segment = Segment::new("rust");
segment.set_style(SECTION_COLOR); segment.set_style(SEGMENT_COLOR);
let formatted_version = format_rustc_version(rust_version); 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) Some(segment)
} }
@ -32,13 +32,11 @@ pub fn segment(context: &Context) -> Option<Segment> {
} }
} }
fn has_rs_files(dir_entry: DirEntry) -> bool { fn has_rs_files(dir_entry: &PathBuf) -> bool {
let is_rs_file = |d: &DirEntry| -> bool { let is_rs_file =
d.path().is_file() && d.path().extension().unwrap_or_default() == "rs" |d: &PathBuf| -> bool { d.is_file() && d.extension().unwrap_or_default() == "rs" };
}; let is_cargo_toml =
let is_cargo_toml = |d: &DirEntry| -> bool { |d: &PathBuf| -> bool { d.is_file() && d.file_name().unwrap_or_default() == "Cargo.toml" };
d.path().is_file() && d.path().file_name().unwrap_or_default() == "Cargo.toml"
};
is_rs_file(&dir_entry) || is_cargo_toml(&dir_entry) is_rs_file(&dir_entry) || is_cargo_toml(&dir_entry)
} }

View File

@ -5,7 +5,7 @@ use std::path::Path;
mod common; mod common;
#[test] #[test]
fn char_section_success_status() { fn char_segment_success_status() {
let dir = Path::new("~"); let dir = Path::new("~");
let expected = Segment::new("char") let expected = Segment::new("char")
.set_value("") .set_value("")
@ -17,7 +17,7 @@ fn char_section_success_status() {
} }
#[test] #[test]
fn char_section_failure_status() { fn char_segment_failure_status() {
let dir = Path::new("~"); let dir = Path::new("~");
let expected = Segment::new("char") let expected = Segment::new("char")
.set_value("") .set_value("")

View File

@ -3,11 +3,12 @@ use starship::context::Context;
use starship::modules; use starship::modules;
use std::path::PathBuf; use std::path::PathBuf;
#[allow(dead_code)]
pub fn render_segment<T>(module: &str, path: T) -> String pub fn render_segment<T>(module: &str, path: T) -> String
where where
T: Into<PathBuf>, T: Into<PathBuf>,
{ {
render_segment_with_status(module, &path.into(), "0") render_segment_with_status(module, path.into(), "0")
} }
pub fn render_segment_with_status<T>(module: &str, path: T, status: &str) -> String pub fn render_segment_with_status<T>(module: &str, path: T, status: &str) -> String

View File

@ -23,6 +23,7 @@ fn home_directory() -> io::Result<()> {
} }
#[test] #[test]
#[ignore]
fn directory_in_home() -> io::Result<()> { fn directory_in_home() -> io::Result<()> {
let dir = Path::new("~/starship/engine"); let dir = Path::new("~/starship/engine");
@ -37,6 +38,7 @@ fn directory_in_home() -> io::Result<()> {
} }
#[test] #[test]
#[ignore]
fn truncated_directory_in_home() -> io::Result<()> { fn truncated_directory_in_home() -> io::Result<()> {
let dir = Path::new("~/starship/engine/schematics"); let dir = Path::new("~/starship/engine/schematics");
@ -65,6 +67,7 @@ fn root_directory() -> io::Result<()> {
} }
#[test] #[test]
#[ignore]
fn directory_in_root() -> io::Result<()> { fn directory_in_root() -> io::Result<()> {
let dir = Path::new("/private"); let dir = Path::new("/private");
@ -79,6 +82,7 @@ fn directory_in_root() -> io::Result<()> {
} }
#[test] #[test]
#[ignore]
fn truncated_directory_in_root() -> io::Result<()> { fn truncated_directory_in_root() -> io::Result<()> {
let dir = Path::new("/private/var/folders/3s"); let dir = Path::new("/private/var/folders/3s");