Skip to content

Commit

Permalink
Merge pull request #262 from kyoheiu/feature-filechooser
Browse files Browse the repository at this point in the history
Add choosefiles mode
  • Loading branch information
kyoheiu authored Jan 19, 2024
2 parents 9e03938 + b4458b5 commit 572802f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
15 changes: 13 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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 {
Expand Down
60 changes: 57 additions & 3 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!();
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -328,6 +329,7 @@ impl State {
keyword: None,
v_start: None,
is_ro: false,
choosefiles_target: None,
})
}

Expand Down

0 comments on commit 572802f

Please sign in to comment.