CLI for a better "cd on quit"

Use `--print-pwd-as-result` to print the last working directory instead
of the focused or selected nodes, when you quit using the `PrintResultAndQuit`
message (i.e. by pressing `enter`).

This helps with implementing the "cd on quit" functionality using a plain shell
alias.

Example:

```
alias xcd='cd "$(xplr --print-pwd-as-result)"'
```

With this alias set, you can navigate directories using xplr by entering
`xcd` command, and when you quit by pressing enter, you will enter the
directory.

You can of course, quit with plain `Quit` (i.e. by pressing `esc`) to
gracefully cancel "cd on quit".
pull/438/head
Arijit Basu 2 years ago committed by Arijit Basu
parent 7da7c54e86
commit bb65870ee0

@ -2647,6 +2647,10 @@ impl App {
.unwrap_or_default()
}
pub fn pwd_str(&self) -> String {
format!("{}\n", &self.pwd)
}
pub fn selection_str(&self) -> String {
self.selection
.iter()

@ -27,7 +27,10 @@ fn main() {
"$HOME/.config/xplr/init.lua")
-C, --extra-config <PATH>... Specifies extra config files to load
--on-load <MESSAGE>... Sends messages when xplr loads
--force-focus Focuses on the given <PATH>, even if directory"###;
--force-focus Focuses on the given <PATH>, even if it is
a directory
--print-pwd-as-result Prints the last working directory when
quitting with `PrintResultAndQuit`"###;
let args = r###"
<PATH> Path to focus on, or enter if directory, (default is `.`)

@ -13,6 +13,7 @@ pub struct Cli {
pub help: bool,
pub read_only: bool,
pub force_focus: bool,
pub print_pwd_as_result: bool,
pub config: Option<PathBuf>,
pub extra_config: Vec<PathBuf>,
pub on_load: Vec<app::ExternalMsg>,
@ -98,6 +99,10 @@ impl Cli {
cli.force_focus = true;
}
"--print-pwd-as-result" => {
cli.print_pwd_as_result = true;
}
// path
path => {
cli.read_path(path)?;

@ -33,6 +33,22 @@ pub fn get_tty() -> Result<fs::File> {
}
}
// In unix system, the std::env::current_dir() calls libc getcwd() that
// returns physical path. As a workaround, this function tries to use `PWD`
// environment variable that is configured by shell.
fn get_current_dir() -> Result<PathBuf, std::io::Error> {
let cur = std::env::current_dir();
if let Ok(pwd) = std::env::var("PWD") {
if pwd.is_empty() {
cur
} else {
Ok(PathBuf::from(pwd))
}
} else {
cur
}
}
fn call_lua_heavy(
app: &app::App,
lua: &mlua::Lua,
@ -110,25 +126,10 @@ pub struct Runner {
extra_config_files: Vec<PathBuf>,
on_load: Vec<app::ExternalMsg>,
read_only: bool,
print_pwd_as_result: bool,
selection: Vec<PathBuf>,
}
// In unix system, the std::env::current_dir() calls libc getcwd() that
// returns physical path. As a workaround, this function tries to use `PWD`
// environment variable that is configured by shell.
fn get_current_dir() -> Result<PathBuf, std::io::Error> {
let cur = std::env::current_dir();
if let Ok(pwd) = std::env::var("PWD") {
if pwd.is_empty() {
cur
} else {
Ok(PathBuf::from(pwd))
}
} else {
cur
}
}
impl Runner {
/// Create a new runner object passing the default arguments
pub fn new() -> Result<Self> {
@ -161,6 +162,7 @@ impl Runner {
extra_config_files: cli.extra_config,
on_load: cli.on_load,
read_only: cli.read_only,
print_pwd_as_result: cli.print_pwd_as_result,
selection: paths.collect(),
})
}
@ -297,7 +299,12 @@ impl Runner {
}
PrintResultAndQuit => {
result = Ok(Some(app.result_str()));
result = if self.print_pwd_as_result {
Ok(Some(app.pwd_str()))
} else {
Ok(Some(app.result_str()))
};
break 'outer;
}

Loading…
Cancel
Save