mirror of
https://github.com/Llewellynvdm/starship.git
synced 2024-11-28 07:46:28 +00:00
Add builder pattern for segment
This commit is contained in:
parent
7356faaec2
commit
d82ebc4457
@ -1,3 +1,4 @@
|
||||
// Lib is present to allow for benchmarking
|
||||
pub mod modules;
|
||||
pub mod print;
|
||||
pub mod segment;
|
||||
|
@ -7,6 +7,7 @@ extern crate git2;
|
||||
|
||||
mod modules;
|
||||
mod print;
|
||||
mod segment;
|
||||
|
||||
use clap::{App, Arg};
|
||||
|
||||
|
@ -15,8 +15,10 @@ pub fn segment(args: &ArgMatches) -> Segment {
|
||||
const COLOR_SUCCESS: Color = Color::Green;
|
||||
const COLOR_FAILURE: Color = Color::Red;
|
||||
|
||||
let segment = Segment::new("char");
|
||||
|
||||
let color = if args.value_of("status_code").unwrap() == "0" {
|
||||
COLOR_SUCCESS
|
||||
segment.set_style(COLOR_SUCCESS);
|
||||
} else {
|
||||
COLOR_FAILURE
|
||||
};
|
||||
|
@ -3,37 +3,12 @@ mod directory;
|
||||
mod line_break;
|
||||
mod nodejs;
|
||||
|
||||
use ansi_term::Style;
|
||||
use crate::segment::Segment;
|
||||
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,
|
||||
pub prefix: Option<Box<Segment>>,
|
||||
pub suffix: Option<Box<Segment>>,
|
||||
}
|
||||
|
||||
impl Default for Segment {
|
||||
fn default() -> Segment {
|
||||
let default_suffix = Some(Box::new(Segment {
|
||||
style: Style::default(),
|
||||
value: String::from(" "),
|
||||
prefix: None,
|
||||
suffix: None,
|
||||
}));
|
||||
|
||||
Segment {
|
||||
style: Style::default(),
|
||||
value: String::from(""),
|
||||
prefix: None,
|
||||
suffix: default_suffix,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle(module: &str, args: &ArgMatches) -> Segment {
|
||||
match module {
|
||||
"dir" | "directory" => directory::segment(&args),
|
||||
|
@ -5,7 +5,7 @@ use crate::modules;
|
||||
use crate::modules::Segment;
|
||||
|
||||
pub fn prompt(args: ArgMatches) {
|
||||
let default_prompt = vec!["directory", "node", "line_break", "character"];
|
||||
let default_prompt = vec!["directory", "nodejs", "line_break", "character"];
|
||||
|
||||
// TODO:
|
||||
// - List files in directory
|
||||
|
83
src/segment.rs
Normal file
83
src/segment.rs
Normal file
@ -0,0 +1,83 @@
|
||||
use ansi_term::Style;
|
||||
|
||||
pub struct Segment {
|
||||
name: Option<String>,
|
||||
style: Style,
|
||||
value: String,
|
||||
prefix: OptionalSegment,
|
||||
suffix: OptionalSegment,
|
||||
}
|
||||
|
||||
impl Segment {
|
||||
pub fn new<S>(name: S) -> Segment where S: Into<String> {
|
||||
let default_prefix = Some(Box::new(Segment {
|
||||
name: Some(format!("{} {}", name.into(), "prefix")),
|
||||
style: Style::default(),
|
||||
value: String::from("via "),
|
||||
prefix: None,
|
||||
suffix: None,
|
||||
}));
|
||||
|
||||
let default_suffix = Some(Box::new(Segment {
|
||||
name: Some(format!("{} {}", name.into(), "suffix")),
|
||||
style: Style::default(),
|
||||
value: String::from(" "),
|
||||
prefix: None,
|
||||
suffix: None,
|
||||
}));
|
||||
|
||||
Segment {
|
||||
name: Some(name.into()),
|
||||
style: Style::default(),
|
||||
value: String::from(""),
|
||||
prefix: default_prefix,
|
||||
suffix: default_suffix,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_style<'a>(&'a mut self, style: Style) -> &'a mut Segment {
|
||||
self.style = style;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn set_value<'a>(&'a mut self, value: String) -> &'a mut Segment {
|
||||
self.value = value;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn set_prefix<'a>(&'a mut self, prefix: Segment) -> &'a mut Segment {
|
||||
self.prefix = Some(Box::new(prefix));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn set_suffix<'a>(&'a mut self, suffix: Segment) -> &'a mut Segment {
|
||||
self.suffix = Some(Box::new(suffix));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn output<'a>(&'a self) -> String {
|
||||
let Segment {
|
||||
name: _name,
|
||||
prefix,
|
||||
value,
|
||||
style,
|
||||
suffix,
|
||||
} = self;
|
||||
|
||||
let mut segment_string = String::new();
|
||||
|
||||
if let Some(prefix) = prefix {
|
||||
segment_string += &prefix.output()
|
||||
}
|
||||
|
||||
segment_string += &style.paint(value).to_string();
|
||||
|
||||
if let Some(suffix) = suffix {
|
||||
segment_string += &suffix.output();
|
||||
}
|
||||
|
||||
segment_string
|
||||
}
|
||||
}
|
||||
|
||||
type OptionalSegment = Option<Box<Segment>>;
|
Loading…
Reference in New Issue
Block a user