mirror of
https://github.com/TECHNOFAB11/pay-respects.git
synced 2025-12-11 22:10:09 +01:00
refactor: initialization
This commit is contained in:
parent
b6b4afc226
commit
89be82307b
3 changed files with 75 additions and 62 deletions
25
src/args.rs
25
src/args.rs
|
|
@ -1,22 +1,20 @@
|
|||
use crate::shell::initialization;
|
||||
use crate::shell::{initialization, Init};
|
||||
use colored::Colorize;
|
||||
|
||||
|
||||
pub enum Status {
|
||||
Continue,
|
||||
Exit, // version, help, etc.
|
||||
Error,
|
||||
}
|
||||
|
||||
// returns true if should exit
|
||||
pub fn handle_args() -> Status {
|
||||
use Status::*;
|
||||
let args = std::env::args().collect::<Vec<String>>();
|
||||
if args.len() <= 1 {
|
||||
return Continue;
|
||||
}
|
||||
let mut auto_aliasing = String::new();
|
||||
let mut shell = String::new();
|
||||
let mut cnf = true;
|
||||
let mut init = Init::new();
|
||||
let mut index = 1;
|
||||
while index < args.len() {
|
||||
match args[index].as_str() {
|
||||
|
|
@ -31,35 +29,36 @@ pub fn handle_args() -> Status {
|
|||
"-a" | "--alias" => {
|
||||
if args.len() > index + 1 {
|
||||
if args[index + 1].starts_with('-') {
|
||||
auto_aliasing = String::from("f");
|
||||
init.alias = String::from("f");
|
||||
} else {
|
||||
auto_aliasing = args[index + 1].clone();
|
||||
init.alias = args[index + 1].clone();
|
||||
index += 1;
|
||||
}
|
||||
} else {
|
||||
auto_aliasing = String::from("f");
|
||||
init.alias = String::from("f");
|
||||
}
|
||||
init.auto_alias = true;
|
||||
index += 1;
|
||||
}
|
||||
"--noncf" => {
|
||||
cnf = false;
|
||||
init.cnf = false;
|
||||
index += 1
|
||||
}
|
||||
_ => {
|
||||
shell = args[index].clone();
|
||||
init.shell = args[index].clone();
|
||||
index += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if shell.is_empty() {
|
||||
if init.shell.is_empty() {
|
||||
eprintln!("{}", t!("no-shell"));
|
||||
return Error;
|
||||
}
|
||||
|
||||
let binary_path = &args[0];
|
||||
init.binary_path = args[0].clone();
|
||||
|
||||
initialization(&shell, binary_path, &auto_aliasing, cnf);
|
||||
initialization(&mut init);
|
||||
Exit
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -60,10 +60,10 @@ fn main() -> Result<(), std::io::Error> {
|
|||
};
|
||||
|
||||
data.expand_command();
|
||||
use shell::Mode;
|
||||
use shell::Mode::*;
|
||||
match data.mode {
|
||||
Mode::Suggestion => modes::suggestion(&mut data),
|
||||
Mode::Cnf => modes::cnf(&mut data),
|
||||
Suggestion => modes::suggestion(&mut data),
|
||||
Cnf => modes::cnf(&mut data),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
|||
106
src/shell.rs
106
src/shell.rs
|
|
@ -15,6 +15,25 @@ pub enum Mode {
|
|||
Suggestion,
|
||||
Cnf,
|
||||
}
|
||||
pub struct Init {
|
||||
pub shell: String,
|
||||
pub binary_path: String,
|
||||
pub alias: String,
|
||||
pub auto_alias: bool,
|
||||
pub cnf: bool,
|
||||
}
|
||||
|
||||
impl Init {
|
||||
pub fn new() -> Init {
|
||||
Init {
|
||||
shell: String::from(""),
|
||||
binary_path: String::from(""),
|
||||
alias: String::from("f"),
|
||||
auto_alias: false,
|
||||
cnf: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Data {
|
||||
pub shell: String,
|
||||
|
|
@ -29,13 +48,6 @@ pub struct Data {
|
|||
pub mode: Mode,
|
||||
}
|
||||
|
||||
pub struct Init {
|
||||
pub shell: String,
|
||||
pub binary_path: String,
|
||||
pub auto_alias: String,
|
||||
pub cnf: bool,
|
||||
}
|
||||
|
||||
impl Data {
|
||||
pub fn init() -> Data {
|
||||
let shell = get_shell();
|
||||
|
|
@ -307,44 +319,46 @@ pub fn expand_alias_multiline(map: &HashMap<String, String>, command: &str) -> O
|
|||
}
|
||||
}
|
||||
|
||||
pub fn initialization(shell: &str, binary_path: &str, auto_alias: &str, cnf: bool) {
|
||||
pub fn initialization(init: &mut Init) {
|
||||
let last_command;
|
||||
let alias;
|
||||
let shell_alias;
|
||||
let alias = &init.alias;
|
||||
let auto_alias = init.auto_alias;
|
||||
let cnf = init.cnf;
|
||||
let binary_path = &init.binary_path;
|
||||
|
||||
match shell {
|
||||
match init.shell.as_str() {
|
||||
"bash" => {
|
||||
last_command = "$(history 2)";
|
||||
alias = "$(alias)"
|
||||
shell_alias = "$(alias)"
|
||||
}
|
||||
"zsh" => {
|
||||
last_command = "$(fc -ln -1)";
|
||||
alias = "$(alias)"
|
||||
shell_alias = "$(alias)"
|
||||
}
|
||||
"fish" => {
|
||||
last_command = "$(history | head -n 1)";
|
||||
alias = "$(alias)";
|
||||
shell_alias = "$(alias)";
|
||||
}
|
||||
"nu" | "nush" | "nushell" => {
|
||||
last_command = "(history | last).command";
|
||||
alias = "\"\"";
|
||||
shell_alias = "\"\"";
|
||||
init.shell = "nu".to_string();
|
||||
}
|
||||
"pwsh" | "powershell" => {
|
||||
last_command = "Get-History | Select-Object -Last 1 | ForEach-Object {$_.CommandLine}";
|
||||
alias = ";";
|
||||
shell_alias = ";";
|
||||
init.shell = "pwsh".to_string();
|
||||
}
|
||||
_ => {
|
||||
println!("Unknown shell: {}", shell);
|
||||
println!("Unknown shell: {}", init.shell);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if shell == "nu" || shell == "nush" || shell == "nushell" {
|
||||
let pr_alias = if auto_alias.is_empty() {
|
||||
"f"
|
||||
} else {
|
||||
auto_alias
|
||||
};
|
||||
let shell = &init.shell;
|
||||
|
||||
if init.shell == "nu" {
|
||||
let init = format!(
|
||||
r#"
|
||||
def --env {} [] {{
|
||||
|
|
@ -352,20 +366,20 @@ def --env {} [] {{
|
|||
cd $dir
|
||||
}}
|
||||
"#,
|
||||
pr_alias, last_command, binary_path
|
||||
init.alias, last_command, init.binary_path
|
||||
);
|
||||
println!("{}", init);
|
||||
return;
|
||||
}
|
||||
|
||||
let mut init = match shell {
|
||||
let mut initialize = match shell.as_str() {
|
||||
"bash" | "zsh" | "fish" => format!(
|
||||
"\
|
||||
eval $(_PR_LAST_COMMAND=\"{}\" \
|
||||
_PR_ALIAS=\"{}\" \
|
||||
_PR_SHELL=\"{}\" \
|
||||
\"{}\")",
|
||||
last_command, alias, shell, binary_path
|
||||
last_command, shell_alias, shell, binary_path
|
||||
),
|
||||
"pwsh" | "powershell" => format!(
|
||||
r#"& {{
|
||||
|
|
@ -397,30 +411,30 @@ def --env {} [] {{
|
|||
return;
|
||||
}
|
||||
};
|
||||
if auto_alias.is_empty() {
|
||||
println!("{}", init);
|
||||
if !auto_alias {
|
||||
println!("{}", initialize);
|
||||
return;
|
||||
}
|
||||
|
||||
match shell {
|
||||
match shell.as_str() {
|
||||
"bash" | "zsh" => {
|
||||
init = format!(r#"alias {}='{}'"#, auto_alias, init);
|
||||
initialize = format!(r#"alias {}='{}'"#, alias, initialize);
|
||||
}
|
||||
"fish" => {
|
||||
init = format!(
|
||||
initialize = format!(
|
||||
r#"
|
||||
function {} -d "Terminal command correction"
|
||||
eval $({})
|
||||
end
|
||||
"#,
|
||||
auto_alias, init
|
||||
alias, initialize
|
||||
);
|
||||
}
|
||||
"pwsh" | "powershell" => {
|
||||
init = format!(
|
||||
"pwsh" => {
|
||||
initialize = format!(
|
||||
"function {} {{\n{}",
|
||||
auto_alias,
|
||||
init.split_once("\n").unwrap().1,
|
||||
alias,
|
||||
initialize.split_once("\n").unwrap().1,
|
||||
);
|
||||
}
|
||||
_ => {
|
||||
|
|
@ -430,9 +444,9 @@ end
|
|||
}
|
||||
|
||||
if cnf {
|
||||
match shell {
|
||||
match shell.as_str() {
|
||||
"bash" => {
|
||||
init = format!(
|
||||
initialize = format!(
|
||||
r#"
|
||||
command_not_found_handle() {{
|
||||
eval $(_PR_LAST_COMMAND="_ $@" _PR_SHELL="{}" _PR_MODE="cnf" "{}")
|
||||
|
|
@ -440,11 +454,11 @@ command_not_found_handle() {{
|
|||
|
||||
{}
|
||||
"#,
|
||||
shell, binary_path, init
|
||||
shell, binary_path, initialize
|
||||
);
|
||||
}
|
||||
"zsh" => {
|
||||
init = format!(
|
||||
initialize = format!(
|
||||
r#"
|
||||
command_not_found_handler() {{
|
||||
eval $(_PR_LAST_COMMAND="$@" _PR_SHELL="{}" _PR_MODE="cnf" "{}")
|
||||
|
|
@ -452,11 +466,11 @@ command_not_found_handler() {{
|
|||
|
||||
{}
|
||||
"#,
|
||||
shell, binary_path, init
|
||||
shell, binary_path, initialize
|
||||
);
|
||||
}
|
||||
"fish" => {
|
||||
init = format!(
|
||||
initialize = format!(
|
||||
r#"
|
||||
function fish_command_not_found --on-event fish_command_not_found
|
||||
eval $(_PR_LAST_COMMAND="$argv" _PR_SHELL="{}" _PR_MODE="cnf" "{}")
|
||||
|
|
@ -464,11 +478,11 @@ end
|
|||
|
||||
{}
|
||||
"#,
|
||||
shell, binary_path, init
|
||||
shell, binary_path, initialize
|
||||
);
|
||||
}
|
||||
"pwsh" | "powershell" => {
|
||||
init = format!(
|
||||
"pwsh" => {
|
||||
initialize = format!(
|
||||
r#"{}
|
||||
$ExecutionContext.InvokeCommand.CommandNotFoundAction =
|
||||
{{
|
||||
|
|
@ -488,7 +502,7 @@ $ExecutionContext.InvokeCommand.CommandNotFoundAction =
|
|||
$eventArgs.StopSearch = $True;
|
||||
}}
|
||||
"#,
|
||||
init, auto_alias
|
||||
initialize, alias
|
||||
)
|
||||
}
|
||||
_ => {
|
||||
|
|
@ -498,7 +512,7 @@ $ExecutionContext.InvokeCommand.CommandNotFoundAction =
|
|||
}
|
||||
}
|
||||
|
||||
println!("{}", init);
|
||||
println!("{}", initialize);
|
||||
}
|
||||
|
||||
pub fn get_shell() -> String {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue