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))?;
let cursor = crate::app::hook::shell_cursor_to_byte(shell, line, cursor);
if line.len() > MAX_HOOK_LINE_BYTES {
let action = crate::app::hook::insert_space_action(line, cursor);
println!("{}", crate::app::hook::render(shell, &action));
return Ok(CmdOutcome::Ok);
}
if paste_pending {
let action = crate::app::hook::insert_space_action(line, cursor);
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 action = crate::app::hook::insert_space_action(line, cursor);
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)
}