Add stringify_segment rustdoc

This commit is contained in:
Matan Kushner 2019-04-04 21:35:24 -04:00
parent 472b66894d
commit c79cbe63b1
No known key found for this signature in database
GPG Key ID: 4B98C3A8949CA8A4
5 changed files with 33 additions and 15 deletions

View File

@ -4,13 +4,13 @@ extern crate test;
#[cfg(test)]
mod tests {
use clap::{App, Arg};
use starship::{modules, print};
use test::Bencher;
use clap::{App, Arg};
#[bench]
fn full_prompt_bench(b: &mut Bencher) {
b.iter(||{
b.iter(|| {
let args = App::new("starship")
.arg(Arg::with_name("status_code"))
.get_matches_from(vec!["starship", "0"]);
@ -25,7 +25,7 @@ mod tests {
let args = App::new("starship")
.arg(Arg::with_name("status_code"))
.get_matches_from(vec!["starship", "0"]);
let segment = modules::handle("char", &args);
print::stringify_segment(segment)
});
@ -37,7 +37,7 @@ mod tests {
let args = App::new("starship")
.arg(Arg::with_name("status_code"))
.get_matches_from(vec!["starship", "0"]);
let segment = modules::handle("dir", &args);
print::stringify_segment(segment)
});

View File

@ -3,7 +3,7 @@ use ansi_term::{Color, Style};
use clap::ArgMatches;
/// Creates a segment for 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`

View File

@ -1,8 +1,8 @@
use super::Segment;
use dirs;
use std::env;
use ansi_term::{Color, Style};
use clap::ArgMatches;
use dirs;
use std::env;
/// Creates a segment with the current directory
pub fn segment(_: &ArgMatches) -> Segment {

View File

@ -1,9 +1,9 @@
mod char;
mod character;
mod directory;
mod line_break;
use clap::ArgMatches;
use ansi_term::Style;
use clap::ArgMatches;
pub struct Segment {
pub style: Style,
@ -18,21 +18,21 @@ impl Default for Segment {
style: Style::default(),
value: String::from(" "),
prefix: None,
suffix: None
suffix: None,
}));
Segment {
style: Style::default(),
value: String::from(""),
prefix: None,
suffix: default_suffix
suffix: default_suffix,
}
}
}
pub fn handle(module: &str, args: &ArgMatches) -> Segment {
match module {
"char" | "character" => char::segment(&args),
"char" | "character" => character::segment(&args),
"dir" | "directory" => directory::segment(&args),
"line_break" => line_break::segment(&args),

View File

@ -5,26 +5,44 @@ use clap::ArgMatches;
pub fn prompt(args: ArgMatches) {
let default_prompt = vec!["directory", "line_break", "character"];
default_prompt.into_iter()
default_prompt
.into_iter()
.map(|module| modules::handle(module, &args))
.map(|segment| stringify_segment(segment))
.for_each(|segment_string| print!("{}", segment_string));
}
/// Create a string with the formatted contents of a segment
///
/// Will recursively also format the prefix and suffix of the segment being
/// stringified.
///
/// # Example
/// ```
/// use starship::modules::Segment;
///
/// let segment = Segment {
/// value: String::from("->"),
/// ..Default::default()
/// };
///
/// let result = starship::print::stringify_segment(segment);
/// assert_eq!(result, "-> ");
/// ```
pub fn stringify_segment(segment: Segment) -> String {
let Segment {
prefix,
value,
style,
suffix,
} = segment;
} = segment;
let mut segment_string = String::new();
if let Some(prefix) = prefix {
segment_string += &stringify_segment(*prefix);
}
segment_string += &style.paint(value).to_string();
if let Some(suffix) = suffix {