From c6ee5c6ac16d360ab1a44d097c91fe9f98f20f85 Mon Sep 17 00:00:00 2001 From: Matan Kushner Date: Wed, 1 May 2019 16:34:24 -0400 Subject: [PATCH] Refactor segments into modules (#40) --- src/lib.rs | 1 + src/main.rs | 1 + src/module.rs | 159 +++++++++++++++++++++++++++++++ src/modules/character.rs | 25 +++-- src/modules/directory.rs | 18 ++-- src/modules/git_branch.rs | 26 ++--- src/modules/line_break.rs | 17 ++-- src/modules/mod.rs | 4 +- src/modules/nodejs.rs | 18 ++-- src/modules/package.rs | 21 +++-- src/modules/python.rs | 19 ++-- src/modules/rust.rs | 19 ++-- src/print.rs | 22 +++-- src/segment.rs | 193 ++++++++++++++++++-------------------- tests/character.rs | 21 ++--- tests/common.rs | 10 +- tests/directory.rs | 91 +++++++++--------- tests/nodejs.rs | 36 ++++--- 18 files changed, 423 insertions(+), 278 deletions(-) create mode 100644 src/module.rs diff --git a/src/lib.rs b/src/lib.rs index 3028646c..078fcd02 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ // Lib is present to allow for benchmarking pub mod context; +pub mod module; pub mod modules; pub mod print; pub mod segment; diff --git a/src/main.rs b/src/main.rs index 233f7217..d5422a12 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ extern crate dirs; extern crate git2; mod context; +mod module; mod modules; mod print; mod segment; diff --git a/src/module.rs b/src/module.rs new file mode 100644 index 00000000..903d5cf5 --- /dev/null +++ b/src/module.rs @@ -0,0 +1,159 @@ +use crate::segment::Segment; +use ansi_term::Style; +use ansi_term::{ANSIString, ANSIStrings}; +use std::fmt; +use std::string::ToString; + +/// A module is a collection of segments showing data for a single integration +/// (e.g. The git module shows the current git branch and status) +pub struct Module { + /// The module's name, to be used in configuration and logging. + name: String, + + /// The styling to be inherited by all segments contained within this module. + style: Style, + + /// The prefix used to separate the current module from the previous one. + prefix: ModuleAffix, + + /// The collection of segments that compose this module. + segments: Vec, + + /// The suffix used to separate the current module from the next one. + suffix: ModuleAffix, +} + +impl Module { + /// Creates a module with no segments. + pub fn new(name: &str) -> Module { + Module { + name: name.to_string(), + style: Style::default(), + prefix: ModuleAffix::default_prefix(name.to_string()), + segments: Vec::new(), + suffix: ModuleAffix::default_suffix(name.to_string()), + } + } + + /// Get a reference to a newly created segment in the module + pub fn new_segment(&mut self, name: &str, value: T) -> &mut Segment + where + T: Into, + { + let mut segment = Segment::new(name); + segment.set_style(self.style); + segment.set_value(value.into()); + self.segments.push(segment); + + self.segments.last_mut().unwrap() + } + + /// Get the module's prefix + pub fn get_prefix(&mut self) -> &mut ModuleAffix { + &mut self.prefix + } + + /// Get the module's suffix + pub fn get_suffix(&mut self) -> &mut ModuleAffix { + &mut self.suffix + } + + /// Sets the style of the segment. + /// + /// Accepts either `Color` or `Style`. + pub fn set_style(&mut self, style: T) -> &mut Module + where + T: Into