Start work on sortable selections

This commit is contained in:
Austen Adler 2022-02-21 11:01:14 -05:00
parent b0e90f63b6
commit 04f9c177c5

View File

@ -34,6 +34,8 @@ enum Commands {
struct SortOptions { struct SortOptions {
#[clap(index = 1)] #[clap(index = 1)]
regex: Option<String>, regex: Option<String>,
#[clap(short = 's', long)]
subselections_register: Option<char>,
// TODO: Can we invert a boolean? This name is terrible // TODO: Can we invert a boolean? This name is terrible
#[clap(short = 'S', long)] #[clap(short = 'S', long)]
no_skip_whitespace: bool, no_skip_whitespace: bool,
@ -41,6 +43,13 @@ struct SortOptions {
lexicographic_sort: bool, lexicographic_sort: bool,
#[clap(short, long)] #[clap(short, long)]
reverse: bool, reverse: bool,
#[clap(short, long)]
ignore_case: bool,
}
struct SortableSelection {
content: &str,
subselections: Vec<&str>,
} }
fn main() { fn main() {
@ -86,7 +95,10 @@ fn run() -> Result<KakMessage, KakMessage> {
} }
} }
fn sort(sort_options: &SortOptions) -> Result<KakMessage, KakMessage> { fn sort_keys_simple<'a>(
sort_options: &SortOptions,
selections: &[&str],
) -> Result<Vec<SortableSelection>, KakMessage> {
let re = sort_options let re = sort_options
.regex .regex
.as_ref() .as_ref()
@ -99,29 +111,67 @@ fn sort(sort_options: &SortOptions) -> Result<KakMessage, KakMessage> {
) )
})?; })?;
let selections = read_selections()?; Ok(selections
let mut zipped = selections
.iter() .iter()
.zip( .map(|s| {
if sort_options.no_skip_whitespace {
SortableSelection{
content: s,
subselections: vec![s],
}
} else {
SortableSelection{
content: s,
subselections: vec![s.trim()],
}
}
})
.map(|(s, k)| {
let captures = re.as_ref()?.captures(k)?;
captures
.get(1)
.or_else(|| captures.get(0))
.map(|m| m.as_str())
})
.collect::<Vec<Option<&str>>>())
}
// fn sort_keys_subselection(sort_options: &SortOptions) -> Result<Vec<(&String, Option<&str>)>, KakMessage> {
// let sort_selections = kak_response("%val{selections}")?.iter_mut().map(|a| {
// if sort_options.no_skip_whitespace {
// a
// } else {
// a.trim()
// }
// });
// let sort_selections_desc = kak_response("%val{selections_desc}")?;
// kak_exec("z")?;
// let selections = kak_response("%val{selections}")?;
// let selections_desc = kak_response("%val{selections_desc}")?;
// }
fn sort(sort_options: &SortOptions) -> Result<KakMessage, KakMessage> {
// let selections = kak_response("%val{selections}")?;
// let sort_keys = if let Some(r) = sort_options.subselections_register {
// let selections_desc = kak_response("%val{selections_desc}")?;
// } else {
// };
let mut zipped = match sort_options.subselections_register {
Some(c) => {
let selections = kak_response("%val{selections}")?;
selections selections
.iter() .into_iter()
.map(|a| { .zip(sort_keys_simple(sort_options, &selections))
if sort_options.no_skip_whitespace { }
a None => {
} else { let selections = kak_response("%val{selections}")?;
a.trim() selections.iter().zip(selections.iter().map(|s| s.as_str()))
} }
}) };
.map(|a| {
let captures = re.as_ref()?.captures(a)?; // let mut zipped = sort_keys_simple(sort_options)?;
captures
.get(1)
.or_else(|| captures.get(0))
.map(|m| m.as_str())
}),
)
.collect::<Vec<(&String, Option<&str>)>>();
zipped.sort_by(|(a, a_key), (b, b_key)| { zipped.sort_by(|(a, a_key), (b, b_key)| {
let a = a_key.unwrap_or(a); let a = a_key.unwrap_or(a);
@ -151,21 +201,22 @@ fn sort(sort_options: &SortOptions) -> Result<KakMessage, KakMessage> {
write!(f, " ; exec R;")?; write!(f, " ; exec R;")?;
Ok(KakMessage( Ok(KakMessage(
format!("Sorted {} selections", selections.len()), format!("Sorted {} selections", zipped.len()),
None, None,
)) ))
} }
fn read_selections() -> Result<Vec<String>, KakMessage> { fn kak_exec(cmd: &str) -> Result<(), KakMessage> {
{ let mut f = open_command_fifo()?;
let mut f = open_command_fifo()?;
write!( write!(f, "{}", cmd).map_err(|e| e.into())
f, }
"echo -quoting shell -to-file {} -- %val{{selections}}",
get_var("kak_response_fifo")? fn kak_response(msg: &str) -> Result<Vec<String>, KakMessage> {
)?; kak_exec(&format!(
} "echo -quoting shell -to-file {} -- {msg}",
get_var("kak_response_fifo")?
))?;
let selections = shellwords::split(&fs::read_to_string(&get_var("kak_response_fifo")?)?)?; let selections = shellwords::split(&fs::read_to_string(&get_var("kak_response_fifo")?)?)?;