From 362d3ea66721ff0cdd6224de5c89cd6c55db6989 Mon Sep 17 00:00:00 2001 From: fdai7451 Date: Sat, 21 Jan 2023 17:30:07 +0100 Subject: [PATCH] feat: add bash-like prompt style --- Cargo.lock | 26 ++++++++++++++++++++++++++ Cargo.toml | 1 + src/builtins/change_prompt.rs | 3 +++ src/prompt.rs | 22 ++++++++++++++++++++++ 4 files changed, 52 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 08a8f0c..d5edb92 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -149,6 +149,16 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "gethostname" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a329e22866dd78b35d2c639a4a23d7b950aeae300dfd79f4fb19f74055c2404" +dependencies = [ + "libc", + "windows", +] + [[package]] name = "getrandom" version = "0.2.8" @@ -231,6 +241,7 @@ dependencies = [ "crossbeam-channel", "ctrlc", "dirs", + "gethostname", "once_cell", "rustyline", "shlex", @@ -434,6 +445,21 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows" +version = "0.43.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04662ed0e3e5630dfa9b26e4cb823b817f1a9addda855d973a9458c236556244" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + [[package]] name = "windows-sys" version = "0.42.0" diff --git a/Cargo.toml b/Cargo.toml index 11a2d1d..14fb220 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" crossbeam-channel = "0.5" ctrlc = "3.2" dirs = "4.0" +gethostname = "0.4" once_cell = "1.17" rustyline = "10.1.0" shlex = "1.1" diff --git a/src/builtins/change_prompt.rs b/src/builtins/change_prompt.rs index 4eae628..89c204f 100644 --- a/src/builtins/change_prompt.rs +++ b/src/builtins/change_prompt.rs @@ -16,6 +16,9 @@ impl Builtin for ChangePrompt { "simple-directory" | "simple_directory" | "simpledirectory" => { config.prompt_style = PromptStyle::SimpleDirectory } + "bash-like" | "bash_like" | "bashlike" => { + config.prompt_style = PromptStyle::BashLike + } _ => { return Err(ShellError::ExecuteFailure(format!( "{} is not a valid prompt style", diff --git a/src/prompt.rs b/src/prompt.rs index de2120e..7221708 100644 --- a/src/prompt.rs +++ b/src/prompt.rs @@ -4,6 +4,7 @@ use std::env; pub enum PromptStyle { None, SimpleDirectory, + BashLike, } impl PromptStyle { @@ -11,6 +12,7 @@ impl PromptStyle { match self { PromptStyle::None => self.fmt_none(), PromptStyle::SimpleDirectory => self.fmt_simple_directory(), + PromptStyle::BashLike => self.fmt_bash_like(), } } @@ -23,6 +25,26 @@ impl PromptStyle { return format!("{} ยป ", path.to_string_lossy()); } + + fn fmt_bash_like(&self) -> String { + let path = env::current_dir().unwrap_or_default(); + let home = dirs::home_dir().unwrap_or_default(); + + let dir_as_string = if path == home { + "~" + } else { + path.file_name() + .unwrap_or_default() + .to_str() + .unwrap_or_default() + }; + + let username = home.file_name().unwrap_or_default().to_string_lossy(); + let _hostname = gethostname::gethostname(); + let hostname = _hostname.to_string_lossy(); + + return format!("[{}@{} {}]$ ", username, hostname, dir_as_string); + } } pub struct Prompt {