1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-12-28 20:12:42 +00:00

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 std::env;
use std::fs;
use std::path::PathBuf;
pub struct Context<'a> {
pub current_dir: PathBuf,
pub dir_files: Vec<PathBuf>,
pub arguments: ArgMatches<'a>,
}
impl<'a> Context<'a> {
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.");
Context {
current_dir,
arguments,
}
Context::new_with_dir(arguments, current_dir)
}
#[allow(dead_code)]
pub fn new_with_dir<T>(arguments: ArgMatches, dir: T) -> Context
where
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 {
current_dir: dir.into(),
current_dir,
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> {
const HOME_SYMBOL: &str = "~";
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 current_dir = &context.current_dir;
@ -41,7 +41,7 @@ pub fn segment(context: &Context) -> Option<Segment> {
segment
.set_value(truncated_dir_string)
.set_style(SECTION_COLOR.bold());
.set_style(SEGMENT_COLOR.bold());
Some(segment)
}

View File

@ -1,5 +1,5 @@
use ansi_term::Color;
use std::fs::{self, DirEntry};
use std::path::PathBuf;
use std::process::Command;
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:
/// - Current directory contains a `.js` file
/// - Current directory contains a `node_modules` directory
/// - Current directory contains a `package.json` file
/// - Current directory contains a `node_modules` directory
pub fn segment(context: &Context) -> Option<Segment> {
const NODE_CHAR: &str = "";
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);
let is_js_project = context.dir_files.iter().any(has_js_files);
if !is_js_project {
return None;
}
match Command::new("node").arg("--version").output() {
Ok(output) => {
let version = String::from_utf8(output.stdout).unwrap();
segment.set_value(format!("{} {}", NODE_CHAR, version.trim()))
}
Err(_) => {
return None;
}
};
match get_node_version() {
Some(node_version) => {
const NODE_CHAR: &str = "";
const SEGMENT_COLOR: Color = Color::Green;
segment.set_style(SECTION_COLOR);
Some(segment)
let mut segment = Segment::new("node");
segment.set_style(SEGMENT_COLOR);
let formatted_version = node_version.trim();
segment.set_value(format!("{} {}", NODE_CHAR, formatted_version));
Some(segment)
}
None => None,
}
}
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"
};
let is_node_modules = |d: &DirEntry| -> bool {
d.path().is_dir() && d.path().file_name().unwrap_or_default() == "node_modules"
};
let is_package_json = |d: &DirEntry| -> bool {
d.path().is_file() && d.path().file_name().unwrap_or_default() == "package.json"
fn has_js_files(dir_entry: &PathBuf) -> bool {
let is_js_file =
|d: &PathBuf| -> bool { d.is_file() && d.extension().unwrap_or_default() == "js" };
let is_node_modules =
|d: &PathBuf| -> bool { d.is_dir() && d.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"
};
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 crate::context::Context;
use ansi_term::Color;
use std::fs::{self, DirEntry};
use std::path::PathBuf;
use std::process::Command;
/// Creates a segment with the current Rust version
///
/// 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> {
let files = fs::read_dir(&context.current_dir).unwrap();
let is_rs_project = files.filter_map(Result::ok).any(has_rs_files);
let is_rs_project = context.dir_files.iter().any(has_rs_files);
if !is_rs_project {
return None;
}
match get_rust_version() {
Some(rust_version) => {
const RUST_LOGO: &str = "🦀";
const SECTION_COLOR: Color = Color::Red;
const RUST_CHAR: &str = "🦀";
const SEGMENT_COLOR: Color = Color::Red;
let mut segment = Segment::new("rust");
segment.set_style(SECTION_COLOR);
segment.set_style(SEGMENT_COLOR);
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)
}
@ -32,13 +32,11 @@ pub fn segment(context: &Context) -> Option<Segment> {
}
}
fn has_rs_files(dir_entry: DirEntry) -> bool {
let is_rs_file = |d: &DirEntry| -> bool {
d.path().is_file() && d.path().extension().unwrap_or_default() == "rs"
};
let is_cargo_toml = |d: &DirEntry| -> bool {
d.path().is_file() && d.path().file_name().unwrap_or_default() == "Cargo.toml"
};
fn has_rs_files(dir_entry: &PathBuf) -> bool {
let is_rs_file =
|d: &PathBuf| -> bool { d.is_file() && d.extension().unwrap_or_default() == "rs" };
let is_cargo_toml =
|d: &PathBuf| -> bool { d.is_file() && d.file_name().unwrap_or_default() == "Cargo.toml" };
is_rs_file(&dir_entry) || is_cargo_toml(&dir_entry)
}

View File

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

View File

@ -3,11 +3,12 @@ use starship::context::Context;
use starship::modules;
use std::path::PathBuf;
#[allow(dead_code)]
pub fn render_segment<T>(module: &str, path: T) -> String
where
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

View File

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