diff options
author | Kyohei Uto <im@kyoheiu.dev> | 2024-01-20 07:21:09 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-20 07:21:09 +0900 |
commit | 572802f0957b154546b9b2c89f0b8de6f69b90cc (patch) | |
tree | 368be8be7fc5dd077d3edb51c0a65ecd5e5df423 | |
parent | 9e03938efe5289eae394968a5713650aedcb9131 (diff) | |
parent | b4458b523605cfd06c04adb7ce16b9f1b7032a1d (diff) |
Merge pull request #262 from kyoheiu/feature-filechooser
Add choosefiles mode
-rw-r--r-- | src/main.rs | 15 | ||||
-rw-r--r-- | src/run.rs | 60 | ||||
-rw-r--r-- | src/state.rs | 2 |
3 files changed, 72 insertions, 5 deletions
diff --git a/src/main.rs b/src/main.rs index a669d8b..d3fd81b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,6 +24,7 @@ fn main() -> Result<(), errors::FxError> { if let Err(e) = run::run( std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")), false, + None, ) { eprintln!("{}", e); } @@ -37,6 +38,7 @@ fn main() -> Result<(), errors::FxError> { if let Err(e) = run::run( std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")), true, + None, ) { eprintln!("{}", e); } @@ -45,14 +47,23 @@ fn main() -> Result<(), errors::FxError> { print!("{}", shell::INTEGRATION_CODE); } _ => { - if let Err(e) = run::run(PathBuf::from(&args[1]), false) { + if args[1].starts_with("--choosefiles=") { + let target_path = PathBuf::from(args[1].split('=').nth(1).unwrap()); + if let Err(e) = run::run( + std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")), + false, + Some(target_path), + ) { + eprintln!("{}", e); + } + } else if let Err(e) = run::run(PathBuf::from(&args[1]), false, None) { eprintln!("{}", e); } } }, 3 => { if args[1] == "-l" || args[1] == "--log" { - if let Err(e) = run::run(PathBuf::from(&args[2]), true) { + if let Err(e) = run::run(PathBuf::from(&args[2]), true, None) { eprintln!("{}", e); } } else { @@ -29,7 +29,7 @@ const INITIAL_POS_COMMAND_LINE: u16 = 3; const INITIAL_POS_Z: u16 = 2; /// Launch the app. If initialization goes wrong, return error. -pub fn run(arg: PathBuf, log: bool) -> Result<(), FxError> { +pub fn run(arg: PathBuf, log: bool, choosefiles_path: Option<PathBuf>) -> Result<(), FxError> { //Check if argument path is valid. if !&arg.exists() { println!(); @@ -123,6 +123,7 @@ pub fn run(arg: PathBuf, log: bool) -> Result<(), FxError> { Ok(b) => !b, Err(_) => false, }; + state.choosefiles_target = choosefiles_path; //If the main function causes panic, catch it. let result = panic::catch_unwind(|| _run(state, session_path)); @@ -477,14 +478,67 @@ fn _run(mut state: State, session_path: PathBuf) -> Result<(), FxError> { //Open file or change directory KeyCode::Char('l') | KeyCode::Enter | KeyCode::Right => { - //In visual mode, this is disabled. + //In visual mode, if not with the choosefiles option, this is disabled. if state.v_start.is_some() { - continue; + if let Some(target_path) = &state.choosefiles_target { + match std::fs::File::options() + .write(true) + .truncate(true) + .create(true) + .open(target_path) + { + Err(e) => print_warning(e, state.layout.y), + Ok(mut f) => { + let items: Vec<&str> = state + .list + .iter() + .filter(|item| item.selected) + .filter_map(|item| { + item.file_path.as_os_str().to_str() + }) + .collect(); + let file_names = &items.join("\n"); + + if let Err(e) = writeln!(&mut f, "{}", file_names) { + print_warning(e, state.layout.y); + } else { + break 'main; + } + } + } + continue; + } else { + continue; + } } let mut dest: Option<PathBuf> = None; if let Ok(item) = state.get_item() { match item.file_type { FileType::File => { + // with choosefiles option, writes the file path + // to the target file + if let Some(target_path) = &state.choosefiles_target { + match std::fs::File::options() + .write(true) + .truncate(true) + .create(true) + .open(target_path) + { + Err(e) => print_warning(e, state.layout.y), + Ok(mut f) => { + if let Err(e) = writeln!( + &mut f, + "{}", + item.file_path.display() + ) { + print_warning(e, state.layout.y); + } else { + break 'main; + } + } + } + continue; + } execute!(screen, EnterAlternateScreen)?; if let Err(e) = state.open_file(item) { print_warning(e, state.layout.y); diff --git a/src/state.rs b/src/state.rs index 7cfecf6..53e185f 100644 --- a/src/state.rs +++ b/src/state.rs @@ -61,6 +61,7 @@ pub struct State { pub layout: Layout, pub v_start: Option<usize>, pub is_ro: bool, + pub choosefiles_target: Option<PathBuf>, } #[derive(Debug)] @@ -328,6 +329,7 @@ impl State { keyword: None, v_start: None, is_ro: false, + choosefiles_target: None, }) } |