Add builder pattern for segment

This commit is contained in:
Matan Kushner 2019-04-12 17:49:20 -04:00
parent 7356faaec2
commit d82ebc4457
No known key found for this signature in database
GPG Key ID: 4B98C3A8949CA8A4
7 changed files with 91 additions and 29 deletions

View File

@ -1,3 +1,4 @@
// Lib is present to allow for benchmarking
pub mod modules;
pub mod print;
pub mod segment;

View File

@ -7,6 +7,7 @@ extern crate git2;
mod modules;
mod print;
mod segment;
use clap::{App, Arg};

View File

@ -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
};

View File

@ -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),

View File

@ -6,7 +6,7 @@ use std::fs::{self, DirEntry};
use std::process::Command;
/// Creates a segment with the current Node.js version
///
///
/// 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

View File

@ -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
View 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>>;