diff options
-rw-r--r-- | src/event_exec.rs | 39 | ||||
-rw-r--r-- | src/mode.rs | 13 | ||||
-rw-r--r-- | src/shell_parser.rs | 4 | ||||
-rw-r--r-- | src/status.rs | 3 | ||||
-rw-r--r-- | src/term_manager.rs | 6 |
5 files changed, 29 insertions, 36 deletions
diff --git a/src/event_exec.rs b/src/event_exec.rs index f9be542..9b4fb4d 100644 --- a/src/event_exec.rs +++ b/src/event_exec.rs @@ -359,20 +359,15 @@ impl EventExec { .directory_of_selected()? .to_owned(); let shell_command = status.selected_non_mut().input.string(); - let mut args = ShellCommandParser::new(shell_command.clone()).compute(status)?; + let mut args = ShellCommandParser::new(&shell_command).compute(status)?; if args.is_empty() { return Ok(()); } let executable = args.remove(0); let run_with_sudo = Self::is_sudo_command(&executable); if run_with_sudo { - Self::event_ask_password( - status, - PasswordKind::SUDO, - None, - PasswordUsage::SUDOCOMMAND, - Some(shell_command), - )?; + status.sudo_command = Some(shell_command); + Self::event_ask_password(status, PasswordKind::SUDO, None, PasswordUsage::SUDOCOMMAND)?; } else { let Ok(executable) = which::which(executable) else { return Ok(()); }; let params: Vec<&str> = args.iter().map(|s| s.as_str()).collect(); @@ -1380,7 +1375,7 @@ impl EventExec { pub fn event_enter(status: &mut Status, colors: &Colors) -> Result<()> { let mut must_refresh = true; let mut must_reset_mode = true; - match &status.selected_non_mut().mode { + match status.selected_non_mut().mode { Mode::InputSimple(InputSimple::Rename) => EventExec::exec_rename(status.selected())?, Mode::InputSimple(InputSimple::Newfile) => EventExec::exec_newfile(status.selected())?, Mode::InputSimple(InputSimple::Newdir) => EventExec::exec_newdir(status.selected())?, @@ -1396,11 +1391,11 @@ impl EventExec { must_refresh = false; EventExec::exec_filter(status, colors)? } - Mode::InputSimple(InputSimple::Password(kind, action, dest, command)) => { + Mode::InputSimple(InputSimple::Password(kind, action, dest)) => { must_refresh = false; must_reset_mode = false; - EventExec::exec_store_password(status, *kind, *dest)?; - EventExec::dispatch_password(status, *dest, *action)?; + EventExec::exec_store_password(status, kind, dest)?; + EventExec::dispatch_password(status, dest, action)?; } Mode::Navigate(Navigate::Jump) => EventExec::exec_jump(status)?, Mode::Navigate(Navigate::History) => EventExec::exec_history(status.selected())?, @@ -1754,7 +1749,6 @@ impl EventExec { PasswordKind::SUDO, Some(BlockDeviceAction::MOUNT), PasswordUsage::ISO, - None, )?; } else { if iso_device.mount(¤t_username()?, &mut status.password_holder)? { @@ -1783,7 +1777,6 @@ impl EventExec { PasswordKind::SUDO, Some(BlockDeviceAction::UMOUNT), PasswordUsage::ISO, - None, )?; } else { iso_device.umount(¤t_username()?, &mut status.password_holder)?; @@ -1802,7 +1795,6 @@ impl EventExec { PasswordKind::SUDO, Some(BlockDeviceAction::MOUNT), PasswordUsage::CRYPTSETUP, - None, ) } else if !status.password_holder.has_cryptsetup() { Self::event_ask_password( @@ -1810,7 +1802,6 @@ impl EventExec { PasswordKind::CRYPTSETUP, Some(BlockDeviceAction::MOUNT), PasswordUsage::CRYPTSETUP, - None, ) } else { status @@ -1839,7 +1830,6 @@ impl EventExec { PasswordKind::SUDO, Some(BlockDeviceAction::UMOUNT), PasswordUsage::CRYPTSETUP, - None, ) } else { status @@ -1854,7 +1844,6 @@ impl EventExec { password_kind: PasswordKind, encrypted_action: Option<BlockDeviceAction>, password_dest: PasswordUsage, - command: Option<String>, ) -> Result<()> { info!("event ask password"); status @@ -1863,7 +1852,6 @@ impl EventExec { password_kind, encrypted_action, password_dest, - command, ))); Ok(()) } @@ -1891,7 +1879,7 @@ impl EventExec { PasswordKind::SUDO => status.password_holder.set_sudo(password), PasswordKind::CRYPTSETUP => status.password_holder.set_cryptsetup(password), }, - PasswordUsage::SUDOCOMMAND => (), + PasswordUsage::SUDOCOMMAND => status.password_holder.set_sudo(password), } status.selected().reset_mode(); Ok(()) @@ -1921,7 +1909,14 @@ impl EventExec { } None => (), }, - PasswordUsage::SUDOCOMMAND => (), + PasswordUsage::SUDOCOMMAND => { + let Some(sudo_command) = &status.sudo_command else { return Ok(()); }; + let args = ShellCommandParser::new(sudo_command).compute(status)?; + if args.is_empty() { + return Ok(()); + } + sudo_password(&args[1..], &status.password_holder.sudo()?)?; + } } Ok(()) } @@ -2021,7 +2016,7 @@ impl EventExec { /// Execute a custom event on the selected file pub fn event_custom(status: &mut Status, string: String) -> Result<()> { info!("event_custom {string}"); - let parser = ShellCommandParser::new(string); + let parser = ShellCommandParser::new(&string); let mut args = parser.compute(status)?; let command = args.remove(0); let args: Vec<&str> = args.iter().map(|s| &**s).collect(); diff --git a/src/mode.rs b/src/mode.rs index fad65f6..ac6ba7d 100644 --- a/src/mode.rs +++ b/src/mode.rs @@ -59,7 +59,7 @@ impl std::fmt::Display for NeedConfirmation { /// the name of a new file, of a new directory, /// A regex to match all files in current directory, /// a kind of sort, a mark name, a new mark or a filter. -#[derive(Clone)] +#[derive(Clone, Copy)] pub enum InputSimple { /// Rename the selected file Rename, @@ -78,12 +78,7 @@ pub enum InputSimple { /// Set a new neovim RPC address SetNvimAddr, /// Input a password (chars a replaced by *) - Password( - PasswordKind, - Option<BlockDeviceAction>, - PasswordUsage, - Option<String>, - ), + Password(PasswordKind, Option<BlockDeviceAction>, PasswordUsage), /// Shell command execute as is Shell, } @@ -116,7 +111,7 @@ pub enum Navigate { /// Different mode in which the application can be. /// It dictates the reaction to event and what to display. -#[derive(Clone)] +#[derive(Clone, Copy)] pub enum Mode { /// Default mode: display the files Normal, @@ -152,7 +147,7 @@ impl fmt::Display for Mode { write!(f, "Sort: Kind Name Modif Size Ext Rev :") } Mode::InputSimple(InputSimple::Filter) => write!(f, "Filter: "), - Mode::InputSimple(InputSimple::Password(password_kind, _, _, _)) => { + Mode::InputSimple(InputSimple::Password(password_kind, _, _)) => { write!(f, "{password_kind}") } Mode::InputCompleted(InputCompleted::Exec) => write!(f, "Exec: "), diff --git a/src/shell_parser.rs b/src/shell_parser.rs index 7504a51..53ffc9c 100644 --- a/src/shell_parser.rs +++ b/src/shell_parser.rs @@ -40,9 +40,9 @@ pub struct ShellCommandParser { impl ShellCommandParser { /// Parse a command into a list of tokens - pub fn new(command: String) -> Self { + pub fn new(command: &str) -> Self { Self { - parsed: Self::parse(&command), + parsed: Self::parse(command), } } diff --git a/src/status.rs b/src/status.rs index 6dfed6a..a0e8ae7 100644 --- a/src/status.rs +++ b/src/status.rs @@ -84,6 +84,7 @@ pub struct Status { pub cli_info: CliInfo, pub start_folder: std::path::PathBuf, pub password_holder: PasswordHolder, + pub sudo_command: Option<String>, } impl Status { @@ -132,6 +133,7 @@ impl Status { .extend_with_mount_points(&Self::disks_mounts(sys.disks())); let iso_mounter = None; let password_holder = PasswordHolder::default(); + let sudo_command = None; Ok(Self { tabs: [left_tab, right_tab], @@ -157,6 +159,7 @@ impl Status { cli_info, start_folder, password_holder, + sudo_command, }) } diff --git a/src/term_manager.rs b/src/term_manager.rs index 796b910..6b14c32 100644 --- a/src/term_manager.rs +++ b/src/term_manager.rs @@ -475,7 +475,7 @@ impl<'a> WinSecondary<'a> { Mode::Navigate(Navigate::Marks(MarkAction::New)) => { vec!["Save mark...".to_owned()] } - Mode::InputSimple(InputSimple::Password(password_kind, _encrypted_action, _, _)) => { + Mode::InputSimple(InputSimple::Password(password_kind, _encrypted_action, _)) => { info!("term: password"); vec![format!("{password_kind}"), tab.input.password()] } @@ -523,7 +523,7 @@ impl<'a> WinSecondary<'a> { InputSimple::Filter => &FILTER_LINES, InputSimple::Newdir => &NEWDIR_LINES, InputSimple::Newfile => &NEWFILE_LINES, - InputSimple::Password(_, _, _, _) => &PASSWORD_LINES, + InputSimple::Password(_, _, __) => &PASSWORD_LINES, InputSimple::RegexMatch => ®EX_LINES, InputSimple::Rename => &RENAME_LINES, InputSimple::SetNvimAddr => &NVIM_ADDRESS_LINES, @@ -557,7 +557,7 @@ impl<'a> WinSecondary<'a> { canvas.show_cursor(true)?; canvas.set_cursor(0, Self::SORT_CURSOR_OFFSET)?; } - Mode::InputSimple(InputSimple::Password(_, _, _, _)) => { + Mode::InputSimple(InputSimple::Password(_, _, _)) => { canvas.show_cursor(true)?; canvas.set_cursor(0, Self::PASSWORD_CURSOR_OFFSET + tab.input.cursor_index)?; } |