use std::path::Path;
use std::str::FromStr;
use crate::domain::shell::Shell;
use crate::{AppContext, CmdOutcome, CmdResult, MAX_HOOK_LINE_BYTES};
pub(crate) fn handle(
shell_str: &str,
line: &str,
cursor: usize,
paste_pending: bool,
config_override: Option<&Path>,
path_prepend: Option<&Path>,
) -> CmdResult {
let shell = Shell::from_str(shell_str).map_err(|e| format!("{}", e))?;
if line.len() > MAX_HOOK_LINE_BYTES {
let cursor_safe = cursor.min(line.len());
let mut s = String::with_capacity(line.len() + 1);
s.push_str(&line[..cursor_safe]);
s.push(' ');
s.push_str(&line[cursor_safe..]);
let action = crate::app::hook::HookAction::InsertSpace {
line: s,
cursor: cursor_safe + 1,
};
println!("{}", crate::app::hook::render(shell, &action));
return Ok(CmdOutcome::Ok);
}
if paste_pending {
let action = crate::app::hook::HookAction::InsertSpace {
line: {
let mut s = String::with_capacity(line.len() + 1);
let cursor = cursor.min(line.len());
s.push_str(&line[..cursor]);
s.push(' ');
s.push_str(&line[cursor..]);
s
},
cursor: cursor.min(line.len()) + 1,
};
println!("{}", crate::app::hook::render(shell, &action));
return Ok(CmdOutcome::Ok);
}
let ctx = AppContext::build_optional(config_override, Some(shell_str), path_prepend, true);
let Some(config) = ctx.config else {
let cursor_safe = cursor.min(line.len());
let mut s = String::with_capacity(line.len() + 1);
s.push_str(&line[..cursor_safe]);
s.push(' ');
s.push_str(&line[cursor_safe..]);
let action = crate::app::hook::HookAction::InsertSpace { line: s, cursor: cursor_safe + 1 };
println!("{}", crate::app::hook::render(shell, &action));
return Ok(CmdOutcome::Ok);
};
let action = crate::app::hook::run(&config, shell, line, cursor, ctx.command_exists);
println!("{}", crate::app::hook::render(shell, &action));
Ok(CmdOutcome::Ok)
}