diff options
-rw-r--r-- | src/bulkrename.rs | 17 | ||||
-rw-r--r-- | src/cli_info.rs | 5 | ||||
-rw-r--r-- | src/compress.rs | 5 | ||||
-rw-r--r-- | src/copy_move.rs | 4 | ||||
-rw-r--r-- | src/event_exec.rs | 52 | ||||
-rw-r--r-- | src/log.rs | 9 | ||||
-rw-r--r-- | src/marks.rs | 37 | ||||
-rw-r--r-- | src/opener.rs | 5 | ||||
-rw-r--r-- | src/password.rs | 8 | ||||
-rw-r--r-- | src/shell_menu.rs | 5 | ||||
-rw-r--r-- | src/status.rs | 8 | ||||
-rw-r--r-- | src/trash.rs | 14 |
12 files changed, 77 insertions, 92 deletions
diff --git a/src/bulkrename.rs b/src/bulkrename.rs index 9729ac7..91aebb2 100644 --- a/src/bulkrename.rs +++ b/src/bulkrename.rs @@ -7,7 +7,7 @@ use std::time::{Duration, SystemTime}; use crate::constant_strings_paths::TMP_FOLDER_PATH; use crate::impl_selectable_content; -use crate::log::write_log_line; +use crate::log_line; use crate::opener::Opener; use crate::status::Status; use crate::utils::random_name; @@ -163,11 +163,9 @@ impl<'a> Bulkrename<'a> { let new_name = sanitize_filename::sanitize(filename); self.rename_file(path, &new_name)?; counter += 1; - let log_line = format!("Bulk renamed {path} to {new_name}", path = path.display()); - write_log_line(log_line); + log_line!("Bulk renamed {path} to {new_name}", path = path.display()); } - let log_line = format!("Bulk renamed {counter} files"); - write_log_line(log_line); + log_line!("Bulk renamed {counter} files"); Ok(()) } @@ -186,20 +184,17 @@ impl<'a> Bulkrename<'a> { }; info!("creating: {new_path:?}"); std::fs::File::create(&new_path)?; - let log_line = format!("Bulk created {new_path}", new_path = new_path.display()); - write_log_line(log_line); + log_line!("Bulk created {new_path}", new_path = new_path.display()); counter += 1; } else { new_path.push(filename); info!("Bulk creating dir: {}", new_path.display()); std::fs::create_dir_all(&new_path)?; - let log_line = format!("Bulk created {new_path}", new_path = new_path.display()); - write_log_line(log_line); + log_line!("Bulk created {new_path}", new_path = new_path.display()); counter += 1; } } - let log_line = format!("Bulk created {counter} files"); - write_log_line(log_line); + log_line!("Bulk created {counter} files"); Ok(()) } diff --git a/src/cli_info.rs b/src/cli_info.rs index aa29b50..91544fd 100644 --- a/src/cli_info.rs +++ b/src/cli_info.rs @@ -5,7 +5,7 @@ use log::info; use crate::constant_strings_paths::CLI_INFO_COMMANDS; use crate::impl_selectable_content; -use crate::log::write_log_line; +use crate::log_line; use crate::utils::is_program_in_path; /// Holds the command line commands we can run and display @@ -45,8 +45,7 @@ impl CliInfo { pub fn execute(&self) -> Result<String> { let args = self.commands[self.index].clone(); info!("execute. {args:?}"); - let log_line = format!("Executed {args:?}"); - write_log_line(log_line); + log_line!("Executed {args:?}"); let child = Command::new(args[0]) .args(&args[1..]) .env("CLICOLOR_FORCE", "1") diff --git a/src/compress.rs b/src/compress.rs index d1bb6fb..8365542 100644 --- a/src/compress.rs +++ b/src/compress.rs @@ -5,7 +5,7 @@ use std::io::Write; use anyhow::Result; use crate::impl_selectable_content; -use crate::log::write_log_line; +use crate::log_line; use flate2::write::{DeflateEncoder, GzEncoder, ZlibEncoder}; use flate2::Compression; use lzma::LzmaWriter; @@ -71,8 +71,7 @@ impl Compresser { CompressionMethod::ZIP => Self::zip(Self::archive(here, "archive.zip")?, files)?, CompressionMethod::LZMA => Self::lzma(Self::archive(here, "archive.tar.xz")?, files)?, } - let log_line = format!("Compressed with {selected}"); - write_log_line(log_line); + log_line!("Compressed with {selected}"); Ok(()) } diff --git a/src/copy_move.rs b/src/copy_move.rs index 35c903e..a84202c 100644 --- a/src/copy_move.rs +++ b/src/copy_move.rs @@ -11,7 +11,7 @@ use tuikit::prelude::{Attr, Color, Effect, Event, Key, Term}; use crate::constant_strings_paths::NOTIFY_EXECUTABLE; use crate::fileinfo::human_size; -use crate::log::write_log_line; +use crate::log_line; use crate::opener::execute_in_child; use crate::utils::{is_program_in_path, random_name}; @@ -90,7 +90,7 @@ impl CopyMove { let message = format!("{preterit} {hs_bytes} bytes", preterit = self.preterit()); let _ = notify(&message); info!("{message}"); - write_log_line(message); + log_line!("{message}"); } fn setup_progress_bar( diff --git a/src/event_exec.rs b/src/event_exec.rs index 881b043..f908b66 100644 --- a/src/event_exec.rs +++ b/src/event_exec.rs @@ -14,7 +14,8 @@ use crate::constant_strings_paths::{ }; use crate::cryptsetup::{lsblk_and_cryptsetup_installed, BlockDeviceAction}; use crate::filter::FilterKind; -use crate::log::{read_log, write_log_line}; +use crate::log::read_log; +use crate::log_line; use crate::mocp::Mocp; use crate::mocp::MOCP; use crate::mode::{InputSimple, MarkAction, Mode, Navigate, NeedConfirmation}; @@ -143,12 +144,11 @@ impl EventAction { .directory_of_selected()? .join(filename); std::os::unix::fs::symlink(original_file, &link)?; - let log_line = format!( + log_line!( "Symlink {link} links to {original_file}", original_file = original_file.display(), link = link.display() ); - write_log_line(log_line); } status.clear_flags_and_reset_view() } @@ -457,7 +457,7 @@ impl EventAction { /// It obviously requires the `dragon-drop` command to be installed. pub fn drag_n_drop(status: &mut Status) -> Result<()> { if !is_program_in_path(DEFAULT_DRAGNDROP) { - write_log_line(format!("{DEFAULT_DRAGNDROP} must be installed.")); + log_line!("{DEFAULT_DRAGNDROP} must be installed."); return Ok(()); } let Some(file) = status.selected_non_mut().selected() else { @@ -795,7 +795,7 @@ impl EventAction { /// Display mediainfo details of an image pub fn mediainfo(tab: &mut Tab) -> Result<()> { if !is_program_in_path(MEDIAINFO) { - write_log_line(format!("{} isn't installed", MEDIAINFO)); + log_line!("{} isn't installed", MEDIAINFO); return Ok(()); } if let Mode::Normal | Mode::Tree = tab.mode { @@ -813,7 +813,7 @@ impl EventAction { /// Display a diff between the first 2 flagged files or dir. pub fn diff(status: &mut Status) -> Result<()> { if !is_program_in_path(DIFF) { - write_log_line(format!("{DIFF} isn't installed")); + log_line!("{DIFF} isn't installed"); return Ok(()); } if status.flagged.len() < 2 { @@ -923,7 +923,7 @@ impl EventAction { /// a luks encrypted device. pub fn encrypted_drive(status: &mut Status) -> Result<()> { if !lsblk_and_cryptsetup_installed() { - write_log_line("lsblk and cryptsetup must be installed.".to_owned()); + log_line!("lsblk and cryptsetup must be installed."); return Ok(()); } if status.encrypted_devices.is_empty() { @@ -937,7 +937,7 @@ impl EventAction { pub fn removable_devices(status: &mut Status) -> Result<()> { if !is_program_in_path(GIO) { - write_log_line("gio must be installed.".to_owned()); + log_line!("gio must be installed."); return Ok(()); } status.removable_devices = RemovableDevices::from_gio(); @@ -991,7 +991,7 @@ impl EventAction { /// Requires `nitrogen` to be installed. pub fn set_wallpaper(tab: &Tab) -> Result<()> { if !is_program_in_path(NITROGEN) { - write_log_line("nitrogen must be installed".to_owned()); + log_line!("nitrogen must be installed"); return Ok(()); } let Some(fileinfo) = tab.path_content.selected() else { @@ -1013,7 +1013,7 @@ impl EventAction { /// Add a song or a folder to MOC playlist. Start it first... pub fn mocp_add_to_playlist(tab: &Tab) -> Result<()> { if !is_program_in_path(MOCP) { - write_log_line("mocp isn't installed".to_owned()); + log_line!("mocp isn't installed"); return Ok(()); } Mocp::add_to_playlist(tab) @@ -1021,7 +1021,7 @@ impl EventAction { pub fn mocp_clear_playlist() -> Result<()> { if !is_program_in_path(MOCP) { - write_log_line("mocp isn't installed".to_owned()); + log_line!("mocp isn't installed"); return Ok(()); } Mocp::clear() @@ -1031,7 +1031,7 @@ impl EventAction { pub fn mocp_go_to_song(status: &mut Status) -> Result<()> { let tab = status.selected(); if !is_program_in_path(MOCP) { - write_log_line("mocp isn't installed".to_owned()); + log_line!("mocp isn't installed"); return Ok(()); } Mocp::go_to_song(tab)?; @@ -1044,7 +1044,7 @@ impl EventAction { /// Then toggle play/pause pub fn mocp_toggle_pause(status: &mut Status) -> Result<()> { if !is_program_in_path(MOCP) { - write_log_line("mocp isn't installed".to_owned()); + log_line!("mocp isn't installed"); return Ok(()); } Mocp::toggle_pause(status) @@ -1053,7 +1053,7 @@ impl EventAction { /// Skip to the next song in MOC pub fn mocp_next() -> Result<()> { if !is_program_in_path(MOCP) { - write_log_line("mocp isn't installed".to_owned()); + log_line!("mocp isn't installed"); return Ok(()); } Mocp::next() @@ -1062,7 +1062,7 @@ impl EventAction { /// Go to the previous song in MOC pub fn mocp_previous() -> Result<()> { if !is_program_in_path(MOCP) { - write_log_line("mocp isn't installed".to_owned()); + log_line!("mocp isn't installed"); return Ok(()); } Mocp::previous() @@ -1109,10 +1109,7 @@ impl NodeCreation { log::info!("root_path: {root_path:?}"); let path = root_path.join(sanitize_filename::sanitize(tab.input.string())); if path.exists() { - write_log_line(format!( - "{self} {path} already exists", - path = path.display() - )); + log_line!("{self} {path} already exists", path = path.display()); } else { match self { Self::Newdir => { @@ -1122,8 +1119,7 @@ impl NodeCreation { fs::File::create(&path)?; } } - let log_line = format!("Created new {self}: {path}", path = path.display()); - write_log_line(log_line); + log_line!("Created new {self}: {path}", path = path.display()); } tab.refresh_view() } @@ -1177,8 +1173,7 @@ impl LeaveMode { if let Some(path_str) = status.selected_non_mut().path_content_str() { let p = path::PathBuf::from(path_str); status.marks.new_mark(*ch, &p)?; - let log_line = format!("Saved mark {ch} -> {p}", p = p.display()); - write_log_line(log_line); + log_line!("Saved mark {ch} -> {p}", p = p.display()); } status.selected().window.reset(len); status.selected().input.reset(); @@ -1221,8 +1216,7 @@ impl LeaveMode { Status::set_permissions(path, permissions)? } status.flagged.clear(); - let log_line = format!("Changed permissions to {input_permission}"); - write_log_line(log_line); + log_line!("Changed permissions to {input_permission}"); } status.selected().refresh_view()?; status.reset_tabs_view() @@ -1318,12 +1312,11 @@ impl LeaveMode { original_path.display(), new_path.display() ); - let log_line = format!( + log_line!( "renaming: original: {} - new: {}", original_path.display(), new_path.display() ); - write_log_line(log_line); fs::rename(original_path, new_path)?; } @@ -1563,9 +1556,8 @@ impl LeaveMode { let first_arg = &format!("{username}@{hostname}:{remote_path}"); let command_output = execute_and_capture_output(SSHFS_EXECUTABLE, &[first_arg, current_path]); - let log_line = format!("{SSHFS_EXECUTABLE} output {command_output:?}"); - info!("{log_line}"); - write_log_line(log_line); + info!("{SSHFS_EXECUTABLE} output {command_output:?}"); + log_line!("{SSHFS_EXECUTABLE} output {command_output:?}"); Ok(()) } } @@ -93,3 +93,12 @@ where log::info!(target: "special", "{log_line}"); write_last_log_line(log_line); } + +#[macro_export] +macro_rules! log_line { + ($($arg:tt)+) => ( + $crate::log::write_log_line( + format!($($arg)+) + ) + ); +} diff --git a/src/marks.rs b/src/marks.rs index 0d5a8ce..87f6311 100644 --- a/src/marks.rs +++ b/src/marks.rs @@ -7,7 +7,7 @@ use log::info; use crate::constant_strings_paths::MARKS_FILEPATH; use crate::impl_selectable_content; -use crate::log::write_log_line; +use crate::log_line; use crate::utils::read_lines; /// Holds the marks created by the user. @@ -82,7 +82,7 @@ impl Marks { fn parse_line(line: Result<String, io::Error>) -> Result<(char, PathBuf)> { let line = line?; let sp: Vec<&str> = line.split(':').collect(); - if sp.len() <= 1 { + if sp.len() != 2 { return Err(anyhow!("marks: parse_line: Invalid mark line: {line}")); } if let Some(ch) = sp[0].chars().next() { @@ -100,30 +100,31 @@ impl Marks { /// If an update is done, the marks are saved again. pub fn new_mark(&mut self, ch: char, path: &Path) -> Result<()> { if ch == ':' { - let log_line = "new mark - ':' can't be used as a mark"; - write_log_line(log_line.to_owned()); + log_line!("new mark - ':' can't be used as a mark"); return Ok(()); } if self.used_chars.contains(&ch) { - let mut found_index = None; - for (index, (k, _)) in self.content.iter().enumerate() { - if *k == ch { - found_index = Some(index); - break; - } - } - let Some(found_index) = found_index else { - return Ok(()); - }; - self.content[found_index] = (ch, path.to_path_buf()); + self.update_mark(ch, path); } else { self.content.push((ch, path.to_path_buf())) } - let log_line = format!("Saved mark {ch} -> {p}", p = path.display()); - write_log_line(log_line); + self.save_marks()?; + log_line!("Saved mark {ch} -> {p}", p = path.display()); + Ok(()) + } - self.save_marks() + fn update_mark(&mut self, ch: char, path: &Path) { + let mut found_index = None; + for (index, (k, _)) in self.content.iter().enumerate() { + if *k == ch { + found_index = Some(index); + break; + } + } + if let Some(found_index) = found_index { + self.content[found_index] = (ch, path.to_path_buf()) + } } fn save_marks(&self) -> Result<()> { diff --git a/src/opener.rs b/src/opener.rs index 565f3d0..1b77ad7 100644 --- a/src/opener.rs +++ b/src/opener.rs @@ -15,7 +15,7 @@ use crate::constant_strings_paths::{ }; use crate::decompress::{decompress_gz, decompress_xz, decompress_zip}; use crate::fileinfo::extract_extension; -use crate::log::write_log_line; +use crate::log_line; use crate::utils::is_program_in_path; /// Different kind of extensions for default openers. @@ -433,8 +433,7 @@ pub fn execute_in_child<S: AsRef<std::ffi::OsStr> + fmt::Debug>( args: &[&str], ) -> Result<std::process::Child> { info!("execute_in_child. executable: {exe:?}, arguments: {args:?}"); - let log_line = format!("Execute: {exe:?}, arguments: {args:?}"); - write_log_line(log_line); + log_line!("Execute: {exe:?}, arguments: {args:?}"); Ok(Command::new(exe).args(args).spawn()?) } diff --git a/src/password.rs b/src/password.rs index 24ec0a7..3f98025 100644 --- a/src/password.rs +++ b/src/password.rs @@ -4,7 +4,7 @@ use std::process::{Command, Stdio}; use anyhow::{Context, Result}; use log::info; -use crate::log::write_log_line; +use crate::log_line; use crate::utils::current_username; /// Different kind of password @@ -132,8 +132,7 @@ where P: AsRef<std::path::Path> + std::fmt::Debug, { info!("sudo_with_password {args:?} CWD {path:?}"); - let log_line = format!("running sudo command with password. args: {args:?}, CWD: {path:?}"); - write_log_line(log_line); + log_line!("running sudo command with password. args: {args:?}, CWD: {path:?}"); let mut child = new_sudo_command_awaiting_password(args, path)?; inject_password(password, &mut child)?; let output = child.wait_with_output()?; @@ -165,8 +164,7 @@ where S: AsRef<std::ffi::OsStr> + std::fmt::Debug, { info!("running sudo {:?}", args); - let log_line = format!("running sudo command. {args:?}"); - write_log_line(log_line); + log_line!("running sudo command. {args:?}"); let child = new_sudo_command_passwordless(args)?; let output = child.wait_with_output()?; Ok(( diff --git a/src/shell_menu.rs b/src/shell_menu.rs index 05fed46..36d17ba 100644 --- a/src/shell_menu.rs +++ b/src/shell_menu.rs @@ -1,7 +1,7 @@ use anyhow::{Context, Result}; use crate::impl_selectable_content; -use crate::log::write_log_line; +use crate::log_line; use crate::opener::{execute_in_child_without_output, execute_in_child_without_output_with_path}; use crate::status::Status; use crate::utils::is_program_in_path; @@ -59,8 +59,7 @@ impl ShellMenu { } else { Self::simple(status, name.as_str())? }; - let log_line = format!("Executed {name}"); - write_log_line(log_line); + log_line!("Executed {name}"); Ok(()) } diff --git a/src/status.rs b/src/status.rs index 69f4bda..063118d 100644 --- a/src/status.rs +++ b/src/status.rs @@ -23,7 +23,7 @@ use crate::cryptsetup::{BlockDeviceAction, CryptoDeviceOpener}; use crate::fileinfo::FileKind; use crate::flagged::Flagged; use crate::iso::IsoDevice; -use crate::log::write_log_line; +use crate::log_line; use crate::marks::Marks; use crate::mode::{InputSimple, Mode, NeedConfirmation}; use crate::mount_help::MountHelper; @@ -728,8 +728,7 @@ impl Status { if iso_device.mount(¤t_username()?, &mut self.password_holder)? { info!("iso mounter mounted {iso_device:?}"); - let log_line = format!("iso : {}", iso_device.as_string()?,); - write_log_line(log_line); + log_line!("iso : {}", iso_device.as_string()?); let path = iso_device.mountpoints.clone().context("no mount point")?; self.selected().set_pathcontent(&path)?; }; @@ -942,8 +941,7 @@ impl Status { std::fs::remove_file(pathbuf)?; } } - let log_line = format!("Deleted {nb} flagged files"); - write_log_line(log_line); + log_line!("Deleted {nb} flagged files"); self.selected().reset_mode(); self.clear_flags_and_reset_view()?; self.refresh_status() diff --git a/src/trash.rs b/src/trash.rs index 3332901..77adaf0 100644 --- a/src/trash.rs +++ b/src/trash.rs @@ -10,7 +10,7 @@ use rand::{thread_rng, Rng}; use crate::constant_strings_paths::{TRASH_FOLDER_FILES, TRASH_FOLDER_INFO, TRASH_INFO_EXTENSION}; use crate::impl_selectable_content; -use crate::log::write_log_line; +use crate::log_line; use crate::utils::read_lines; const TRASHINFO_DATETIME_FORMAT: &str = "%Y-%m-%dT%H:%M:%S"; @@ -305,8 +305,7 @@ impl Trash { fn log_trash_add(origin: &Path, dest_file_name: &str) { info!("moved to trash {:?} -> {:?}", origin, dest_file_name); - let log_line = format!("moved to trash {:?} -> {:?}", origin, dest_file_name); - write_log_line(log_line); + log_line!("moved to trash {:?} -> {:?}", origin, dest_file_name); } /// Empty the trash, removing all the files and the trashinfo. @@ -331,8 +330,7 @@ impl Trash { } fn log_trash_empty(number_of_elements: usize) { - let log_line = format!("Emptied the trash: {number_of_elements} files permanently deleted"); - write_log_line(log_line); + log_line!("Emptied the trash: {number_of_elements} files permanently deleted"); info!("Emptied the trash: {number_of_elements} files permanently deleted"); } @@ -390,8 +388,7 @@ impl Trash { } fn log_trash_restore(origin: &Path) { - let log_line = format!("Trash restored: {origin}", origin = origin.display()); - write_log_line(log_line); + log_line!("Trash restored: {origin}", origin = origin.display()); } /// Deletes a file permanently from the trash. @@ -412,11 +409,10 @@ impl Trash { } fn log_trash_remove(trashed_file_content: &Path) { - let log_line = format!( + log_line!( "Trash removed: {trashed_file_content}", trashed_file_content = trashed_file_content.display() ); - write_log_line(log_line); } } |