Skip to content

Commit

Permalink
Add builder pattern for segment
Browse files Browse the repository at this point in the history
  • Loading branch information
Matan Kushner committed Apr 12, 2019
1 parent 7356faa commit d82ebc4
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 29 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
@@ -1,3 +1,4 @@
// Lib is present to allow for benchmarking
pub mod modules;
pub mod print;
pub mod segment;
1 change: 1 addition & 0 deletions src/main.rs
Expand Up @@ -7,6 +7,7 @@ extern crate git2;

mod modules;
mod print;
mod segment;

use clap::{App, Arg};

Expand Down
4 changes: 3 additions & 1 deletion src/modules/character.rs
Expand Up @@ -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
};
Expand Down
27 changes: 1 addition & 26 deletions src/modules/mod.rs
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion src/modules/nodejs.rs
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/print.rs
Expand Up @@ -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
Expand Down
83 changes: 83 additions & 0 deletions src/segment.rs
@@ -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>>;

0 comments on commit d82ebc4

Please sign in to comment.