Add xargs and stdin
This commit is contained in:
parent
8f3e1f5dc1
commit
d423f75989
@ -18,6 +18,12 @@ impl From<String> for KakMessage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<&str> for KakMessage {
|
||||||
|
fn from(err: &str) -> Self {
|
||||||
|
Self(err.to_string(), None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<shellwords::MismatchedQuotes> for KakMessage {
|
impl From<shellwords::MismatchedQuotes> for KakMessage {
|
||||||
fn from(err: shellwords::MismatchedQuotes) -> Self {
|
fn from(err: shellwords::MismatchedQuotes) -> Self {
|
||||||
Self("Corrupt kak response".to_string(), Some(err.to_string()))
|
Self("Corrupt kak response".to_string(), Some(err.to_string()))
|
||||||
|
@ -186,7 +186,7 @@ pub fn get_selections_with_desc() -> Result<Vec<SelectionWithDesc>, KakMessage>
|
|||||||
/// # Errors
|
/// # Errors
|
||||||
///
|
///
|
||||||
/// Will return `Err` if command fifo could not be opened, read from, or written to
|
/// Will return `Err` if command fifo could not be opened, read from, or written to
|
||||||
pub fn set_selections<'a, I, S: 'a>(selections: I) -> Result<(), KakMessage>
|
pub fn set_selections<'a, I, S: 'a + ?Sized>(selections: I) -> Result<(), KakMessage>
|
||||||
where
|
where
|
||||||
I: IntoIterator<Item = &'a S>,
|
I: IntoIterator<Item = &'a S>,
|
||||||
S: AsRef<str> + fmt::Display,
|
S: AsRef<str> + fmt::Display,
|
||||||
|
@ -14,6 +14,7 @@ mod kak;
|
|||||||
mod math_eval;
|
mod math_eval;
|
||||||
mod shuf;
|
mod shuf;
|
||||||
mod sort;
|
mod sort;
|
||||||
|
mod stdin;
|
||||||
mod trim;
|
mod trim;
|
||||||
mod uniq;
|
mod uniq;
|
||||||
mod xargs;
|
mod xargs;
|
||||||
@ -43,6 +44,7 @@ enum Commands {
|
|||||||
MathEval(math_eval::Options),
|
MathEval(math_eval::Options),
|
||||||
Trim(trim::Options),
|
Trim(trim::Options),
|
||||||
Xargs(xargs::Options),
|
Xargs(xargs::Options),
|
||||||
|
Stdin(stdin::Options),
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -79,6 +81,7 @@ fn run() -> Result<KakMessage, KakMessage> {
|
|||||||
Commands::MathEval(o) => math_eval::math_eval(o),
|
Commands::MathEval(o) => math_eval::math_eval(o),
|
||||||
Commands::Trim(o) => trim::trim(o),
|
Commands::Trim(o) => trim::trim(o),
|
||||||
Commands::Xargs(o) => xargs::xargs(o),
|
Commands::Xargs(o) => xargs::xargs(o),
|
||||||
|
Commands::Stdin(o) => stdin::stdin(o),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@ pub struct Options {
|
|||||||
reverse: bool,
|
reverse: bool,
|
||||||
#[clap(short, long)]
|
#[clap(short, long)]
|
||||||
ignore_case: bool,
|
ignore_case: bool,
|
||||||
|
// TODO: Sort by character ([xba] => [abx])
|
||||||
}
|
}
|
||||||
|
|
||||||
fn invert_bool(s: &str) -> Result<bool, &'static str> {
|
fn invert_bool(s: &str) -> Result<bool, &'static str> {
|
||||||
@ -117,8 +118,6 @@ fn to_sortable_selection<'a, 'b>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn sort(options: &Options) -> Result<KakMessage, KakMessage> {
|
pub fn sort(options: &Options) -> Result<KakMessage, KakMessage> {
|
||||||
eprintln!("Got sort options: {:?}", options);
|
|
||||||
|
|
||||||
// subselections is Some if the user requests it in subselections_register
|
// subselections is Some if the user requests it in subselections_register
|
||||||
// It will "exec z" to restore the selections before setting selections
|
// It will "exec z" to restore the selections before setting selections
|
||||||
// If subselections is None, "exec z" is not called
|
// If subselections is None, "exec z" is not called
|
||||||
|
26
src/xargs.rs
26
src/xargs.rs
@ -8,10 +8,9 @@ pub struct Options {
|
|||||||
args: Vec<String>,
|
args: Vec<String>,
|
||||||
}
|
}
|
||||||
pub fn xargs(options: &Options) -> Result<KakMessage, KakMessage> {
|
pub fn xargs(options: &Options) -> Result<KakMessage, KakMessage> {
|
||||||
// let mut selections = get_selections()?;
|
|
||||||
|
|
||||||
let mut child = Command::new("xargs")
|
let mut child = Command::new("xargs")
|
||||||
.arg("-0")
|
.arg("-0")
|
||||||
|
.arg("--")
|
||||||
.args(&options.args)
|
.args(&options.args)
|
||||||
.stdin(Stdio::piped())
|
.stdin(Stdio::piped())
|
||||||
.stdout(Stdio::piped())
|
.stdout(Stdio::piped())
|
||||||
@ -21,20 +20,27 @@ pub fn xargs(options: &Options) -> Result<KakMessage, KakMessage> {
|
|||||||
let mut stdin = child.stdin.take().expect("Failed to open stdin");
|
let mut stdin = child.stdin.take().expect("Failed to open stdin");
|
||||||
let handle = std::thread::spawn(move || -> Result<(), KakMessage> {
|
let handle = std::thread::spawn(move || -> Result<(), KakMessage> {
|
||||||
for s in get_selections_with_desc()? {
|
for s in get_selections_with_desc()? {
|
||||||
|
eprintln!("Got selection {}", s.content);
|
||||||
write!(stdin, "{}\0", s.content)?;
|
write!(stdin, "{}\0", s.content)?;
|
||||||
// stdin
|
|
||||||
// .write_all(&.as_bytes())
|
|
||||||
// .expect("Failed to write to stdin");
|
|
||||||
// stdin.write_all(&[b'\0']).expect("Failed to write to stdin");
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
});
|
});
|
||||||
|
|
||||||
set_selections(BufReader::new(child.stdout.take().expect("Failed to get stdout")).split(b'\0'));
|
eprintln!("About t oreadvv");
|
||||||
|
|
||||||
// stdout.
|
set_selections(
|
||||||
|
BufReader::new(child.stdout.take().ok_or("Failed to get stdout")?)
|
||||||
|
.split(b'\0')
|
||||||
|
.map(|s| Ok(String::from_utf8_lossy(&s?).into_owned()))
|
||||||
|
.collect::<Result<Vec<_>, KakMessage>>()?
|
||||||
|
.iter(),
|
||||||
|
)?;
|
||||||
|
|
||||||
// set_selections(selections.iter())?;
|
// Wait for the background process to exit
|
||||||
|
// TODO: Do not use a string
|
||||||
|
handle
|
||||||
|
.join()
|
||||||
|
.map_err(|_e| "Could not join background process")??;
|
||||||
|
|
||||||
Ok(KakMessage(format!("Shuf selections",), None))
|
Ok("xargs selections".into())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user