Skip to content

Commit

Permalink
Pass char width to segments predicate closure
Browse files Browse the repository at this point in the history
  • Loading branch information
ku1ik committed Mar 7, 2024
1 parent d8d0571 commit ed4cfc0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
18 changes: 10 additions & 8 deletions src/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ impl Line {
}

pub fn segments(&self) -> impl Iterator<Item = Segment> + '_ {
self.group(|_c| false)
self.group(|_c, _w| false)
}

pub fn group<'a>(
&'a self,
predicate: impl Fn(&char) -> bool + 'a,
predicate: impl Fn(&char, usize) -> bool + 'a,
) -> impl Iterator<Item = Segment> + '_ {
Segments::new(self.cells.iter(), predicate)
}
Expand Down Expand Up @@ -170,7 +170,7 @@ impl Line {
struct Segments<'a, I, F>
where
I: Iterator<Item = &'a Cell>,
F: Fn(&char) -> bool,
F: Fn(&char, usize) -> bool,
{
iter: I,
current: Option<Segment>,
Expand All @@ -179,7 +179,7 @@ where
predicate: F,
}

impl<'a, I: Iterator<Item = &'a Cell>, F: Fn(&char) -> bool> Segments<'a, I, F> {
impl<'a, I: Iterator<Item = &'a Cell>, F: Fn(&char, usize) -> bool> Segments<'a, I, F> {
fn new(iter: I, predicate: F) -> Self {
Self {
iter,
Expand All @@ -191,7 +191,9 @@ impl<'a, I: Iterator<Item = &'a Cell>, F: Fn(&char) -> bool> Segments<'a, I, F>
}
}

impl<'a, I: Iterator<Item = &'a Cell>, F: Fn(&char) -> bool> Iterator for Segments<'a, I, F> {
impl<'a, I: Iterator<Item = &'a Cell>, F: Fn(&char, usize) -> bool> Iterator
for Segments<'a, I, F>
{
type Item = Segment;

fn next(&mut self) -> Option<Self::Item> {
Expand All @@ -204,7 +206,7 @@ impl<'a, I: Iterator<Item = &'a Cell>, F: Fn(&char) -> bool> Iterator for Segmen
let offset = self.offset;
self.offset += char_width;

if (self.predicate)(&cell.0) {
if (self.predicate)(&cell.0, char_width) {
let ready = Some(Segment {
chars: vec![cell.0],
pen: cell.1,
Expand Down Expand Up @@ -305,7 +307,7 @@ mod tests {
Cell('f', pen1),
];

let segments: Vec<Segment> = Segments::new(cells.iter(), |_| false).collect();
let segments: Vec<Segment> = Segments::new(cells.iter(), |_, _| false).collect();

assert_eq!(&segments[0].chars, &['a', 'b']);
assert_eq!(segments[0].pen, pen1);
Expand Down Expand Up @@ -345,7 +347,7 @@ mod tests {
Cell('e', pen2),
];

let segments: Vec<Segment> = Segments::new(cells.iter(), |c| c == &'c').collect();
let segments: Vec<Segment> = Segments::new(cells.iter(), |c, _w| c == &'c').collect();

assert_eq!(&segments[0].chars, &['a', 'b']);
assert_eq!(segments[0].pen, pen1);
Expand Down
2 changes: 1 addition & 1 deletion src/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct Segment {
pub(crate) chars: Vec<char>,
pub(crate) pen: Pen,
pub(crate) offset: usize,
#[serde(skip)]
#[serde(rename = "charWidth")]
pub(crate) char_width: usize,
}

Expand Down

0 comments on commit ed4cfc0

Please sign in to comment.