From 55e31d64313895dca60a2b56625ef42aa7230db7 Mon Sep 17 00:00:00 2001 From: qkzk Date: Sat, 26 Nov 2022 15:33:52 +0100 Subject: cd on quit --- readme.md | 17 +++++++++++++++++ src/actioner.rs | 19 ------------------- src/help.rs | 1 - src/lib.rs | 1 + src/log.rs | 2 +- src/main.rs | 49 ++----------------------------------------------- src/status.rs | 3 --- src/utils.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 69 insertions(+), 71 deletions(-) create mode 100644 src/utils.rs diff --git a/readme.md b/readme.md index dc77d29..3b52482 100644 --- a/readme.md +++ b/readme.md @@ -116,6 +116,23 @@ - [x] hardcoded limit to 10 tabs - [x] print selected path on quit - [x] Alt+d call dragon-drop on selected file +- [x] cd on quit: + + fm prints its current directory when exiting + + 1. Install a link to `fm` in your path or copy the binary + + 2. Add this to .zshrc : + + ```bash + function f() { + dest=$(fm $@) + if [[ ! -z $dest ]] + then + cd $dest + fi + } + ``` ## TODO diff --git a/src/actioner.rs b/src/actioner.rs index 259f09d..0c0a33c 100644 --- a/src/actioner.rs +++ b/src/actioner.rs @@ -71,9 +71,6 @@ impl Actioner { Event::Key(Key::Ctrl('q')) => self.escape(status), Event::Key(Key::Delete) => self.delete(status), Event::Key(Key::Insert) => self.insert(status), - Event::Key(Key::Alt('q')) => self.alt_q(status), - Event::Key(Key::Alt('d')) => self.alt_d(status), - Event::Key(Key::Char('Q')) => self.shift_q(status), Event::Key(Key::Char(c)) => self.char(status, c), Event::Key(Key::Home) => self.home(status), Event::Key(Key::End) => self.end(status), @@ -362,22 +359,6 @@ impl Actioner { status.selected().event_decompress() } - fn shift_q(&self, status: &mut Status) -> FmResult<()> { - status.selected().event_quit(); - Ok(()) - } - - fn alt_q(&self, status: &mut Status) -> FmResult<()> { - status.print_path_on_quit = true; - status.selected().event_quit(); - Ok(()) - } - - fn alt_d(&self, status: &mut Status) -> FmResult<()> { - status.selected().event_drag_n_drop()?; - Ok(()) - } - /// 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/help.rs b/src/help.rs index df71ea4..dd2cea9 100644 --- a/src/help.rs +++ b/src/help.rs @@ -4,7 +4,6 @@ pub static HELP_LINES: &str = " Default key bindings: q: quit -Atl+q: quit and print selected path h: help - Navigation - diff --git a/src/lib.rs b/src/lib.rs index 49e60dc..1ea4503 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,4 +25,5 @@ pub mod skim; pub mod status; pub mod tab; pub mod term_manager; +pub mod utils; pub mod visited; diff --git a/src/log.rs b/src/log.rs index 3a25fd1..7de0116 100644 --- a/src/log.rs +++ b/src/log.rs @@ -41,7 +41,7 @@ pub fn set_logger() -> FmResult { CompoundPolicy::new(Box::new(size_trigger), Box::new(fixed_window_roller)); let log_path = shellexpand::tilde(LOG_PATH).to_string(); // Don't propagate the error with ? since it crashes the application. - create_log_folder(LOG_PATH)?; + create_log_folder(&log_path)?; // Log Trace level output to file where trace is the default level // and the programmatically specified level to stderr. let config = Config::builder() diff --git a/src/main.rs b/src/main.rs index c0d82b4..90f04e3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,64 +2,19 @@ use std::sync::Arc; use clap::Parser; use log::info; -use sysinfo::{Disk, DiskExt, System, SystemExt}; -use tuikit::term::Term; +use sysinfo::{System, SystemExt}; use fm::actioner::Actioner; use fm::args::Args; use fm::config::load_config; -use fm::fileinfo::human_size; use fm::fm_error::FmResult; use fm::log::set_logger; use fm::status::Status; use fm::term_manager::{Display, EventReader}; +use fm::utils::{disk_space, init_term, print_on_quit}; static CONFIG_PATH: &str = "~/.config/fm/config.yaml"; -/// Returns a `Display` instance after `tuikit::term::Term` creation. -fn init_term() -> FmResult { - let term: Term<()> = Term::new()?; - term.enable_mouse_support()?; - Ok(term) -} - -fn disk_space(disks: &[Disk], path_str: String) -> String { - if path_str.is_empty() { - return "".to_owned(); - } - let mut size = 0_u64; - let mut disks: Vec<&Disk> = disks.iter().collect(); - disks.sort_by_key(|disk| disk.mount_point().as_os_str().len()); - for disk in disks { - if path_str.contains(disk.mount_point().as_os_str().to_str().unwrap()) { - size = disk.available_space(); - }; - } - human_size(size) -} - -fn print_on_quit( - term: Arc, - actioner: Actioner, - event_reader: EventReader, - status: Status, - display: Display, -) { - if status.print_path_on_quit { - let path = status - .selected_non_mut() - .path_content - .selected_path_str() - .unwrap_or_default(); - std::mem::drop(term); - std::mem::drop(actioner); - std::mem::drop(event_reader); - std::mem::drop(status); - std::mem::drop(display); - println!("{}", path) - } -} - /// Main function /// Init the status and display and listen to events from keyboard and mouse. /// The application is redrawn after every event. diff --git a/src/status.rs b/src/status.rs index b553028..9bd14ee 100644 --- a/src/status.rs +++ b/src/status.rs @@ -39,8 +39,6 @@ pub struct Status { pub marks: Marks, /// Colors for extension pub colors: ColorCache, - /// Should it display the last selected file before exiting? - pub print_path_on_quit: bool, /// terminal term: Arc, } @@ -57,7 +55,6 @@ impl Status { jump_index: 0, marks: Marks::read_from_config_file(), colors: ColorCache::default(), - print_path_on_quit: false, term, }) } diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..3ce6685 --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,48 @@ +use std::sync::Arc; + +use sysinfo::{Disk, DiskExt}; +use tuikit::term::Term; + +use crate::actioner::Actioner; +use crate::fileinfo::human_size; +use crate::fm_error::FmResult; +use crate::status::Status; +use crate::term_manager::{Display, EventReader}; + +/// Returns a `Display` instance after `tuikit::term::Term` creation. +pub fn init_term() -> FmResult { + let term: Term<()> = Term::new()?; + term.enable_mouse_support()?; + Ok(term) +} + +pub fn disk_space(disks: &[Disk], path_str: String) -> String { + if path_str.is_empty() { + return "".to_owned(); + } + let mut size = 0_u64; + let mut disks: Vec<&Disk> = disks.iter().collect(); + disks.sort_by_key(|disk| disk.mount_point().as_os_str().len()); + for disk in disks { + if path_str.contains(disk.mount_point().as_os_str().to_str().unwrap()) { + size = disk.available_space(); + }; + } + human_size(size) +} + +pub fn print_on_quit( + term: Arc, + actioner: Actioner, + event_reader: EventReader, + status: Status, + display: Display, +) { + let path = status.selected_non_mut().path_str().unwrap_or_default(); + std::mem::drop(term); + std::mem::drop(actioner); + std::mem::drop(event_reader); + std::mem::drop(status); + std::mem::drop(display); + println!("{}", path) +} -- cgit v1.2.3