summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorqkzk <qu3nt1n@gmail.com>2022-11-01 21:29:43 +0100
committerqkzk <qu3nt1n@gmail.com>2022-11-01 21:29:43 +0100
commit01f5f99d56a40f2e4ee90935962ee09e250b9ae0 (patch)
tree39b082eba6a785f18be01b2bd01a257a2f24d779
parentee828e77c0b3e28d2b006543ac65a6637825c0b3 (diff)
decompress a file with ctrl+x
-rw-r--r--readme.md1
-rw-r--r--src/actioner.rs5
-rw-r--r--src/compress.rs40
-rw-r--r--src/fileinfo.rs2
-rw-r--r--src/lib.rs1
-rw-r--r--src/opener.rs2
-rw-r--r--src/tab.rs9
7 files changed, 58 insertions, 2 deletions
diff --git a/readme.md b/readme.md
index 61daf34..035f22d 100644
--- a/readme.md
+++ b/readme.md
@@ -111,6 +111,7 @@
- [ ] args read correctly, use NVIM_LISTEN_ADDRESS if args is sent
- [ ] display / event separation. use async and message passing between coroutines
- [ ] preview images @ranger [ueberzug-rs](https://github.com/Adit-Chauhan/Ueberzug-rs) @[termimage](https://rawcdn.githack.com/nabijaczleweli/termimage/doc/termimage/index.html)
+- [ ] compress, decompress
## BUGS
diff --git a/src/actioner.rs b/src/actioner.rs
index ff4c50a..5794da3 100644
--- a/src/actioner.rs
+++ b/src/actioner.rs
@@ -92,6 +92,7 @@ impl Actioner {
Event::Key(Key::Ctrl('c')) => self.ctrl_c(status),
Event::Key(Key::Ctrl('p')) => self.ctrl_p(status),
Event::Key(Key::Ctrl('r')) => self.refresh_selected_view(status),
+ Event::Key(Key::Ctrl('x')) => self.ctrl_x(status),
Event::User(_) => self.refresh_selected_view(status),
_ => Ok(()),
}
@@ -338,6 +339,10 @@ impl Actioner {
status.selected().refresh_view()
}
+ fn ctrl_x(&self, status: &mut Status) -> FmResult<()> {
+ status.selected().event_decompress()
+ }
+
/// Match read key to a relevent event, depending on keybindings.
/// Keybindings are read from `Config`.
fn char(&self, status: &mut Status, c: char) -> FmResult<()> {
diff --git a/src/compress.rs b/src/compress.rs
new file mode 100644
index 0000000..8057adb
--- /dev/null
+++ b/src/compress.rs
@@ -0,0 +1,40 @@
+use std::path::Path;
+
+use crate::fm_error::FmResult;
+use crate::opener::execute_in_child;
+
+fn decompression_command(compressed_file: &Path) -> FmResult<Vec<&str>> {
+ match compressed_file
+ .extension()
+ .unwrap_or_default()
+ .to_str()
+ .unwrap_or_default()
+ {
+ "tgz" => Ok(vec!["tar", "xf"]),
+ "zip" => Ok(vec!["unzip"]),
+ "gzip" => Ok(vec!["gunzip"]),
+ "bzip2" => Ok(vec!["bunzip2"]),
+ "xz" => Ok(vec!["xz -d"]),
+ "7z" => Ok(vec!["7z e"]),
+ _ => Ok(vec![""]),
+ }
+}
+
+pub fn decompress(compressed_file: &Path) -> FmResult<()> {
+ let mut command = decompression_command(compressed_file)?;
+ if !command.is_empty() {
+ if let Some(parent) = compressed_file.parent() {
+ let oldwd = std::env::current_dir()?;
+
+ std::env::set_current_dir(parent)?;
+ command.push(compressed_file.to_str().unwrap_or_default());
+ let exe = command.remove(0);
+ execute_in_child(exe, &command)?;
+
+ std::env::set_current_dir(oldwd)?
+ } else {
+ return Ok(());
+ }
+ }
+ Ok(())
+}
diff --git a/src/fileinfo.rs b/src/fileinfo.rs
index ab4addd..e50153f 100644
--- a/src/fileinfo.rs
+++ b/src/fileinfo.rs
@@ -176,7 +176,7 @@ impl FileInfo {
);
if let FileKind::SymbolicLink = self.file_kind {
repr.push_str(" -> ");
- repr.push_str(&self.read_dest().unwrap_or("Broken link".to_owned()));
+ repr.push_str(&self.read_dest().unwrap_or_else(|| "Broken link".to_owned()));
}
Ok(repr)
}
diff --git a/src/lib.rs b/src/lib.rs
index 9e92729..f82bd6d 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -3,6 +3,7 @@ pub mod args;
pub mod bulkrename;
pub mod color_cache;
pub mod completion;
+pub mod compress;
pub mod config;
pub mod content_window;
pub mod copy_move;
diff --git a/src/opener.rs b/src/opener.rs
index 6d42d91..abe244d 100644
--- a/src/opener.rs
+++ b/src/opener.rs
@@ -274,7 +274,7 @@ impl Opener {
}
/// Execute the command in a fork.
-fn execute_in_child(exe: &str, args: &Vec<&str>) -> FmResult<std::process::Child> {
+pub fn execute_in_child(exe: &str, args: &Vec<&str>) -> FmResult<std::process::Child> {
eprintln!("exec exe {}, args {:?}", exe, args);
Ok(Command::new(exe).args(args).spawn()?)
}
diff --git a/src/tab.rs b/src/tab.rs
index 06d0dc1..07a2325 100644
--- a/src/tab.rs
+++ b/src/tab.rs
@@ -8,6 +8,7 @@ use copypasta::{ClipboardContext, ClipboardProvider};
use crate::args::Args;
use crate::completion::Completion;
+use crate::compress::decompress;
use crate::config::Config;
use crate::content_window::ContentWindow;
use crate::fileinfo::{FileKind, PathContent, SortBy};
@@ -492,6 +493,14 @@ impl Tab {
Ok(())
}
+ pub fn event_decompress(&mut self) -> FmResult<()> {
+ if let Some(fileinfo) = self.path_content.selected_file() {
+ decompress(&fileinfo.path)
+ } else {
+ Ok(())
+ }
+ }
+
fn nvim_listen_address(&self) -> Result<String, std::env::VarError> {
if !self.nvim_server.is_empty() {
Ok(self.nvim_server.clone())