From 9f673196786491689c8ce2515a44e66a01679aec Mon Sep 17 00:00:00 2001 From: fdai7381 Date: Mon, 23 Jan 2023 01:56:51 +0100 Subject: [PATCH] feat: update default prompt style --- src/main.rs | 4 ++-- src/prompt.rs | 27 ++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 73e605a..87e73b1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use crate::builtins::BuiltinConfig; use crate::prompt::Prompt; -use colored::*; +use colored::Colorize; use execute::interpret; use rustyline::config::Configurer; use rustyline::error::ReadlineError; @@ -26,7 +26,7 @@ fn main() -> Result<()> { let mut prompt = Prompt::new(); let mut config = BuiltinConfig::new(); - println!("Welcome, {}.", username().bright_green().bold()); + println!("\nWelcome, {}.", username().bright_green().bold()); loop { let readline = rl.readline(&prompt.get_prompt()); diff --git a/src/prompt.rs b/src/prompt.rs index d6fcb64..e1fc667 100644 --- a/src/prompt.rs +++ b/src/prompt.rs @@ -1,3 +1,4 @@ +use colored::Colorize; use std::env; #[derive(Clone)] @@ -17,7 +18,31 @@ impl PromptStyle { } fn fmt_default(&self) -> String { - "» ".to_string() + let current = env::current_dir().unwrap_or_default(); + let home = dirs::home_dir().unwrap_or_default(); + + let dir = if current.starts_with(home.clone()) { + let mut path = "~/".to_string(); + + path.push_str( + current + .as_path() + .strip_prefix(home) + .unwrap() + .to_str() + .unwrap_or_default(), + ); + + path + } else { + current.into_os_string().into_string().unwrap_or_default() + }; + + format!( + "\n{}\n{} ", + dir.bright_cyan().bold(), + "»".bright_green().bold() + ) } fn fmt_simple_directory(&self) -> String {