mirror of
https://github.com/Llewellynvdm/starship.git
synced 2024-12-01 01:04:00 +00:00
Make segments optionals
This commit is contained in:
parent
d62bb107f2
commit
9d4492c313
@ -10,7 +10,7 @@ use clap::ArgMatches;
|
|||||||
/// (green by default)
|
/// (green by default)
|
||||||
/// - If the exit-code was anything else, the arrow will be formatted with
|
/// - If the exit-code was anything else, the arrow will be formatted with
|
||||||
/// `COLOR_FAILURE` (red by default)
|
/// `COLOR_FAILURE` (red by default)
|
||||||
pub fn segment(args: &ArgMatches) -> Segment {
|
pub fn segment(args: &ArgMatches) -> Option<Segment> {
|
||||||
const PROMPT_CHAR: &str = "➜";
|
const PROMPT_CHAR: &str = "➜";
|
||||||
const COLOR_SUCCESS: Color = Color::Green;
|
const COLOR_SUCCESS: Color = Color::Green;
|
||||||
const COLOR_FAILURE: Color = Color::Red;
|
const COLOR_FAILURE: Color = Color::Red;
|
||||||
@ -23,7 +23,9 @@ pub fn segment(args: &ArgMatches) -> Segment {
|
|||||||
segment.set_style(COLOR_FAILURE);
|
segment.set_style(COLOR_FAILURE);
|
||||||
};
|
};
|
||||||
|
|
||||||
segment.set_value(PROMPT_CHAR).clone()
|
segment.set_value(PROMPT_CHAR).set_prefix(None);
|
||||||
|
|
||||||
|
Some(segment)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -15,7 +15,7 @@ use std::path::Path;
|
|||||||
///
|
///
|
||||||
/// **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) -> 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 SECTION_COLOR: Color = Color::Cyan;
|
||||||
@ -44,8 +44,9 @@ pub fn segment(_: &ArgMatches) -> Segment {
|
|||||||
|
|
||||||
segment
|
segment
|
||||||
.set_value(truncated_dir_string)
|
.set_value(truncated_dir_string)
|
||||||
.set_style(SECTION_COLOR.bold())
|
.set_style(SECTION_COLOR.bold());
|
||||||
.clone()
|
|
||||||
|
Some(segment)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the root directory of a git repo
|
/// Get the root directory of a git repo
|
||||||
|
@ -2,10 +2,15 @@ use super::Segment;
|
|||||||
use clap::ArgMatches;
|
use clap::ArgMatches;
|
||||||
|
|
||||||
/// Creates a segment for the line break
|
/// Creates a segment for the line break
|
||||||
pub fn segment(_: &ArgMatches) -> Segment {
|
pub fn segment(_: &ArgMatches) -> Option<Segment> {
|
||||||
const LINE_ENDING: &str = "\n";
|
const LINE_ENDING: &str = "\n";
|
||||||
|
|
||||||
let mut segment = Segment::new("line_break");
|
let mut segment = Segment::new("line_break");
|
||||||
|
|
||||||
segment.set_value(LINE_ENDING).clone()
|
segment
|
||||||
|
.set_value(LINE_ENDING)
|
||||||
|
.set_prefix(None)
|
||||||
|
.set_suffix(None);
|
||||||
|
|
||||||
|
Some(segment)
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ use clap::ArgMatches;
|
|||||||
// pub static current_dir: PathBuf = env::current_dir().expect("Unable to identify current directory");
|
// pub static current_dir: PathBuf = env::current_dir().expect("Unable to identify current directory");
|
||||||
// TODO: Currently gets the physical directory. Get the logical directory.
|
// TODO: Currently gets the physical directory. Get the logical directory.
|
||||||
|
|
||||||
pub fn handle(module: &str, args: &ArgMatches) -> Segment {
|
pub fn handle(module: &str, args: &ArgMatches) -> Option<Segment> {
|
||||||
match module {
|
match module {
|
||||||
"dir" | "directory" => directory::segment(&args),
|
"dir" | "directory" => directory::segment(&args),
|
||||||
"char" | "character" => character::segment(&args),
|
"char" | "character" => character::segment(&args),
|
||||||
|
@ -11,7 +11,7 @@ use std::process::Command;
|
|||||||
/// - Current directory contains a `.js` file
|
/// - Current directory contains a `.js` file
|
||||||
/// - Current directory contains a `node_modules` directory
|
/// - Current directory contains a `node_modules` directory
|
||||||
/// - Current directory contains a `package.json` file
|
/// - Current directory contains a `package.json` file
|
||||||
pub fn segment(_: &ArgMatches) -> Segment {
|
pub fn segment(_: &ArgMatches) -> Option<Segment> {
|
||||||
const NODE_CHAR: &str = "⬢";
|
const NODE_CHAR: &str = "⬢";
|
||||||
const SECTION_COLOR: Color = Color::Green;
|
const SECTION_COLOR: Color = Color::Green;
|
||||||
|
|
||||||
@ -22,7 +22,7 @@ pub fn segment(_: &ArgMatches) -> Segment {
|
|||||||
// Early return if there are no JS project files
|
// 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 = files.filter_map(Result::ok).any(has_js_files);
|
||||||
if !is_js_project {
|
if !is_js_project {
|
||||||
return segment;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
match Command::new("node").arg("--version").output() {
|
match Command::new("node").arg("--version").output() {
|
||||||
@ -31,11 +31,12 @@ pub fn segment(_: &ArgMatches) -> Segment {
|
|||||||
segment.set_value(format!("{} {}", NODE_CHAR, version.trim()))
|
segment.set_value(format!("{} {}", NODE_CHAR, version.trim()))
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
return segment;
|
return None;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
segment.set_style(SECTION_COLOR).clone()
|
segment.set_style(SECTION_COLOR);
|
||||||
|
Some(segment)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn has_js_files(dir_entry: DirEntry) -> bool {
|
fn has_js_files(dir_entry: DirEntry) -> bool {
|
||||||
|
@ -18,7 +18,9 @@ pub fn prompt(args: ArgMatches) {
|
|||||||
|
|
||||||
default_prompt
|
default_prompt
|
||||||
.iter()
|
.iter()
|
||||||
.map(|module| modules::handle(module, &args))
|
.map(|module| modules::handle(module, &args)) // Compute segments
|
||||||
.map(|segment| segment.output())
|
.flatten() // Remove segments set to `None`
|
||||||
|
.enumerate() // Turn segment into tuple with index
|
||||||
|
.map(|(index, segment)| segment.output(index)) // Generate string outputs
|
||||||
.for_each(|segment_string| write!(handle, "{}", segment_string).unwrap());
|
.for_each(|segment_string| write!(handle, "{}", segment_string).unwrap());
|
||||||
}
|
}
|
||||||
|
@ -56,13 +56,13 @@ impl Segment {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_prefix(&mut self, prefix: Segment) -> &mut Segment {
|
pub fn set_prefix(&mut self, prefix: OptionalSegment) -> &mut Segment {
|
||||||
self.prefix = Some(Box::new(prefix));
|
self.prefix = prefix;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_suffix(&mut self, suffix: Segment) -> &mut Segment {
|
pub fn set_suffix(&mut self, suffix: OptionalSegment) -> &mut Segment {
|
||||||
self.suffix = Some(Box::new(suffix));
|
self.suffix = suffix;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,7 +70,7 @@ impl Segment {
|
|||||||
///
|
///
|
||||||
/// Will recursively also format the prefix and suffix of the segment being
|
/// Will recursively also format the prefix and suffix of the segment being
|
||||||
/// stringified.
|
/// stringified.
|
||||||
pub fn output<'a>(&'a self) -> String {
|
pub fn output(&self, index: usize) -> String {
|
||||||
let Segment {
|
let Segment {
|
||||||
name: _name,
|
name: _name,
|
||||||
prefix,
|
prefix,
|
||||||
@ -81,14 +81,17 @@ impl Segment {
|
|||||||
|
|
||||||
let mut segment_string = String::new();
|
let mut segment_string = String::new();
|
||||||
|
|
||||||
if let Some(prefix) = prefix {
|
// Skip the prefix for the first segment
|
||||||
segment_string += &prefix.output()
|
if index != 0 {
|
||||||
|
if let Some(prefix) = prefix {
|
||||||
|
segment_string += &prefix.output(index)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
segment_string += &style.paint(value).to_string();
|
segment_string += &style.paint(value).to_string();
|
||||||
|
|
||||||
if let Some(suffix) = suffix {
|
if let Some(suffix) = suffix {
|
||||||
segment_string += &suffix.output();
|
segment_string += &suffix.output(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
segment_string
|
segment_string
|
||||||
|
Loading…
Reference in New Issue
Block a user