Merge branch 'fifo'
This commit is contained in:
commit
cd566d1708
@ -47,7 +47,8 @@ define-command sort-selections -params 0.. %{
|
|||||||
|
|
||||||
# TODO: Send additional parameters
|
# TODO: Send additional parameters
|
||||||
|
|
||||||
rust-selection-sort -R "$regex" $args -- "$@" > "$kak_command_fifo"
|
rust-selection-sort -R "$regex" -f "$kak_command_fifo" -- "$@"
|
||||||
|
|
||||||
}
|
}
|
||||||
exec R
|
exec R
|
||||||
}
|
}
|
||||||
|
23
src/errors.rs
Normal file
23
src/errors.rs
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#[derive(Debug)]
|
||||||
|
pub struct KakMessage(pub String, pub Option<String>);
|
||||||
|
|
||||||
|
impl From<std::io::Error> for KakMessage {
|
||||||
|
fn from(err: std::io::Error) -> Self {
|
||||||
|
Self(
|
||||||
|
"Error writing to fifo".to_string(),
|
||||||
|
Some(format!("{:?}", err)),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<String> for KakMessage {
|
||||||
|
fn from(err: String) -> Self {
|
||||||
|
Self(err, None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<shellwords::MismatchedQuotes> for KakMessage {
|
||||||
|
fn from(err: shellwords::MismatchedQuotes) -> Self {
|
||||||
|
Self("Corrupt kak response".to_string(), Some(err.to_string()))
|
||||||
|
}
|
||||||
|
}
|
139
src/main.rs
139
src/main.rs
@ -1,21 +1,29 @@
|
|||||||
#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
|
#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
|
||||||
|
|
||||||
|
mod errors;
|
||||||
use alphanumeric_sort::compare_str;
|
use alphanumeric_sort::compare_str;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
use errors::KakMessage;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
use std::env;
|
||||||
|
use std::fs;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::fs::OpenOptions;
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
struct KakMessage(String, Option<String>);
|
#[derive(Parser, Debug)]
|
||||||
|
|
||||||
#[derive(Parser)]
|
|
||||||
#[clap(about, version, author)]
|
#[clap(about, version, author)]
|
||||||
struct Options {
|
struct Options {
|
||||||
|
// TODO: Allow clap to parse these. Currently clap treats them as positional
|
||||||
|
// #[clap(env = "kak_command_fifo", takes_value = false)]
|
||||||
|
// kak_command_fifo_name: PathBuf,
|
||||||
|
// #[clap(env = "kak_response_fifo", takes_value = false)]
|
||||||
|
// kak_response_fifo_name: PathBuf,
|
||||||
|
#[clap(index = 1)]
|
||||||
|
regex: Option<String>,
|
||||||
#[clap(short = 'S', long)]
|
#[clap(short = 'S', long)]
|
||||||
// TODO: Can we invert a boolean? This name is terrible
|
// TODO: Can we invert a boolean? This name is terrible
|
||||||
no_skip_whitespace: bool,
|
no_skip_whitespace: bool,
|
||||||
#[clap(short = 'R', long, required = true)]
|
|
||||||
regex: String,
|
|
||||||
#[clap(multiple_occurrences = true, required = true)]
|
|
||||||
selections: Vec<String>,
|
|
||||||
#[clap(short, long)]
|
#[clap(short, long)]
|
||||||
lexicographic_sort: bool,
|
lexicographic_sort: bool,
|
||||||
#[clap(short, long)]
|
#[clap(short, long)]
|
||||||
@ -23,37 +31,57 @@ struct Options {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
match run() {
|
let msg = match run() {
|
||||||
Ok(()) => send_message(&KakMessage("Replaced successfully".to_string(), None)),
|
Ok(msg) => msg,
|
||||||
Err(msg) => send_message(&msg),
|
Err(msg) => {
|
||||||
}
|
eprintln!("{} (Debug info: {:?})", msg.0, msg.1);
|
||||||
|
msg
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
send_message(&msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send_message(msg: &KakMessage) {
|
fn send_message(msg: &KakMessage) {
|
||||||
// TODO: This isn't echoing anything
|
|
||||||
let msg_str = msg.0.replace('\'', "''");
|
let msg_str = msg.0.replace('\'', "''");
|
||||||
print!("echo '{}';", msg_str);
|
{
|
||||||
|
let mut f = open_command_fifo().unwrap();
|
||||||
|
|
||||||
if let Some(debug_info) = &msg.1 {
|
write!(f, "echo '{}';", msg_str).unwrap();
|
||||||
print!("echo -debug '{}';", msg_str);
|
write!(f, "echo -debug '{}';", msg_str).unwrap();
|
||||||
print!("echo -debug '{}';", debug_info.replace('\'', "''"));
|
|
||||||
|
if let Some(debug_msg_str) = &msg.1 {
|
||||||
|
write!(f, "echo -debug '{}';", debug_msg_str.replace('\'', "''")).unwrap();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run() -> Result<(), KakMessage> {
|
fn run() -> Result<KakMessage, KakMessage> {
|
||||||
let options = Options::try_parse()?;
|
let options = Options::try_parse().map_err(|e| {
|
||||||
|
KakMessage(
|
||||||
|
"Error parsing arguments".to_string(),
|
||||||
|
Some(format!("Could not parse: {:?}", e)),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
let replacement_re = options.regex;
|
let re = options
|
||||||
|
.regex
|
||||||
|
.as_ref()
|
||||||
|
.map(|r| Regex::new(r))
|
||||||
|
.transpose()
|
||||||
|
.map_err(|_| {
|
||||||
|
format!(
|
||||||
|
"Invalid regular expression: {}",
|
||||||
|
options.regex.unwrap_or("".to_string())
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
let re = Regex::new(&replacement_re)
|
let selections = read_selections()?;
|
||||||
.map_err(|_| format!("Invalid regular expression: {}", replacement_re))?;
|
|
||||||
|
|
||||||
let mut zipped = options
|
let mut zipped = selections
|
||||||
.selections
|
|
||||||
.iter()
|
.iter()
|
||||||
.zip(
|
.zip(
|
||||||
options
|
selections
|
||||||
.selections
|
|
||||||
.iter()
|
.iter()
|
||||||
.map(|a| {
|
.map(|a| {
|
||||||
if options.no_skip_whitespace {
|
if options.no_skip_whitespace {
|
||||||
@ -63,7 +91,7 @@ fn run() -> Result<(), KakMessage> {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
.map(|a| {
|
.map(|a| {
|
||||||
let captures = re.captures(a)?;
|
let captures = re.as_ref()?.captures(a)?;
|
||||||
captures
|
captures
|
||||||
.get(1)
|
.get(1)
|
||||||
.or_else(|| captures.get(0))
|
.or_else(|| captures.get(0))
|
||||||
@ -83,7 +111,9 @@ fn run() -> Result<(), KakMessage> {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
print!("reg '\"'");
|
let mut f = open_command_fifo()?;
|
||||||
|
|
||||||
|
write!(f, "reg '\"'")?;
|
||||||
|
|
||||||
let iter: Box<dyn Iterator<Item = _>> = if options.reverse {
|
let iter: Box<dyn Iterator<Item = _>> = if options.reverse {
|
||||||
Box::new(zipped.iter().rev())
|
Box::new(zipped.iter().rev())
|
||||||
@ -93,32 +123,47 @@ fn run() -> Result<(), KakMessage> {
|
|||||||
|
|
||||||
for i in iter {
|
for i in iter {
|
||||||
let new_selection = i.0.replace('\'', "''");
|
let new_selection = i.0.replace('\'', "''");
|
||||||
print!(" '{}'", new_selection);
|
write!(f, " '{}'", new_selection)?;
|
||||||
}
|
}
|
||||||
print!(" ;");
|
write!(f, " ; exec R;")?;
|
||||||
Ok(())
|
|
||||||
|
Ok(KakMessage(
|
||||||
|
format!("Sorted {} selections", selections.len()),
|
||||||
|
None,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<std::io::Error> for KakMessage {
|
fn read_selections() -> Result<Vec<String>, KakMessage> {
|
||||||
fn from(err: std::io::Error) -> Self {
|
{
|
||||||
Self(
|
let mut f = open_command_fifo()?;
|
||||||
"Error writing to fifo".to_string(),
|
|
||||||
Some(format!("{:?}", err)),
|
write!(
|
||||||
)
|
f,
|
||||||
|
"echo -quoting shell -to-file {} -- %val{{selections}}",
|
||||||
|
get_var("kak_response_fifo")?
|
||||||
|
)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let selections = shellwords::split(&fs::read_to_string(&get_var("kak_response_fifo")?)?)?;
|
||||||
|
|
||||||
|
Ok(selections)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<clap::Error> for KakMessage {
|
fn open_command_fifo() -> Result<File, KakMessage> {
|
||||||
fn from(err: clap::Error) -> Self {
|
OpenOptions::new()
|
||||||
Self(
|
.write(true)
|
||||||
"Error parsing arguments".to_string(),
|
.append(true)
|
||||||
Some(format!("{:?}", err)), // Some(err.message.pieces.map(|p| p.0).join()),
|
.open(&get_var("kak_command_fifo")?)
|
||||||
)
|
.map_err(|e| e.into())
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<String> for KakMessage {
|
fn get_var(var_name: &str) -> Result<String, KakMessage> {
|
||||||
fn from(err: String) -> Self {
|
env::var(var_name).map_err(|e| match e {
|
||||||
Self(err, None)
|
env::VarError::NotPresent => {
|
||||||
}
|
KakMessage(format!("Env var {} is not defined", var_name), None)
|
||||||
|
}
|
||||||
|
env::VarError::NotUnicode(_) => {
|
||||||
|
KakMessage(format!("Env var {} is not unicode", var_name), None)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user