Browse Source

feat: add bash-like prompt style

main
fdai7451 2 years ago
parent
commit
362d3ea667
  1. 26
      Cargo.lock
  2. 1
      Cargo.toml
  3. 3
      src/builtins/change_prompt.rs
  4. 22
      src/prompt.rs

26
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"

1
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"

3
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",

22
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 {

Loading…
Cancel
Save