diff options
author | Cameron Eldridge <cameldridge+git@gmail.com> | 2021-11-16 19:28:00 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-16 19:28:00 -0500 |
commit | 7d7894a1bf5bf38ee183911e6cfd4e43d1deed26 (patch) | |
tree | ad8dea3fde5cb5f174e92903dc5afd7e862e2952 | |
parent | b4357d641c1e046fdbcfaa27ecb453da4bfb2040 (diff) |
Add choosefiles feature (#105)
* Add choosefiles feature
* Use new QuitType::ChooseFile for choosefiles exit path
* Compute final selection better
-rw-r--r-- | src/commands/open_file.rs | 6 | ||||
-rw-r--r-- | src/context/app_context.rs | 7 | ||||
-rw-r--r-- | src/main.rs | 17 |
3 files changed, 26 insertions, 4 deletions
diff --git a/src/commands/open_file.rs b/src/commands/open_file.rs index e1d4208..cfa178a 100644 --- a/src/commands/open_file.rs +++ b/src/commands/open_file.rs @@ -2,7 +2,7 @@ use std::io; use std::path; use crate::config::AppMimetypeEntry; -use crate::context::AppContext; +use crate::context::{AppContext, QuitType}; use crate::error::{JoshutoError, JoshutoErrorKind, JoshutoResult}; use crate::ui::views::TuiTextField; use crate::ui::TuiBackend; @@ -51,7 +51,9 @@ pub fn open(context: &mut AppContext, backend: &mut TuiBackend) -> JoshutoResult let options = get_options(paths[0].as_path()); - if !options.is_empty() { + if context.args.choosefiles.is_some() { + context.quit = QuitType::ChooseFiles; + } else if !options.is_empty() { if options[0].get_fork() { options[0].execute_with(files.as_slice())?; } else { diff --git a/src/context/app_context.rs b/src/context/app_context.rs index 72b8426..4834a36 100644 --- a/src/context/app_context.rs +++ b/src/context/app_context.rs @@ -6,6 +6,7 @@ use crate::context::{ }; use crate::event::{AppEvent, Events}; use crate::util::search::SearchPattern; +use crate::Args; #[derive(Clone, Copy, Debug, PartialEq)] pub enum QuitType { @@ -13,12 +14,15 @@ pub enum QuitType { Normal, Force, ToCurrentDirectory, + ChooseFiles, } pub struct AppContext { pub quit: QuitType, // event loop querying pub events: Events, + // args from the command line + pub args: Args, // app config config: config::AppConfig, // context related to tabs @@ -38,7 +42,7 @@ pub struct AppContext { } impl AppContext { - pub fn new(config: config::AppConfig) -> Self { + pub fn new(config: config::AppConfig, args: Args) -> Self { let events = Events::new(); let event_tx = events.event_tx.clone(); @@ -47,6 +51,7 @@ impl AppContext { Self { quit: QuitType::DoNot, events, + args, tab_context: TabContext::new(), local_state: None, search_context: None, diff --git a/src/main.rs b/src/main.rs index e28e05a..375db4c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -78,6 +78,8 @@ lazy_static! { pub struct Args { #[structopt(long = "path", parse(from_os_str))] path: Option<PathBuf>, + #[structopt(long)] + choosefiles: Option<PathBuf>, #[structopt(short = "v", long = "version")] version: bool, #[structopt(long = "last-dir", parse(from_os_str))] @@ -100,7 +102,7 @@ fn run_joshuto(args: Args) -> Result<(), JoshutoError> { let config = AppConfig::get_config(CONFIG_FILE); let keymap = AppKeyMapping::get_config(KEYMAP_FILE); - let mut context = AppContext::new(config); + let mut context = AppContext::new(config, args.clone()); { let mut backend: ui::TuiBackend = ui::TuiBackend::new()?; run(&mut backend, &mut context, keymap)?; @@ -121,6 +123,19 @@ fn run_joshuto(args: Args) -> Result<(), JoshutoError> { file.write_all("\n".as_bytes())?; } } + QuitType::ChooseFiles => { + if let Some(path) = args.choosefiles { + let curr_tab = context.tab_context_ref().curr_tab_ref(); + let final_selection = curr_tab + .curr_list_ref() + .into_iter() + .flat_map(|s| s.get_selected_paths()); + let mut f = File::create(path)?; + for file in final_selection { + writeln!(f, "{}", file.display())?; + } + } + } QuitType::Force => {} _ => {} } |