Add support for null characters as separator

- Use `--read0 -` to read `\0` separated paths from stdin.
- Use `--write0` to write `\0` separated paths to stdout.
- Use `-0` or `--null` to combine `--read0` and `--write0`.
This commit is contained in:
Arijit Basu 2022-05-21 00:13:17 +05:30 committed by Arijit Basu
parent 1fef30ce56
commit 09002d0e65
2 changed files with 36 additions and 5 deletions

View File

@ -16,13 +16,16 @@ fn main() {
xplr [FLAG]... [OPTION]... [PATH] [SELECTION]..."###;
let flags = r###"
- Reads paths from stdin
- Reads new-line (\n) separated paths from stdin
-- Denotes the end of command-line flags and options
--force-focus Focuses on the given <PATH>, even if it is a directory
-h, --help Prints help information
--print-pwd-as-result Prints the present working directory when quitting
with `PrintResultAndQuit`
--read-only Enables read-only mode (config.general.read_only)
--read0 Reads paths separated using the null character (\0)
--write0 Prints paths separated using the null character (\0)
-0 --null Combines --read0 and --write0
-V, --version Prints version information"###;
let options = r###"
@ -51,8 +54,14 @@ fn main() {
} else if cli.version {
println!("xplr {}", xplr::app::VERSION);
} else {
let write0 = cli.write0;
match runner::from_cli(cli).and_then(|a| a.run()) {
Ok(Some(out)) => print!("{}", out),
Ok(Some(mut out)) => {
if write0 {
out = out.trim_end().replace('\n', "\0");
}
print!("{}", out);
}
Ok(None) => {}
Err(err) => {
if !err.to_string().is_empty() {

View File

@ -14,6 +14,8 @@ pub struct Cli {
pub read_only: bool,
pub force_focus: bool,
pub print_pwd_as_result: bool,
pub read0: bool,
pub write0: bool,
pub config: Option<PathBuf>,
pub extra_config: Vec<PathBuf>,
pub on_load: Vec<app::ExternalMsg>,
@ -49,9 +51,16 @@ impl Cli {
match arg.as_str() {
// Flags
"-" => {
for path in BufReader::new(std::io::stdin()).lines() {
cli.read_path(&path?)?;
}
let reader = BufReader::new(std::io::stdin());
if cli.read0 {
for path in reader.split(b'\0') {
cli.read_path(&String::from_utf8(path?)?)?;
}
} else {
for path in reader.lines() {
cli.read_path(&path?)?;
}
};
}
"-h" | "--help" => {
@ -62,6 +71,19 @@ impl Cli {
cli.version = true;
}
"--read0" => {
cli.read0 = true;
}
"--write0" => {
cli.write0 = true;
}
"-0" | "--null" => {
cli.read0 = true;
cli.write0 = true;
}
"--" => {
flag_ends = true;
}