From d58ea0659b3f0c4a62c1165a597d72757b99d088 Mon Sep 17 00:00:00 2001 From: Matan Kushner Date: Mon, 8 Apr 2019 17:35:38 -0400 Subject: [PATCH] Add a new line before the prompt --- src/modules/directory.rs | 13 +++++++++---- src/print.rs | 12 ++++++++++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/modules/directory.rs b/src/modules/directory.rs index a028506f..9b5b8195 100644 --- a/src/modules/directory.rs +++ b/src/modules/directory.rs @@ -66,6 +66,11 @@ fn get_repo_root(repo: Repository) -> PathBuf { } /// Truncate a path to a predefined number of path components +/// +/// Trim the path in the prompt to only have the last few paths, set by `length`. +/// This function also serves to replace the top-level path of the prompt. +/// This can be used to replace the path to a git repo with only the repo +/// directory name. fn truncate_path( length: &usize, full_path: &PathBuf, @@ -91,10 +96,10 @@ fn truncate_path( } format!( - "{}{}{}", - top_level_replacement, - std::path::MAIN_SEPARATOR, - full_path + "{replacement}{separator}{path}", + replacement = top_level_replacement, + separator = std::path::MAIN_SEPARATOR, + path = full_path .iter() .skip(top_level_path_depth) .collect::() diff --git a/src/print.rs b/src/print.rs index baf3ebe0..55e05ed8 100644 --- a/src/print.rs +++ b/src/print.rs @@ -1,15 +1,23 @@ +use std::io::{self, Write}; +use clap::ArgMatches; + use crate::modules; use crate::modules::Segment; -use clap::ArgMatches; pub fn prompt(args: ArgMatches) { let default_prompt = vec!["directory", "line_break", "character"]; + let stdout = io::stdout(); + let mut handle = stdout.lock(); + + // Write a new line before the prompt + write!(handle, "{}", "\n").unwrap(); + default_prompt .into_iter() .map(|module| modules::handle(module, &args)) .map(stringify_segment) - .for_each(|segment_string| print!("{}", segment_string)); + .for_each(|segment_string| write!(handle, "{}", segment_string).unwrap()); } /// Create a string with the formatted contents of a segment