pay-respects/src/args.rs

77 lines
1.8 KiB
Rust
Raw Normal View History

2023-07-31 21:12:57 +02:00
use std::io::prelude::*;
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];
let last_command;
let alias;
match shell.as_str() {
"bash" => {
last_command = "$(history 2)";
alias = "$(alias)"
}
"zsh" => {
last_command = "$(fc -ln -1)";
alias = "$(alias)"
}
"fish" => {
last_command = "$(history | head -n 1)";
alias = "$(alias)";
}
2023-07-31 18:46:35 +02:00
"nu" | "nush" | "nushell" => {
2023-07-31 15:06:30 +02:00
last_command = "(history | last).command";
alias = "\"\"";
2023-07-31 21:12:57 +02:00
let command = format!(
2023-07-31 18:46:35 +02:00
"with-env {{ _PR_LAST_COMMAND : {},\
2023-07-31 15:06:30 +02:00
_PR_ALIAS : {},\
2023-07-31 18:46:35 +02:00
_PR_SHELL : nu }} \
{{ {} }}",
last_command, alias, binary_path
);
2023-07-31 21:12:57 +02:00
println!("{}\n", command);
println!("Add following to your config file? (Y/n)");
let alias = format!("alias f = {}", command);
println!("{}", alias);
let mut input = String::new();
std::io::stdin().read_line(&mut input).unwrap();
match input.trim() {
"Y" | "y" | "" => {
let output = std::process::Command::new("nu")
.arg("-c")
.arg("echo $nu.config-path")
.output()
.expect("Failed to execute process");
2023-07-31 23:48:14 +02:00
let config_path = String::from_utf8_lossy(&output.stdout);
2023-07-31 21:12:57 +02:00
let mut file = std::fs::OpenOptions::new()
.write(true)
.append(true)
.open(config_path.trim())
.expect("Failed to open config file");
writeln!(file, "{}", alias).expect("Failed to write to config file");
2023-07-31 23:48:14 +02:00
}
_ => std::process::exit(0),
2023-07-31 21:12:57 +02:00
};
2023-07-31 15:06:30 +02:00
std::process::exit(0);
}
_ => {
println!("Unknown shell: {}", shell);
std::process::exit(1);
}
}
println!(
"\
_PR_LAST_COMMAND=\"{}\" \
_PR_ALIAS=\"{}\" \
_PR_SHELL=\"{}\" \
\"{}\"",
last_command, alias, shell, binary_path
);
std::process::exit(0);
}
}