mirror of
https://github.com/TECHNOFAB11/pay-respects.git
synced 2025-12-12 22:40:09 +01:00
format: move shell initialization to shell.rs
This commit is contained in:
parent
a9cea6edf1
commit
511b309d99
2 changed files with 73 additions and 67 deletions
69
src/args.rs
69
src/args.rs
|
|
@ -1,76 +1,11 @@
|
||||||
use std::io::prelude::*;
|
use crate::shell::print_command_with_env;
|
||||||
|
|
||||||
pub fn handle_args() {
|
pub fn handle_args() {
|
||||||
let args = std::env::args().collect::<Vec<String>>();
|
let args = std::env::args().collect::<Vec<String>>();
|
||||||
if args.len() > 1 {
|
if args.len() > 1 {
|
||||||
let shell = &args[1];
|
let shell = &args[1];
|
||||||
let binary_path = &args[0];
|
let binary_path = &args[0];
|
||||||
let last_command;
|
|
||||||
let alias;
|
|
||||||
|
|
||||||
match shell.as_str() {
|
print_command_with_env(shell, binary_path);
|
||||||
"bash" => {
|
|
||||||
last_command = "$(history 2)";
|
|
||||||
alias = "$(alias)"
|
|
||||||
}
|
|
||||||
"zsh" => {
|
|
||||||
last_command = "$(fc -ln -1)";
|
|
||||||
alias = "$(alias)"
|
|
||||||
}
|
|
||||||
"fish" => {
|
|
||||||
last_command = "$(history | head -n 1)";
|
|
||||||
alias = "$(alias)";
|
|
||||||
}
|
|
||||||
"nu" | "nush" | "nushell" => {
|
|
||||||
last_command = "(history | last).command";
|
|
||||||
alias = "\"\"";
|
|
||||||
let command = format!(
|
|
||||||
"with-env {{ _PR_LAST_COMMAND : {},\
|
|
||||||
_PR_ALIAS : {},\
|
|
||||||
_PR_SHELL : nu }} \
|
|
||||||
{{ {} }}",
|
|
||||||
last_command, alias, binary_path
|
|
||||||
);
|
|
||||||
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");
|
|
||||||
let config_path = String::from_utf8_lossy(&output.stdout);
|
|
||||||
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");
|
|
||||||
}
|
|
||||||
_ => std::process::exit(0),
|
|
||||||
};
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
71
src/shell.rs
71
src/shell.rs
|
|
@ -1,4 +1,5 @@
|
||||||
use std::process::exit;
|
use std::process::exit;
|
||||||
|
use std::io::prelude::*;
|
||||||
|
|
||||||
pub const PRIVILEGE_LIST: [&str; 2] = ["sudo", "doas"];
|
pub const PRIVILEGE_LIST: [&str; 2] = ["sudo", "doas"];
|
||||||
|
|
||||||
|
|
@ -92,3 +93,73 @@ pub fn last_command_expanded_alias(shell: &str) -> String {
|
||||||
|
|
||||||
last_command.replacen(command, &expanded_command, 1)
|
last_command.replacen(command, &expanded_command, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn print_command_with_env(shell: &str, binary_path: &str) {
|
||||||
|
let last_command;
|
||||||
|
let alias;
|
||||||
|
|
||||||
|
match shell {
|
||||||
|
"bash" => {
|
||||||
|
last_command = "$(history 2)";
|
||||||
|
alias = "$(alias)"
|
||||||
|
}
|
||||||
|
"zsh" => {
|
||||||
|
last_command = "$(fc -ln -1)";
|
||||||
|
alias = "$(alias)"
|
||||||
|
}
|
||||||
|
"fish" => {
|
||||||
|
last_command = "$(history | head -n 1)";
|
||||||
|
alias = "$(alias)";
|
||||||
|
}
|
||||||
|
"nu" | "nush" | "nushell" => {
|
||||||
|
last_command = "(history | last).command";
|
||||||
|
alias = "\"\"";
|
||||||
|
let command = format!(
|
||||||
|
"with-env {{ _PR_LAST_COMMAND : {},\
|
||||||
|
_PR_ALIAS : {},\
|
||||||
|
_PR_SHELL : nu }} \
|
||||||
|
{{ {} }}",
|
||||||
|
last_command, alias, binary_path
|
||||||
|
);
|
||||||
|
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");
|
||||||
|
let config_path = String::from_utf8_lossy(&output.stdout);
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
_ => std::process::exit(0),
|
||||||
|
};
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue