#[derive(Debug, Clone, Default)]
#[allow(dead_code)]
pub(crate) struct InputBuffer {
text: String, cursor: usize, }
mod cursor;
mod edit;
mod view;
pub(crate) mod wrap;
#[cfg(test)]
mod input_tests;
#[allow(dead_code)]
fn char_to_byte_idx(text: &str, char_idx: usize) -> usize {
match text.char_indices().nth(char_idx) {
Some((idx, _)) => idx,
None => text.len(),
}
}
#[allow(dead_code)]
impl InputBuffer {
pub(crate) fn new() -> Self {
Self::default()
}
pub(crate) fn text(&self) -> &str {
&self.text
}
pub(crate) fn is_empty(&self) -> bool {
self.text.is_empty()
}
pub(crate) fn cursor(&self) -> usize {
self.cursor
}
pub(crate) fn clear(&mut self) {
self.text.clear();
self.cursor = 0;
}
pub(crate) fn set_text(&mut self, text: impl Into<String>) {
self.text = text.into();
self.cursor = self.text.chars().count();
}
pub(crate) fn insert_char(&mut self, c: char) {
let byte_idx = char_to_byte_idx(&self.text, self.cursor);
self.text.insert(byte_idx, c);
self.cursor += 1;
}
pub(crate) fn insert_str(&mut self, s: &str) {
let byte_idx = char_to_byte_idx(&self.text, self.cursor);
self.text.insert_str(byte_idx, s);
self.cursor += s.chars().count();
}
pub(crate) fn insert_newline(&mut self) {
self.insert_char('\n');
}
}