diff options
author | Matan Kushner <hello@matchai.me> | 2019-05-01 16:34:24 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-01 16:34:24 -0400 |
commit | c6ee5c6ac16d360ab1a44d097c91fe9f98f20f85 (patch) | |
tree | e06814175f34e508fbae4de66571e3d0042786e7 /src/modules | |
parent | d945b03093cf130bdc702056650f7e8e8869753b (diff) |
Refactor segments into modules (#40)
Diffstat (limited to 'src/modules')
-rw-r--r-- | src/modules/character.rs | 25 | ||||
-rw-r--r-- | src/modules/directory.rs | 18 | ||||
-rw-r--r-- | src/modules/git_branch.rs | 26 | ||||
-rw-r--r-- | src/modules/line_break.rs | 17 | ||||
-rw-r--r-- | src/modules/mod.rs | 4 | ||||
-rw-r--r-- | src/modules/nodejs.rs | 18 | ||||
-rw-r--r-- | src/modules/package.rs | 21 | ||||
-rw-r--r-- | src/modules/python.rs | 19 | ||||
-rw-r--r-- | src/modules/rust.rs | 19 |
9 files changed, 80 insertions, 87 deletions
diff --git a/src/modules/character.rs b/src/modules/character.rs index f624e19f2..2a9334894 100644 --- a/src/modules/character.rs +++ b/src/modules/character.rs @@ -1,8 +1,6 @@ +use super::{Context, Module}; use ansi_term::Color; -use super::Segment; -use crate::context::Context; - /// Creates a segment for the prompt character /// /// The char segment prints an arrow character in a color dependant on the exit- @@ -11,21 +9,22 @@ use crate::context::Context; /// (green by default) /// - If the exit-code was anything else, the arrow will be formatted with /// `COLOR_FAILURE` (red by default) -pub fn segment(context: &Context) -> Option<Segment> { +pub fn segment(context: &Context) -> Option<Module> { const PROMPT_CHAR: &str = "➜"; - const COLOR_SUCCESS: Color = Color::Green; - const COLOR_FAILURE: Color = Color::Red; + let color_success = Color::Green.bold(); + let color_failure = Color::Red.bold(); - let mut segment = Segment::new("char"); - let arguments = &context.arguments; + let mut module = Module::new("char"); + module.get_prefix().set_value(""); + + let symbol = module.new_segment("symbol", PROMPT_CHAR); + let arguments = &context.arguments; if arguments.value_of("status_code").unwrap() == "0" { - segment.set_style(COLOR_SUCCESS); + symbol.set_style(color_success.bold()); } else { - segment.set_style(COLOR_FAILURE); + symbol.set_style(color_failure.bold()); }; - segment.set_value(PROMPT_CHAR).set_prefix(None); - - Some(segment) + Some(module) } diff --git a/src/modules/directory.rs b/src/modules/directory.rs index d5fbc88c9..a85287a67 100644 --- a/src/modules/directory.rs +++ b/src/modules/directory.rs @@ -1,8 +1,7 @@ use ansi_term::Color; use std::path::Path; -use super::Segment; -use crate::context::Context; +use super::{Context, Module}; /// Creates a segment with the current directory /// @@ -13,12 +12,14 @@ use crate::context::Context; /// /// **Truncation** /// Paths will be limited in length to `3` path components by default. -pub fn segment(context: &Context) -> Option<Segment> { +pub fn segment(context: &Context) -> Option<Module> { const HOME_SYMBOL: &str = "~"; const DIR_TRUNCATION_LENGTH: usize = 3; - const SEGMENT_COLOR: Color = Color::Cyan; + let module_color = Color::Cyan.bold(); + + let mut module = Module::new("directory"); + module.set_style(module_color); - let mut segment = Segment::new("dir"); let current_dir = &context.current_dir; let dir_string; @@ -37,12 +38,9 @@ pub fn segment(context: &Context) -> Option<Segment> { // Truncate the dir string to the maximum number of path components let truncated_dir_string = truncate(dir_string, DIR_TRUNCATION_LENGTH); + module.new_segment("path", truncated_dir_string); - segment - .set_value(truncated_dir_string) - .set_style(SEGMENT_COLOR.bold()); - - Some(segment) + Some(module) } /// Contract the root component of a path diff --git a/src/modules/git_branch.rs b/src/modules/git_branch.rs index 586724b2e..a89e77f3e 100644 --- a/src/modules/git_branch.rs +++ b/src/modules/git_branch.rs @@ -1,13 +1,12 @@ use ansi_term::Color; use git2::Repository; -use super::Segment; -use crate::context::Context; +use super::{Context, Module}; /// Creates a segment with the Git branch in the current directory /// /// Will display the branch name if the current directory is a git repo -pub fn segment(context: &Context) -> Option<Segment> { +pub fn segment(context: &Context) -> Option<Module> { if context.repository.is_none() { return None; } @@ -15,22 +14,17 @@ pub fn segment(context: &Context) -> Option<Segment> { let repository = context.repository.as_ref().unwrap(); match get_current_branch(repository) { Ok(branch_name) => { - const GIT_BRANCH_CHAR: &str = ""; - const SEGMENT_COLOR: Color = Color::Purple; + const GIT_BRANCH_CHAR: &str = " "; + let segment_color = Color::Purple.bold(); - // TODO: Make the prefix for the module "in " - let mut segment_prefix = Segment::new("git_branch_prefix"); - segment_prefix - .set_value(GIT_BRANCH_CHAR) - .set_style(SEGMENT_COLOR.bold()); + let mut module = Module::new("git_branch"); + module.set_style(segment_color); + module.get_prefix().set_value("in "); - let mut segment = Segment::new("git_branch"); - segment - .set_prefix(Some(Box::new(segment_prefix))) - .set_style(SEGMENT_COLOR.bold()) - .set_value(branch_name); + module.new_segment("branch_char", GIT_BRANCH_CHAR); + module.new_segment("branch_name", branch_name); - Some(segment) + Some(module) } Err(_e) => None, } diff --git a/src/modules/line_break.rs b/src/modules/line_break.rs index de0e6f9be..223695dcf 100644 --- a/src/modules/line_break.rs +++ b/src/modules/line_break.rs @@ -1,16 +1,15 @@ -use super::Segment; -use crate::context::Context; +use super::{Context, Module}; /// Creates a segment for the line break -pub fn segment(_context: &Context) -> Option<Segment> { +pub fn segment(_context: &Context) -> Option<Module> { const LINE_ENDING: &str = "\n"; - let mut segment = Segment::new("line_break"); + let mut module = Module::new("line_break"); - segment - .set_value(LINE_ENDING) - .set_prefix(None) - .set_suffix(None); + module.get_prefix().set_value(""); + module.get_suffix().set_value(""); - Some(segment) + module.new_segment("character", LINE_ENDING); + + Some(module) } diff --git a/src/modules/mod.rs b/src/modules/mod.rs index 5c55f6aa9..911486c49 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -8,9 +8,9 @@ mod python; mod rust; use crate::context::Context; -use crate::segment::Segment; +use crate::module::Module; -pub fn handle(module: &str, context: &Context) -> Option<Segment> { +pub fn handle(module: &str, context: &Context) -> Option<Module> { match module { "dir" | "directory" => directory::segment(context), "char" | "character" => character::segment(context), diff --git a/src/modules/nodejs.rs b/src/modules/nodejs.rs index 818a0cf5c..6552c5556 100644 --- a/src/modules/nodejs.rs +++ b/src/modules/nodejs.rs @@ -2,8 +2,7 @@ use ansi_term::Color; use std::path::PathBuf; use std::process::Command; -use super::Segment; -use crate::context::Context; +use super::{Context, Module}; /// Creates a segment with the current Node.js version /// @@ -11,7 +10,7 @@ use crate::context::Context; /// - Current directory contains a `.js` file /// - Current directory contains a `package.json` file /// - Current directory contains a `node_modules` directory -pub fn segment(context: &Context) -> Option<Segment> { +pub fn segment(context: &Context) -> Option<Module> { let is_js_project = context.dir_files.iter().any(has_js_files); if !is_js_project { return None; @@ -19,16 +18,17 @@ pub fn segment(context: &Context) -> Option<Segment> { match get_node_version() { Some(node_version) => { - const NODE_CHAR: &str = "⬢"; - const SEGMENT_COLOR: Color = Color::Green; + const NODE_CHAR: &str = "⬢ "; + let module_color = Color::Green.bold(); - let mut segment = Segment::new("node"); - segment.set_style(SEGMENT_COLOR.bold()); + let mut module = Module::new("node"); + module.set_style(module_color); let formatted_version = node_version.trim(); - segment.set_value(format!("{} {}", NODE_CHAR, formatted_version)); + module.new_segment("symbol", NODE_CHAR); + module.new_segment("version", formatted_version); - Some(segment) + Some(module) } None => None, } diff --git a/src/modules/package.rs b/src/modules/package.rs index ab201e4bc..8e7d4d3af 100644 --- a/src/modules/package.rs +++ b/src/modules/package.rs @@ -1,5 +1,5 @@ -use super::Segment; -use crate::context::Context; +use super::{Context, Module}; + use ansi_term::Color; use serde_json; use std::fs::File; @@ -11,19 +11,20 @@ use toml; /// Creates a segment with the current package version /// /// Will display if a version is defined for your Node.js or Rust project (if one exists) -pub fn segment(context: &Context) -> Option<Segment> { +pub fn segment(context: &Context) -> Option<Module> { match get_package_version(context) { Some(package_version) => { - const PACKAGE_CHAR: &str = "📦"; - const SEGMENT_COLOR: Color = Color::Red; + const PACKAGE_CHAR: &str = "📦 "; + let module_color = Color::Red.bold(); - // TODO: Make the prefix for the module "is " - let mut segment = Segment::new("package"); - segment.set_style(SEGMENT_COLOR.bold()); + let mut module = Module::new("package"); + module.set_style(module_color); + module.get_prefix().set_value("is "); - segment.set_value(format!("{} {}", PACKAGE_CHAR, package_version)); + module.new_segment("symbol", PACKAGE_CHAR); + module.new_segment("version", package_version); - Some(segment) + Some(module) } None => None, } diff --git a/src/modules/python.rs b/src/modules/python.rs index afd62690a..70625a47c 100644 --- a/src/modules/python.rs +++ b/src/modules/python.rs @@ -1,9 +1,9 @@ -use super::Segment; -use crate::context::Context; use ansi_term::Color; use std::path::PathBuf; use std::process::Command; +use super::{Context, Module}; + /// Creates a segment with the current Python version /// /// Will display the Python version if any of the following criteria are met: @@ -11,7 +11,7 @@ use std::process::Command; /// - Current directory contains a `.python-version` file /// - Current directory contains a `requirements.txt` file /// - Current directory contains a `pyproject.toml` file -pub fn segment(context: &Context) -> Option<Segment> { +pub fn segment(context: &Context) -> Option<Module> { let is_py_project = context.dir_files.iter().any(has_py_files); if !is_py_project { return None; @@ -19,16 +19,17 @@ pub fn segment(context: &Context) -> Option<Segment> { match get_python_version() { Some(python_version) => { - const PYTHON_CHAR: &str = "🐍"; - const SEGMENT_COLOR: Color = Color::Yellow; + const PYTHON_CHAR: &str = "🐍 "; + let module_color = Color::Yellow.bold(); - let mut segment = Segment::new("python"); - segment.set_style(SEGMENT_COLOR.bold()); + let mut module = Module::new("python"); + module.set_style(module_color); let formatted_version = format_python_version(python_version); - segment.set_value(format!("{} {}", PYTHON_CHAR, formatted_version)); + module.new_segment("symbol", PYTHON_CHAR); + module.new_segment("version", formatted_version); - Some(segment) + Some(module) } None => None, } diff --git a/src/modules/rust.rs b/src/modules/rust.rs index 14af3b854..1f0ae6036 100644 --- a/src/modules/rust.rs +++ b/src/modules/rust.rs @@ -1,15 +1,15 @@ -use super::Segment; -use crate::context::Context; use ansi_term::Color; use std::path::PathBuf; use std::process::Command; +use super::{Context, Module}; + /// Creates a segment with the current Rust version /// /// Will display the Rust version if any of the following criteria are met: /// - Current directory contains a `.rs` file /// - Current directory contains a `Cargo.toml` file -pub fn segment(context: &Context) -> Option<Segment> { +pub fn segment(context: &Context) -> Option<Module> { let is_rs_project = context.dir_files.iter().any(has_rs_files); if !is_rs_project { return None; @@ -17,16 +17,17 @@ pub fn segment(context: &Context) -> Option<Segment> { match get_rust_version() { Some(rust_version) => { - const RUST_CHAR: &str = "🦀"; - const SEGMENT_COLOR: Color = Color::Red; + const RUST_CHAR: &str = "🦀 "; + let module_color = Color::Red.bold(); - let mut segment = Segment::new("rust"); - segment.set_style(SEGMENT_COLOR.bold()); + let mut module = Module::new("rust"); + module.set_style(module_color); let formatted_version = format_rustc_version(rust_version); - segment.set_value(format!("{} {}", RUST_CHAR, formatted_version)); + module.new_segment("symbol", RUST_CHAR); + module.new_segment("version", formatted_version); - Some(segment) + Some(module) } None => None, } |