diff options
author | qkzk <qu3nt1n@gmail.com> | 2023-12-12 21:40:39 +0100 |
---|---|---|
committer | qkzk <qu3nt1n@gmail.com> | 2023-12-12 21:40:39 +0100 |
commit | 3ded042f88d2f4708b5ff3cd76df1bf43d31b1e8 (patch) | |
tree | eec2b9461c74d8ae0e3f2cf8a210e00d7222e284 | |
parent | d72165280f0c61a202b09863ea7dba6773ab7812 (diff) |
!command and cli_application display their command and outputv0.1.24-use_rc
-rw-r--r-- | development.md | 3 | ||||
-rw-r--r-- | src/app/status.rs | 31 | ||||
-rw-r--r-- | src/io/display.rs | 8 | ||||
-rw-r--r-- | src/modes/display/preview.rs | 12 | ||||
-rw-r--r-- | src/modes/edit/cli_menu.rs | 6 | ||||
-rw-r--r-- | src/modes/edit/leave_mode.rs | 14 | ||||
-rw-r--r-- | src/modes/edit/shell_parser.rs | 2 |
7 files changed, 51 insertions, 25 deletions
diff --git a/development.md b/development.md index 6a327e0..fe266b6 100644 --- a/development.md +++ b/development.md @@ -705,7 +705,7 @@ New view: Tree ! Toggle with 't', fold with 'z'. Navigate normally. - [x] read (using serde_yaml) and write (using serde::Serialize) display settings from a [session file](~/.config/fm/session.yaml). - [x] make every session element private, ensure we read the correct setting for dual. - [x] FIX: opening help or fuzzyfindhelp crashes if a listed action has no keybind (aka. the user overwritten a keybind without creating one for old action). -- [ ] !command should print something +- [x] !command & cli_application print the command run and the output - [ ] Pre release - [x] Fix last missing items - [x] check installation (remove config, cargo build) @@ -716,6 +716,7 @@ New view: Tree ! Toggle with 't', fold with 'z'. Navigate normally. ## TODO +- [ ] refactor & unify all shell commands. Better names : Action::Action instead of command, Action::Command instead of shellwhatever - [ ] config loading : https://www.reddit.com/r/rust/comments/17v65j8/implement_configuration_files_without_reading_the/ - [ ] Only store one Selectable thing in status - [ ] use `Rc<str>` instead of string to avoid copying diff --git a/src/app/status.rs b/src/app/status.rs index de76f45..823aaa5 100644 --- a/src/app/status.rs +++ b/src/app/status.rs @@ -18,14 +18,14 @@ use crate::app::Tab; use crate::common::{args_is_empty, is_sudo_command, path_to_string}; use crate::common::{current_username, disk_space, filename_from_path, is_program_in_path}; use crate::config::Bindings; -use crate::io::Args; use crate::io::Internal; use crate::io::Kind; use crate::io::Opener; use crate::io::MIN_WIDTH_FOR_DUAL_PANE; +use crate::io::{execute_and_capture_output_with_path, Args}; use crate::io::{ execute_and_capture_output_without_check, execute_sudo_command_with_password, - execute_without_output_with_path, reset_sudo_faillock, + reset_sudo_faillock, }; use crate::modes::CopyMove; use crate::modes::Display; @@ -728,8 +728,12 @@ impl Status { } let current_directory = self.current_tab().directory_of_selected()?.to_owned(); let params: Vec<&str> = args.iter().map(|s| s.as_str()).collect(); - execute_without_output_with_path(executable, current_directory, Some(¶ms))?; - self.current_tab_mut().set_edit_mode(Edit::Nothing); + if let Ok(output) = + execute_and_capture_output_with_path(executable, current_directory, ¶ms) + { + self.preview_command_output(output, shell_command); + } + // self.current_tab_mut().set_edit_mode(Edit::Nothing); Ok(true) } } @@ -822,14 +826,14 @@ impl Status { fn run_sudo_command(&mut self) -> Result<()> { self.current_tab_mut().set_edit_mode(Edit::Nothing); reset_sudo_faillock()?; - let Some(sudo_command) = &self.menu.sudo_command else { + let Some(sudo_command) = self.menu.sudo_command.to_owned() else { return self.menu.clear_sudo_attributes(); }; - let args = ShellCommandParser::new(sudo_command).compute(self)?; + let args = ShellCommandParser::new(&sudo_command).compute(self)?; if args.is_empty() { return self.menu.clear_sudo_attributes(); } - execute_sudo_command_with_password( + let (_, output, _) = execute_sudo_command_with_password( &args[1..], self.menu .password_holder @@ -839,7 +843,9 @@ impl Status { self.current_tab().directory_of_selected()?, )?; self.menu.clear_sudo_attributes()?; - self.refresh_status() + self.preview_command_output(output, sudo_command.to_owned()); + // self.refresh_status() + Ok(()) } pub fn dispatch_password( @@ -862,6 +868,15 @@ impl Status { } } + pub fn preview_command_output(&mut self, output: String, command: String) { + log_info!("output {output}"); + self.current_tab_mut().reset_edit_mode(); + self.current_tab_mut().set_display_mode(Display::Preview); + let preview = Preview::cli_info(&output, command); + self.current_tab_mut().window.reset(preview.len()); + self.current_tab_mut().preview = preview; + } + pub fn update_nvim_listen_address(&mut self) { self.internal_settings.update_nvim_listen_address() } diff --git a/src/io/display.rs b/src/io/display.rs index a9ada82..95f6070 100644 --- a/src/io/display.rs +++ b/src/io/display.rs @@ -598,6 +598,7 @@ impl PreviewHeader { TextKind::LOG => Self::make_log(), _ => Self::make_default_preview(status, tab), }, + Preview::ColoredText(colored_text) => Self::make_colored_text(colored_text), _ => Self::make_default_preview(status, tab), } } @@ -617,6 +618,13 @@ impl PreviewHeader { ] } + fn make_colored_text(colored_text: &ColoredText) -> Vec<String> { + vec![ + " Command: ".to_owned(), + format!(" {command} ", command = colored_text.title()), + ] + } + fn _pick_previewed_fileinfo(status: &Status) -> Result<FileInfo> { if status.display_settings.dual() && status.display_settings.preview() { status.tabs[0].current_file() diff --git a/src/modes/display/preview.rs b/src/modes/display/preview.rs index bea6df0..75bd6ff 100644 --- a/src/modes/display/preview.rs +++ b/src/modes/display/preview.rs @@ -280,8 +280,8 @@ impl Preview { Self::Text(TextContent::log(log)) } - pub fn cli_info(output: &str) -> Self { - Self::ColoredText(ColoredText::new(output)) + pub fn cli_info(output: &str, command: String) -> Self { + Self::ColoredText(ColoredText::new(output, command)) } pub fn epub(path: &Path) -> Result<Self> { @@ -998,6 +998,7 @@ impl Ueberzug { #[derive(Clone, Debug)] pub struct ColoredText { + title: String, pub content: Vec<String>, len: usize, pub selected_index: usize, @@ -1012,12 +1013,17 @@ impl ColoredText { self.len == 0 } + pub fn title(&self) -> &str { + self.title.as_str() + } + /// Make a new previewed colored text. - pub fn new(output: &str) -> Self { + pub fn new(output: &str, title: String) -> Self { let content: Vec<String> = output.lines().map(|line| line.to_owned()).collect(); let len = content.len(); let selected_index = 0; Self { + title, content, len, selected_index, diff --git a/src/modes/edit/cli_menu.rs b/src/modes/edit/cli_menu.rs index 591f484..08f4d5d 100644 --- a/src/modes/edit/cli_menu.rs +++ b/src/modes/edit/cli_menu.rs @@ -52,7 +52,7 @@ impl CliCommand { /// Run its parsable command and capture its output. /// Some environement variables are first set to ensure the colored output. /// Long running commands may freeze the display. - fn execute(&self, status: &Status) -> Result<String> { + fn execute(&self, status: &Status) -> Result<(String, String)> { let args = ShellCommandParser::new(self.parsable_command).compute(status)?; log_info!("execute. {args:?}"); log_line!("Executed {args:?}"); @@ -67,7 +67,7 @@ impl CliCommand { e = command_output.status ); }; - Ok(text_output) + Ok((text_output, self.parsable_command.to_owned())) } } @@ -97,7 +97,7 @@ impl CliApplications { /// Run the selected command and capture its output. /// Some environement variables are first set to ensure the colored output. /// Long running commands may freeze the display. - pub fn execute(&self, status: &Status) -> Result<String> { + pub fn execute(&self, status: &Status) -> Result<(String, String)> { self.content[self.index].execute(status) } } diff --git a/src/modes/edit/leave_mode.rs b/src/modes/edit/leave_mode.rs index d201fa3..7d61f8a 100644 --- a/src/modes/edit/leave_mode.rs +++ b/src/modes/edit/leave_mode.rs @@ -23,7 +23,6 @@ use crate::modes::InputSimple; use crate::modes::MarkAction; use crate::modes::Navigate; use crate::modes::NodeCreation; -use crate::modes::Preview; use crate::modes::SelectableContent; use crate::modes::SortKind; @@ -46,7 +45,8 @@ impl LeaveMode { Edit::InputSimple(InputSimple::SetNvimAddr) => LeaveMode::set_nvim_addr(status)?, Edit::InputSimple(InputSimple::Shell) => { must_reset_mode = false; - must_refresh = LeaveMode::shell(status)?; + must_refresh = false; + LeaveMode::shell(status)?; } Edit::InputSimple(InputSimple::Sort) => LeaveMode::sort(status)?, Edit::InputSimple(InputSimple::Filter) => { @@ -157,13 +157,9 @@ impl LeaveMode { } pub fn cli_info(status: &mut Status) -> Result<()> { - let output = status.menu.cli_applications.execute(status)?; - log_info!("output\n{output}"); - status.current_tab_mut().reset_edit_mode(); - status.current_tab_mut().set_display_mode(Display::Preview); - let preview = Preview::cli_info(&output); - status.current_tab_mut().window.reset(preview.len()); - status.current_tab_mut().preview = preview; + let (output, command) = status.menu.cli_applications.execute(status)?; + log_info!("command {command}, output\n{output}"); + status.preview_command_output(output, command); Ok(()) } diff --git a/src/modes/edit/shell_parser.rs b/src/modes/edit/shell_parser.rs index 13fbfb9..c62ba04 100644 --- a/src/modes/edit/shell_parser.rs +++ b/src/modes/edit/shell_parser.rs @@ -25,7 +25,7 @@ impl Token { "%e" => Self::Extension, "%n" => Self::Filename, "%f" => Self::Flagged, - "%p" => Self::Path, + "%d" => Self::Path, _ => Self::Arg(arg.to_owned()), } } |