rust-selection-sort.kak/src/sort.rs

204 lines
6.6 KiB
Rust
Raw Normal View History

use crate::{
get_selections_with_desc, kak, open_command_fifo, KakMessage, SelectionDesc, SelectionWithDesc,
};
2022-02-21 17:23:34 -05:00
use alphanumeric_sort::compare_str;
use regex::Regex;
2022-02-21 18:30:32 -05:00
use std::{cmp::Ordering, io::Write, str::FromStr};
2022-02-21 17:23:34 -05:00
#[derive(clap::StructOpt, Debug)]
pub struct Options {
2022-02-21 17:23:34 -05:00
#[clap(index = 1)]
regex: Option<Regex>,
#[clap(short = 's', long)]
subselections_register: Option<char>,
// TODO: Can we invert a boolean? This name is terrible
2022-02-27 18:14:42 -05:00
#[clap(short = 'S', long, parse(try_from_str = invert_bool), default_value_t)]
2022-02-21 17:23:34 -05:00
no_skip_whitespace: bool,
#[clap(short, long)]
lexicographic_sort: bool,
#[clap(short, long)]
reverse: bool,
#[clap(short, long)]
ignore_case: bool,
}
2022-02-27 18:14:42 -05:00
fn invert_bool(s: &str) -> Result<bool, &'static str> {
// Invert the boolean
match s {
"false" => Ok(true),
"true" => Ok(false),
_ => Err("Unparsable boolean value"),
}
}
2022-02-21 17:23:34 -05:00
struct SortableSelection<'a> {
/// The content of the selection
content: &'a str,
/// The string used to compare the content
content_comparison: &'a str,
/// Any subselections
subselections: Vec<&'a str>,
}
2022-02-21 18:27:46 -05:00
/// Gets a Vec of sortable selections with a given list of subselections and descriptions
2022-02-21 17:23:34 -05:00
fn get_sortable_selections_subselections<'a, 'b, 'tmp, S: AsRef<str> + std::fmt::Debug + 'a>(
options: &'b Options,
selections: Vec<SelectionWithDesc>,
subselections: Vec<SelectionWithDesc>,
2022-02-21 17:23:34 -05:00
) -> Result<Vec<SortableSelection<'a>>, KakMessage> {
let mut sortable_selections = selections
.iter()
2022-02-21 18:08:18 -05:00
.zip(selections_desc.iter())
.map(|(s, sd)| {
Ok((
to_sortable_selection(s.as_ref(), options),
2022-02-21 18:08:18 -05:00
SelectionDesc::from_str(sd.as_ref())?,
))
})
.collect::<Result<Vec<(SortableSelection, SelectionDesc)>, KakMessage>>()?;
2022-02-21 17:23:34 -05:00
2022-02-21 18:08:18 -05:00
let mut subselections = subselections
.iter()
.zip(subselections_desc.iter())
2022-02-27 18:14:42 -05:00
// Bind selection with associated description
// Sort descriptions so left is always <= right
.map(|(s, sd)| Ok((s.as_ref(), SelectionDesc::from_str(sd.as_ref())?.sort())))
2022-02-21 18:08:18 -05:00
.collect::<Result<Vec<(&str, SelectionDesc)>, KakMessage>>()?;
2022-02-27 18:14:42 -05:00
// Sort subselections by description
2022-02-21 18:08:18 -05:00
subselections.sort_by(|(_, ssd_a), (_, ssd_b)| ssd_a.cmp(ssd_b));
2022-02-21 18:27:46 -05:00
// For each selection, check if they contain any subselections
// If so, add them to the subselections vector
2022-02-21 18:08:18 -05:00
// TODO: This is O(n^2), but can be made more efficient since subselections is sorted
for (s, s_desc) in &mut sortable_selections {
for i in &subselections {
if s_desc.contains(&i.1) {
s.subselections.push(i.0);
2022-02-21 18:08:18 -05:00
}
}
}
sortable_selections.sort_by(|(a, _), (b, _)| {
2022-02-21 18:27:46 -05:00
// First, check if there are any subselection comparisons to be made
// If one has more subselections than the other, stop comparing
2022-02-21 18:08:18 -05:00
for (a_subsel, b_subsel) in a.subselections.iter().zip(b.subselections.iter()) {
match a_subsel.cmp(b_subsel) {
2022-02-21 18:30:17 -05:00
// These subselections are equal, so we can't do anything
2022-02-21 18:08:18 -05:00
Ordering::Equal => continue,
2022-02-21 18:27:46 -05:00
// We found a difference, so return the comparison
2022-02-21 18:08:18 -05:00
o => return o,
}
}
2022-02-21 18:27:46 -05:00
// No subselections mismatched, so compare the (possibly trimmed) content
2022-02-21 18:08:18 -05:00
a.content_comparison.cmp(b.content_comparison)
});
Ok(sortable_selections.into_iter().map(|(s, _)| s).collect())
2022-02-21 17:23:34 -05:00
}
fn to_sortable_selection<'a, 'b>(
selection: &'a str,
options: &'b Options,
2022-02-21 17:23:34 -05:00
) -> SortableSelection<'a> {
if options.no_skip_whitespace {
2022-02-21 17:23:34 -05:00
SortableSelection {
content: selection,
content_comparison: selection,
subselections: vec![],
}
} else {
SortableSelection {
content: selection,
content_comparison: selection.trim(),
subselections: vec![],
}
}
}
pub fn sort(options: &Options) -> Result<KakMessage, KakMessage> {
eprintln!("Got sort options: {:?}", options);
2022-02-27 18:14:42 -05:00
let subselections = options
2022-02-21 17:23:34 -05:00
.subselections_register
.map(|_r| -> Result<Vec<SelectionWithDesc>, KakMessage> {
let ret = get_selections_with_desc()?;
kak::exec("exec z")?;
Ok(ret)
2022-02-21 17:23:34 -05:00
})
.transpose()?;
let selections = get_selections_with_desc()?;
// let subselections: Option<(Vec<String>, Vec<String>)> = options
// .subselections_register
// .map::<Result<(Vec<String>, Vec<String>), KakMessage>, _>(|_c| {
// let subselections = kak::response("%val{selections}")?;
// let subselections_desc = kak::response("%val{selections_desc}")?;
// // kak_exec(&format!("exec z",))?;
// Ok((subselections, subselections_desc))
// })
// .transpose()?;
// let selections = kak::response("%val{selections}")?;
2022-02-21 17:23:34 -05:00
let mut zipped: Vec<SortableSelection<'_>> = match subselections {
Some(subselections) => {
let selections_desc = kak::response("%val{selections_desc}")?;
2022-02-21 17:23:34 -05:00
get_sortable_selections_subselections(
options,
2022-02-21 17:23:34 -05:00
&selections,
&selections_desc,
subselections,
subselections_desc,
)?
}
None => selections
.iter()
.map(|s| to_sortable_selection(s, options))
2022-02-21 17:23:34 -05:00
.collect(),
};
zipped.sort_by(|a, b| {
for (a_subselection, b_subselection) in a.subselections.iter().zip(b.subselections.iter()) {
let comparison = if options.lexicographic_sort {
2022-02-21 17:23:34 -05:00
a_subselection.cmp(b_subselection)
} else {
compare_str(a_subselection, b_subselection)
};
match comparison {
Ordering::Less | Ordering::Greater => return comparison,
Ordering::Equal => {}
}
}
if options.lexicographic_sort {
2022-02-21 17:23:34 -05:00
a.content_comparison.cmp(b.content_comparison)
} else {
compare_str(a.content_comparison, b.content_comparison)
}
});
let mut f = open_command_fifo()?;
write!(f, "reg '\"'")?;
let iter: Box<dyn Iterator<Item = _>> = if options.reverse {
2022-02-21 17:23:34 -05:00
Box::new(zipped.iter().rev())
} else {
Box::new(zipped.iter())
};
for i in iter {
let new_selection = i.content.replace('\'', "''");
write!(f, " '{}'", new_selection)?;
}
write!(f, " ; exec R;")?;
2022-03-07 21:54:10 -05:00
f.flush()?;
2022-02-21 17:23:34 -05:00
Ok(KakMessage(
format!("Sorted {} selections", zipped.len()),
None,
))
}