Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When argument path is file, point to that file #272

Merged
merged 3 commits into from
Jan 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 49 additions & 27 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,14 @@ pub fn run(arg: PathBuf, log: bool, choosefiles_path: Option<PathBuf>) -> Result
"Invalid path: {}\n`fx -h` shows help.",
&arg.display()
)));
} else if !&arg.is_dir() {
return Err(FxError::Arg(
"Path should be directory.\n`fx -h` shows help.".to_owned(),
));
}

let shell_pid: Option<String> = env::var("SHELL_PID").ok();

//Prepare config and data local path.
let config_dir_path = {
let mut path = dirs::config_dir()
.ok_or_else(|| FxError::Dirs("Cannot read the config directory.".to_string()))?;
path.push(FELIX);
path
};
//Prepare data local and trash dir path.
} // else if !&arg.is_dir() {
// return Err(FxError::Arg(
// "Path should be directory.\n`fx -h` shows help.".to_owned(),
// ));
// }
// disabled in favor of file-selection

//Prepare data local dir path, trash dir path and other paths you need.
let data_local_path = {
let mut path = dirs::data_local_dir()
.ok_or_else(|| FxError::Dirs("Cannot read the data local directory.".to_string()))?;
Expand All @@ -74,9 +66,6 @@ pub fn run(arg: PathBuf, log: bool, choosefiles_path: Option<PathBuf>) -> Result
path.push(FELIX);
path
};
if !config_dir_path.exists() {
std::fs::create_dir_all(&config_dir_path)?;
}
if !data_local_path.exists() {
std::fs::create_dir_all(&data_local_path)?;
}
Expand All @@ -85,6 +74,7 @@ pub fn run(arg: PathBuf, log: bool, choosefiles_path: Option<PathBuf>) -> Result
}

//Path of the file used to store lwd (Last Working Directory) at the end of the session.
let shell_pid: Option<String> = env::var("SHELL_PID").ok();
let lwd_file_path = shell_pid.map(|basename| runtime_path.join(basename));

let trash_dir_path = {
Expand All @@ -110,14 +100,32 @@ pub fn run(arg: PathBuf, log: bool, choosefiles_path: Option<PathBuf>) -> Result

//Initialize app state. Inside State::new(), config file is read or created.
let mut state = State::new(&session_path)?;
state.trash_dir = trash_dir_path;
state.lwd_file = lwd_file_path;
state.current_dir = if cfg!(not(windows)) {
// If executed this on windows, "//?" will be inserted at the beginning of the path.
arg.canonicalize()?
let mut file_selected = None;
let _current_dir = if arg.is_dir() {
if cfg!(not(windows)) {
// If executed this on windows, "//?" will be inserted at the beginning of the path.
arg.canonicalize()?
} else {
arg
}
} else if let Some(parent) = arg.clone().parent() {
file_selected = {
arg.file_name().map(|name| name.to_str()).unwrap_or_else(|| None)
};
if cfg!(not(windows)) {
// Same as when is_dir()
parent.canonicalize()?
} else {
parent.to_path_buf()
}
} else {
arg
eprintln!("Cannot detect the directory: Will open the current directory.");
std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."))
};

state.current_dir = _current_dir;
state.trash_dir = trash_dir_path;
state.lwd_file = lwd_file_path;
state.jumplist.add(&state.current_dir);
state.is_ro = match has_write_permission(&state.current_dir) {
Ok(b) => !b,
Expand All @@ -126,7 +134,7 @@ pub fn run(arg: PathBuf, log: bool, choosefiles_path: Option<PathBuf>) -> Result
state.choosefiles_target = choosefiles_path;

//If the main function causes panic, catch it.
let result = panic::catch_unwind(|| _run(state, session_path));
let result = panic::catch_unwind(|| _run(state, session_path, file_selected));
leave_raw_mode();

if let Err(panic) = result {
Expand All @@ -147,7 +155,11 @@ pub fn run(arg: PathBuf, log: bool, choosefiles_path: Option<PathBuf>) -> Result
}

/// Run the app. (Containing the main loop)
fn _run(mut state: State, session_path: PathBuf) -> Result<(), FxError> {
fn _run(
mut state: State,
session_path: PathBuf,
file_selected: Option<&str>,
) -> Result<(), FxError> {
//Enter the alternate screen with crossterm
let mut screen = stdout();
enter_raw_mode();
Expand All @@ -168,6 +180,16 @@ fn _run(mut state: State, session_path: PathBuf) -> Result<(), FxError> {
} else {
state.reload(BEGINNING_ROW)?;
}

// If file path is set as argument, point to that file.
if let Some(p) = file_selected {
if let Some(target) = state.list.iter().position(|x| x.file_name == p) {
state.layout.nums.skip = target as u16;
state.layout.nums.index = target;
state.redraw(BEGINNING_ROW);
}
}

screen.flush()?;

'main: loop {
Expand Down
Loading