Skip to content

Commit

Permalink
IMPACT: remove offset from matrixify
Browse files Browse the repository at this point in the history
  • Loading branch information
ynqa committed May 4, 2024
1 parent 3516804 commit cbe126e
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 51 deletions.
2 changes: 1 addition & 1 deletion promkit/src/core/checkbox/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl PaneFactory for State {
}
})
.fold((vec![], 0), |(mut acc, pos), item| {
let rows = item.matrixify(width as usize, height, 0).0;
let rows = item.matrixify(width as usize, height, 0);
if pos < self.checkbox.position() + height {
acc.extend(rows);
}
Expand Down
2 changes: 1 addition & 1 deletion promkit/src/core/json/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ impl PaneFactory for State {
}
})
.fold((vec![], 0), |(mut acc, pos), item| {
let rows = item.matrixify(width as usize, height, 0).0;
let rows = item.matrixify(width as usize, height, 0);
if pos < self.stream.cursor.cross_contents_position() + height {
acc.extend(rows);
}
Expand Down
2 changes: 1 addition & 1 deletion promkit/src/core/listbox/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl PaneFactory for State {
}
})
.fold((vec![], 0), |(mut acc, pos), item| {
let rows = item.matrixify(width as usize, height, 0).0;
let rows = item.matrixify(width as usize, height, 0);
if pos < self.listbox.position() + height {
acc.extend(rows);
}
Expand Down
2 changes: 1 addition & 1 deletion promkit/src/core/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl State {

impl PaneFactory for State {
fn create_pane(&self, width: u16, height: u16) -> Pane {
let (matrix, _) = StyledGraphemes::from_str(&self.text, self.style).matrixify(
let matrix = StyledGraphemes::from_str(&self.text, self.style).matrixify(
width as usize,
height as usize,
0,
Expand Down
22 changes: 16 additions & 6 deletions promkit/src/core/text_editor/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ impl PaneFactory for State {
fn create_pane(&self, width: u16, height: u16) -> Pane {
let mut buf = StyledGraphemes::default();

let mut styled_prefix = StyledGraphemes::from_str(&self.prefix, self.prefix_style);

buf.append(&mut styled_prefix);
buf.append(&mut StyledGraphemes::from_str(
&self.prefix,
self.prefix_style,
));

let text = match self.mask {
Some(mask) => self.texteditor.masking(mask),
Expand All @@ -55,12 +56,21 @@ impl PaneFactory for State {
None => height as usize,
};

let (matrix, offset) = buf.matrixify(
let matrix = buf.matrixify(
width as usize,
height,
(styled_prefix.widths() + self.texteditor.position()) / width as usize,
(StyledGraphemes::from_str(&self.prefix, self.prefix_style).widths()
+ self.texteditor.position())
/ width as usize,
);

Pane::new(matrix, offset)
Pane::new(
matrix
.iter()
.enumerate()
.map(|(_, item)| item.clone())
.collect(),
0,
)
}
}
2 changes: 1 addition & 1 deletion promkit/src/core/tree/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl PaneFactory for State {
}
})
.fold((vec![], 0), |(mut acc, pos), item| {
let rows = item.matrixify(width as usize, height, 0).0;
let rows = item.matrixify(width as usize, height, 0);
if pos < self.tree.position() + height {
acc.extend(rows);
}
Expand Down
94 changes: 55 additions & 39 deletions promkit/src/grapheme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,40 +239,42 @@ impl StyledGraphemes {
&self,
width: usize,
height: usize,
offset: usize,
) -> (Vec<StyledGraphemes>, usize) {
let mut all = vec![];
mut offset: usize,
) -> Vec<StyledGraphemes> {
let mut all = VecDeque::new();
let mut row = StyledGraphemes::default();
for styled in self.iter() {
let width_with_next_char = row.iter().fold(0, |mut layout, g| {
layout += g.width;
layout
}) + styled.width;
if !row.is_empty() && width < width_with_next_char {
all.push(row);
all.push_back(row);
row = StyledGraphemes::default();
}
if width >= styled.width {
row.push_back(styled.clone());
}
}
if !row.is_empty() {
all.push(row);
all.push_back(row);
}

if all.is_empty() {
return (vec![], 0);
return vec![];
}

let chunks: Vec<Vec<StyledGraphemes>> =
all.chunks(height).map(|chunk| chunk.to_vec()).collect();

let chunk_index = std::cmp::min(offset / height, chunks.len().saturating_sub(1));
let selected_chunk = chunks.get(chunk_index).cloned().unwrap_or_default();

let local_offset = offset % height;
// Adjust the start and end rows based on the offset and height
while all.len() > height && offset < all.len() {
if offset > 0 {
all.pop_front();
offset -= 1;
} else {
all.pop_back();
}
}

(selected_chunk, local_offset)
Vec::from(all)
}
}

Expand Down Expand Up @@ -474,47 +476,61 @@ mod test {
}
}

#[cfg(test)]
mod matrixify {
use super::*;

#[test]
fn test_with_single_line_no_offset() {
let input =
StyledGraphemes::from("Hello, world! This is a longer test without offset.");
let (matrix, offset) = input.matrixify(50, 1, 0);
fn test_with_empty_input() {
let input = StyledGraphemes::default();
let matrix = input.matrixify(10, 2, 0);
assert!(matrix.is_empty());
}

#[test]
fn test_with_exact_width_fit() {
let input = StyledGraphemes::from("1234567890");
let matrix = input.matrixify(10, 1, 0);
assert_eq!(1, matrix.len());
assert_eq!(
"Hello, world! This is a longer test without offset",
matrix[0].to_string()
);
assert_eq!(0, offset);
assert_eq!("1234567890", matrix[0].to_string());
}

#[test]
fn test_with_multiple_lines_and_offset() {
let input = StyledGraphemes::from("One Two Three Four Five Six Seven Eight Nine Ten");
let (matrix, offset) = input.matrixify(10, 3, 10);
fn test_with_narrow_width() {
let input = StyledGraphemes::from("1234567890");
let matrix = input.matrixify(5, 2, 0);
assert_eq!(2, matrix.len());
assert_eq!("ven Eight ", matrix[0].to_string());
assert_eq!("Nine Ten", matrix[1].to_string());
assert_eq!(1, offset);
assert_eq!("12345", matrix[0].to_string());
assert_eq!("67890", matrix[1].to_string());
}

#[test]
fn test_with_empty_input() {
let input = StyledGraphemes::default();
let (matrix, offset) = input.matrixify(10, 2, 0);
assert!(matrix.is_empty());
assert_eq!(0, offset);
fn test_with_offset() {
let input = StyledGraphemes::from("1234567890");
let matrix = input.matrixify(2, 2, 1);
assert_eq!(2, matrix.len());
assert_eq!("34", matrix[0].to_string());
assert_eq!("56", matrix[1].to_string());
}

#[test]
fn test_with_padding() {
let input = StyledGraphemes::from("1234567890");
let matrix = input.matrixify(2, 100, 1);
assert_eq!(5, matrix.len());
assert_eq!("12", matrix[0].to_string());
assert_eq!("34", matrix[1].to_string());
assert_eq!("56", matrix[2].to_string());
assert_eq!("78", matrix[3].to_string());
assert_eq!("90", matrix[4].to_string());
}

#[test]
fn test_with_large_offset_beyond_content() {
let input = StyledGraphemes::from("Short text");
let (matrix, offset) = input.matrixify(10, 2, 20);
fn test_with_large_offset() {
let input = StyledGraphemes::from("1234567890");
let matrix = input.matrixify(10, 2, 100); // Offset beyond content
assert_eq!(1, matrix.len());
assert_eq!("Short text", matrix[0].to_string());
assert_eq!(0, offset);
assert_eq!("1234567890", matrix[0].to_string());
}
}
}
2 changes: 1 addition & 1 deletion promkit/src/pane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ mod test {
assert_eq!(
true,
Pane {
layout: StyledGraphemes::from("").matrixify(10, 10, 0).0,
layout: StyledGraphemes::from("").matrixify(10, 10, 0),
offset: 0,
}
.is_empty()
Expand Down

0 comments on commit cbe126e

Please sign in to comment.