Implement --select

pull/395/head
Tom van Dijk 3 years ago committed by Arijit Basu
parent a81dd3f63f
commit cbb244f9a0

1
Cargo.lock generated

@ -1107,6 +1107,7 @@ dependencies = [
"ansi-to-tui", "ansi-to-tui",
"anyhow", "anyhow",
"assert_cmd", "assert_cmd",
"atty",
"chrono", "chrono",
"criterion", "criterion",
"crossterm 0.22.1", "crossterm 0.22.1",

@ -34,6 +34,7 @@ humansize = "1.1.1"
mlua = { version = "0.6.6", features = ["luajit", "vendored", "serialize", "send"] } mlua = { version = "0.6.6", features = ["luajit", "vendored", "serialize", "send"] }
ansi-to-tui = "0.4.1" ansi-to-tui = "0.4.1"
libc = "0.2.105" libc = "0.2.105"
atty = "0.2.14"
[dev-dependencies] [dev-dependencies]
criterion = "0.3.5" criterion = "0.3.5"

@ -26,7 +26,8 @@ fn main() {
-c, --config <PATH> Specifies a custom config file (default is -c, --config <PATH> Specifies a custom config file (default is
"$HOME/.config/xplr/init.lua") "$HOME/.config/xplr/init.lua")
-C, --extra-config <PATH>... Specifies extra config files to load -C, --extra-config <PATH>... Specifies extra config files to load
--on-load <MESSAGE>... Sends messages when xplr loads"###; --on-load <MESSAGE>... Sends messages when xplr loads
--select <FILE> Selects all entries in a given file."###;
let args = r###" let args = r###"
<PATH> Path to focus on, or enter if directory"###; <PATH> Path to focus on, or enter if directory"###;

@ -15,6 +15,7 @@ pub struct Cli {
pub config: Option<PathBuf>, pub config: Option<PathBuf>,
pub extra_config: Vec<PathBuf>, pub extra_config: Vec<PathBuf>,
pub on_load: Vec<app::ExternalMsg>, pub on_load: Vec<app::ExternalMsg>,
pub select_file: Option<PathBuf>,
} }
impl Cli { impl Cli {
@ -75,6 +76,11 @@ impl Cli {
} }
} }
} }
"--select" => {
if cli.select_file.is_none() {
cli.select_file = args.pop_front().map(PathBuf::from);
}
}
// path // path
path => { path => {

@ -14,7 +14,10 @@ use crossterm::execute;
use crossterm::terminal as term; use crossterm::terminal as term;
use mlua::LuaSerdeExt; use mlua::LuaSerdeExt;
use std::fs; use std::fs;
use std::fs::File;
use std::io; use std::io;
use std::io::BufRead;
use std::io::BufReader;
use std::io::Write; use std::io::Write;
use std::path::PathBuf; use std::path::PathBuf;
use std::process::{Command, ExitStatus, Stdio}; use std::process::{Command, ExitStatus, Stdio};
@ -102,6 +105,20 @@ fn start_fifo(path: &str, focus_path: &str) -> Result<fs::File> {
} }
} }
fn process_lines<R: std::io::Read>(
lines: BufReader<R>,
tx_msg_in: &mpsc::Sender<app::Task>,
) -> Result<()> {
for line in lines.lines() {
let line = line?;
tx_msg_in.send(app::Task::new(
app::MsgIn::External(app::ExternalMsg::SelectPath(line)),
None,
))?;
}
Ok(())
}
pub struct Runner { pub struct Runner {
pwd: PathBuf, pwd: PathBuf,
focused_path: Option<PathBuf>, focused_path: Option<PathBuf>,
@ -109,6 +126,7 @@ pub struct Runner {
extra_config_files: Vec<PathBuf>, extra_config_files: Vec<PathBuf>,
on_load: Vec<app::ExternalMsg>, on_load: Vec<app::ExternalMsg>,
read_only: bool, read_only: bool,
select_file: Option<PathBuf>,
} }
impl Runner { impl Runner {
@ -138,6 +156,7 @@ impl Runner {
extra_config_files: cli.extra_config, extra_config_files: cli.extra_config,
on_load: cli.on_load, on_load: cli.on_load,
read_only: cli.read_only, read_only: cli.read_only,
select_file: cli.select_file,
}) })
} }
@ -222,6 +241,26 @@ impl Runner {
tx_msg_in.send(app::Task::new(app::MsgIn::External(msg), None))?; tx_msg_in.send(app::Task::new(app::MsgIn::External(msg), None))?;
} }
// Select all files in the select file
if let Some(f) = self.select_file {
let is_stdin = f.as_os_str() == "-";
if !f.is_file() && !is_stdin {
app = app.log_error(format!(
"Could not find your select file: {}",
f.to_string_lossy()
))?;
}
if is_stdin {
if atty::isnt(atty::Stream::Stdin) {
let lines = BufReader::new(std::io::stdin());
process_lines(lines, &tx_msg_in)?;
}
} else {
let lines = BufReader::new(File::open(f)?);
process_lines(lines, &tx_msg_in)?;
};
}
'outer: for task in rx_msg_in { 'outer: for task in rx_msg_in {
match app.handle_task(task) { match app.handle_task(task) {
Ok(a) => { Ok(a) => {

Loading…
Cancel
Save