From a81eabd690f171cda1738b37de3266993712c714 Mon Sep 17 00:00:00 2001 From: Matan Kushner Date: Thu, 4 Apr 2019 12:18:02 -0400 Subject: [PATCH] Move Segment struct to modules --- src/main.rs | 8 -------- src/modules/char.rs | 13 ++++++++++--- src/modules/mod.rs | 20 +++++++++++++++++++- src/print.rs | 2 +- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7cbe7d22..f1de02bb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,16 +5,8 @@ extern crate ansi_term; mod modules; mod print; -use ansi_term::Style; use clap::{App, Arg}; -pub struct Segment { - style: Style, - value: String, - prefix: Option>, - suffix: Option>, -} - fn main() { let args = App::new("Starship") .about("The cross-platform prompt for astronauts. ✨🚀") diff --git a/src/modules/char.rs b/src/modules/char.rs index b5d6183a..93d8df69 100644 --- a/src/modules/char.rs +++ b/src/modules/char.rs @@ -1,7 +1,15 @@ -use crate::Segment; +use super::Segment; use ansi_term::{Color, Style}; use clap::ArgMatches; +/// Prints the prompt character +/// +/// The char segment prints an arrow character in a color dependant on the exit- +/// code of the last executed command: +/// - If the exit-code was "0", the arrow will be formatted with `COLOR_SUCCESS` +/// (green by default) +/// - If the exit-code was anything else, the arrow will be formatted with +/// `COLOR_FAILURE` (red by default) pub fn segment(args: &ArgMatches) -> Segment { const PROMPT_CHAR: &str = "➜ "; const COLOR_SUCCESS: Color = Color::Green; @@ -15,10 +23,9 @@ pub fn segment(args: &ArgMatches) -> Segment { } Segment { - prefix: None, value: String::from(PROMPT_CHAR), style: Style::from(color), - suffix: None, + ..Default::default() } } diff --git a/src/modules/mod.rs b/src/modules/mod.rs index 52474cbc..869a82c2 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -1,7 +1,25 @@ mod char; -use crate::Segment; use clap::ArgMatches; +use ansi_term::Style; + +pub struct Segment { + pub style: Style, + pub value: String, + pub prefix: Option>, + pub suffix: Option>, +} + +impl Default for Segment { + fn default() -> Segment { + Segment { + style: Style::default(), + value: String::from(""), + prefix: None, + suffix: None + } + } +} pub fn handle(module: &str, args: &ArgMatches) -> Segment { match module { diff --git a/src/print.rs b/src/print.rs index 1fc6e535..a74655aa 100644 --- a/src/print.rs +++ b/src/print.rs @@ -1,5 +1,5 @@ use crate::modules; -use crate::Segment; +use crate::modules::Segment; use clap::ArgMatches; pub fn prompt(args: ArgMatches) {