feat: alternative initialization

This commit is contained in:
iff 2023-08-04 15:40:45 +02:00
parent e56ac04078
commit 683fdad837
3 changed files with 93 additions and 8 deletions

View file

@ -23,6 +23,12 @@ alias f="$(pay_respects <your_shell_here>)"
# for example, using `zsh`:
alias f="$(pay_respects zsh)"
# Alternatively, you can also use the following initialization in your config file
# for bash and zsh
eval "$(pay_respects <shell> --alias)"
# for fish
pay_respects fish --alias | source
# for `nushell`, the alias can be added automatically with:
pay_respects nushell
```

View file

@ -1,11 +1,66 @@
use crate::shell::print_command_with_env;
use crate::shell::initialization;
pub fn handle_args() {
let args = std::env::args().collect::<Vec<String>>();
if args.len() > 1 {
let shell = &args[1];
let binary_path = &args[0];
print_command_with_env(shell, binary_path);
if args.len() <= 1 {
return;
}
let mut auto_aliasing = String::new();
let mut shell = String::new();
let mut index = 0;
while index < args.len() {
match args[index].as_str() {
"-h" | "--help" => {
print_help();
}
"-a" | "--alias" => {
if args.len() > index + 1 {
if args[index + 1].starts_with('-') {
auto_aliasing = String::from("f");
} else {
auto_aliasing = args[index + 1].clone();
index += 1;
}
} else {
auto_aliasing = String::from("f");
}
index += 1;
}
_ => {
shell = args[index].clone();
index += 1
}
}
}
if shell.is_empty() {
eprintln!("No shell specified. Please specify a shell.");
std::process::exit(1);
}
let binary_path = &args[0];
initialization(&shell, binary_path, &auto_aliasing);
}
fn print_help() {
let help_message = String::from(
"
Usage: pay_respects [your shell] [--alias [alias]]
Example 1, manual aliasing: `pay_respects bash`
The command will output the command that you can use to execute the binary with
the correct environment. You can alias such output to a shorter key. Such as
alias f=$(pay_respects bash)
Example 2, auto aliasing: `pay_respects bash --alias f`
The command will output a declaration that can be directly embedded in your
config file with `eval $(pay_respects bash --alias)`. For fish, use
`pay_respects fish --alias | source` instead.
",
);
println!("{}", help_message);
std::process::exit(0);
}

View file

@ -94,7 +94,7 @@ pub fn last_command_expanded_alias(shell: &str) -> String {
last_command.replacen(command, &expanded_command, 1)
}
pub fn print_command_with_env(shell: &str, binary_path: &str) {
pub fn initialization(shell: &str, binary_path: &str, auto_alias: &str) {
let last_command;
let alias;
@ -153,7 +153,7 @@ pub fn print_command_with_env(shell: &str, binary_path: &str) {
}
}
println!(
let mut init = format!(
"\
_PR_LAST_COMMAND=\"{}\" \
_PR_ALIAS=\"{}\" \
@ -161,5 +161,29 @@ pub fn print_command_with_env(shell: &str, binary_path: &str) {
\"{}\"",
last_command, alias, shell, binary_path
);
if auto_alias.is_empty() {
println!("{}", init);
std::process::exit(0);
}
match shell {
"bash" | "zsh" => {
init = format!(r#"alias {}='{}'"#, auto_alias, init);
}
"fish" => {
init = format!(
r#"function {} -d "Terminal command correction"; {}; end"#,
auto_alias, init
);
}
_ => {
println!("Unsupported shell: {}", shell);
exit(1);
}
}
println!("{}", init);
std::process::exit(0);
}