Add join command

This commit is contained in:
Austen Adler 2023-02-09 23:27:26 -05:00
parent caa3953f2c
commit 8ec3ab3338
4 changed files with 62 additions and 7 deletions

View File

@ -57,10 +57,7 @@ where
/// # Errors
///
/// Will return `Err` if command fifo could not be opened, read from, or written to
pub fn get_selections_desc_unordered<S>(keys: Option<S>) -> Result<Vec<SelectionDesc>, KakError>
where
S: AsRef<str>,
{
pub fn get_selections_desc_unordered(keys: Option<&str>) -> Result<Vec<SelectionDesc>, KakError> {
response("%val{selections_desc}", keys.as_ref())?
.iter()
.map(|sd| SelectionDesc::from_str(sd))

View File

@ -86,6 +86,46 @@ impl SelectionDesc {
s.right.row - s.left.row + 1
}
/// Gets the smallest selection that encompases both selections
///
/// ```rust
/// let sel1 = SelectionDesc {
/// left: AnchorPosition { row: 10, col: 16 },
/// right: AnchorPosition { row: 1, col: 14 },
/// };
/// let sel2 = SelectionDesc {
/// left: AnchorPosition { row: 64, col: 10 },
/// right: AnchorPosition { row: 1, col: 100 },
/// };
/// let expected_bounding = SelectionDesc {
/// left: AnchorPosition { row: 1, col: 14 },
/// right: AnchorPosition { row: 64, col: 27 },
/// };
/// assert_eq!(sel1.rev().bounding_selection(&sel1), sel1.sort());
/// assert_eq!(sel2.bounding_selection(&sel1), expected_bounding.sort());
/// assert_eq!(sel2.rev().bounding_selection(&sel1.rev()), expected_bounding.sort());
/// assert_eq!(sel2.rev().bounding_selection(&sel1), expected_bounding.sort());
/// ```
pub fn bounding_selection<SD>(&self, other: SD) -> Self
where
SD: AsRef<Self>,
{
// So left is the minimum and right is the maximum
let (a, b) = (self.sort(), other.as_ref().sort());
Self {
left: min(a.left, b.left),
right: max(a.right, b.right),
}
}
pub fn rev(&self) -> Self {
Self {
left: self.right,
right: self.left,
}
}
#[must_use]
pub fn sort(&self) -> Self {
if self.left < self.right {

14
src/join.rs Normal file
View File

@ -0,0 +1,14 @@
use kakplugin::{get_selections_desc_unordered, set_selections_desc, KakError};
#[derive(clap::StructOpt, Debug)]
pub struct Options;
pub fn join(_options: &Options) -> Result<String, KakError> {
set_selections_desc(
get_selections_desc_unordered(None)?
.into_iter()
.reduce(|acc, sd| acc.bounding_selection(sd)),
)?;
Ok(format!("Joined all selections"))
}

View File

@ -10,12 +10,13 @@
#![feature(array_chunks)]
mod box_;
mod rev;
mod errors;
mod incr;
mod invert;
mod join;
mod math_eval;
mod pad;
mod rev;
mod set;
mod shuf;
mod sort;
@ -70,8 +71,10 @@ enum Commands {
Decr(incr::Options),
#[clap(about = "Decrement selections")]
Incr(incr::Options),
#[clap(about = "Reverse selectinos")]
Rev(rev::Options)
#[clap(about = "Reverse selections")]
Rev(rev::Options),
#[clap(about = "Join selections")]
Join(join::Options),
}
fn main() {
@ -123,5 +126,6 @@ fn run() -> Result<String, KakError> {
Commands::Incr(o) => incr::incr(o, true),
Commands::Decr(o) => incr::incr(o, false),
Commands::Rev(o) => rev::rev(o),
Commands::Join(o) => join::join(o),
}
}