Skip to content

Commit

Permalink
feat: improve cursor bounds checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrreadiness committed Jun 13, 2024
1 parent b5184df commit a90e5a2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
9 changes: 6 additions & 3 deletions termwiz/src/lineedit/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ impl LineEditBuffer {
self.cursor += text.len();
}

/// The cursor position is the byte index into the line UTF-8 bytes.
/// Panics: the cursor must be the first byte in a UTF-8 code point
/// sequence or the end of the provided line.
pub fn set_line_and_cursor(&mut self, line: &str, cursor: usize) {
assert!(
cursor <= line.len(),
"cursor {} is outside the byte length of the new line of length {}",
line.is_char_boundary(cursor),
"cursor {} is not a char boundary of the new line {}",
cursor,
line.len()
line
);
self.line = line.to_string();
self.cursor = cursor;
Expand Down
3 changes: 2 additions & 1 deletion termwiz/src/lineedit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,8 @@ impl<'term> LineEditor<'term> {
/// You don't normally need to call this unless you are defining
/// a custom editor operation on the line buffer contents.
/// The cursor position is the byte index into the line UTF-8 bytes.
/// Panics: the cursor must be within the bounds of the provided line.
/// Panics: the cursor must be the first byte in a UTF-8 code point
/// sequence or the end of the provided line.
pub fn set_line_and_cursor(&mut self, line: &str, cursor: usize) {
self.line.set_line_and_cursor(line, cursor);
}
Expand Down

0 comments on commit a90e5a2

Please sign in to comment.