Compare commits

..

9 Commits

Author SHA1 Message Date
Matthew Gordon 00e4959055 Remove unused code 2025-11-10 13:24:14 -04:00
Matthew Gordon e01210f679 Add TextBufferWriter 2025-11-10 13:24:04 -04:00
Matthew Gordon d95ab09180 Create io::Reader instance for TextBuffer 2025-11-10 12:33:07 -04:00
Matthew Gordon 01b1ed1591 Test adding strings at end of TextBuffer 2025-11-10 12:32:15 -04:00
Matthew Gordon 4ca32decc9 Remove unused function Rope::is_leaf() 2025-10-30 21:49:37 -03:00
Matthew Gordon ef375f6472 Remove old dbg!() 2025-10-30 21:49:07 -03:00
Matthew Gordon 529ac555ec Expose some Rope functions through TextBuffer
This is mostly just to silence unused code warnings for now.
2025-10-30 21:48:10 -03:00
Matthew Gordon 0ae501d150 Change Rust edition to 2024 2025-10-30 21:46:29 -03:00
Matthew Gordon c9aa5290e9 Add stub TUI app 2025-10-30 20:08:50 -03:00
10 changed files with 202 additions and 20 deletions

View File

@ -1,3 +1,3 @@
[workspace] [workspace]
resolver = "2" resolver = "2"
members = ["core"] members = ["core", "tui"]

View File

@ -1,7 +1,7 @@
[package] [package]
name = "ged-core" name = "ged-core"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2024"
[dev-dependencies] [dev-dependencies]
ntest = "0.9.3" ntest = "0.9.3"

View File

@ -1,2 +1,2 @@
mod text_buffer; mod text_buffer;
pub use text_buffer::TextBuffer; pub use text_buffer::{TextBuffer, TextBufferReader, TextBufferWriter};

View File

@ -2,6 +2,13 @@ use std::rc::Rc;
mod rope; mod rope;
use rope::Rope; use rope::Rope;
pub use rope::{CharIterator, CharWithPointIterator};
mod reader;
pub use reader::TextBufferReader;
mod writer;
pub use writer::TextBufferWriter;
pub struct TextBuffer { pub struct TextBuffer {
contents: Rc<Rope>, contents: Rc<Rope>,
@ -31,7 +38,7 @@ impl TextBuffer {
if num_chars == 0 { if num_chars == 0 {
0 0
} else { } else {
dbg!(self.contents.total_lines()) self.contents.total_lines()
+ if self + if self
.contents .contents
.get_char_at_index(self.contents.total_chars() - 1) .get_char_at_index(self.contents.total_chars() - 1)
@ -44,6 +51,16 @@ impl TextBuffer {
} }
} }
pub fn insert_text(&mut self, text: impl Into<String>, point: Point) {
match point {
Point::End => {
self.contents = self
.contents
.insert_at_char_index(self.contents.total_chars(), text);
}
}
}
pub fn insert_char(&mut self, c: char, point: Point) { pub fn insert_char(&mut self, c: char, point: Point) {
match point { match point {
Point::End => { Point::End => {
@ -53,6 +70,18 @@ impl TextBuffer {
} }
} }
} }
pub fn delete_at_char_index(&mut self, start: usize, length: usize) {
self.contents = self.contents.delete_at_char_index(start, length)
}
pub fn iter_chars(&self) -> CharIterator {
self.contents.iter_chars()
}
pub fn iter_chars_with_point(&self) -> CharWithPointIterator {
self.contents.iter_chars_with_point()
}
} }
impl Default for TextBuffer { impl Default for TextBuffer {
@ -141,4 +170,29 @@ mod tests {
assert_eq!(16, target.num_chars()); assert_eq!(16, target.num_chars());
assert_eq!(5, target.num_lines()); assert_eq!(5, target.num_lines());
} }
#[test]
fn insert_text_at_end_increases_counts_as_expected() {
let mut target = TextBuffer::new();
target.insert_text("The", Point::End);
assert_eq!(3, target.num_bytes());
assert_eq!(3, target.num_chars());
assert_eq!(1, target.num_lines());
target.insert_text(" ", Point::End);
assert_eq!(4, target.num_bytes());
assert_eq!(4, target.num_chars());
assert_eq!(1, target.num_lines());
target.insert_text("quick brown", Point::End);
assert_eq!(15, target.num_bytes());
assert_eq!(15, target.num_chars());
assert_eq!(1, target.num_lines());
target.insert_text(" fox\n", Point::End);
assert_eq!(20, target.num_bytes());
assert_eq!(20, target.num_chars());
assert_eq!(1, target.num_lines());
target.insert_text("jumps over the lazy 猫.", Point::End);
assert_eq!(44, target.num_bytes());
assert_eq!(42, target.num_chars());
assert_eq!(2, target.num_lines());
}
} }

View File

@ -0,0 +1,91 @@
use std::rc::Rc;
use super::{
TextBuffer,
rope::{NodeIterator, Rope},
};
pub struct TextBufferReader {
node_iterator: NodeIterator,
current_node: Option<Rc<Rope>>,
cursor: usize,
}
impl TextBufferReader {
pub fn new(text_buffer: &TextBuffer) -> Self {
let mut node_iterator = Rc::clone(&text_buffer.contents).iter_nodes();
let current_node = node_iterator.next();
Self {
node_iterator,
current_node,
cursor: 0,
}
}
}
impl std::io::Read for TextBufferReader {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
let mut bytes_written = 0;
while let Some(rope) = self.current_node.clone() {
if let Rope::Leaf { text } = rope.as_ref() {
let bytes = text.as_bytes();
let length = bytes
.len()
.min(buf.len() - bytes_written)
.min(bytes.len() - self.cursor);
buf[bytes_written..bytes_written + length]
.copy_from_slice(&bytes[self.cursor..self.cursor + length]);
bytes_written += length;
self.cursor += length;
if self.cursor == bytes.len() {
self.current_node = self.node_iterator.next();
self.cursor = 0;
}
} else {
// Should always be leaf node
panic!();
}
if bytes_written == buf.len() {
break;
}
}
Ok(bytes_written)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::text_buffer::Point;
use std::io::Read;
#[test]
fn test_read_string() {
let text_fragments = [
"Sed ut perspiciatis unde omnis iste natus error sit voluptatem ",
"accusantium doloremque laudantium, totam rem aperiam, eaque ",
"ipsa quae ab illo inventore veritatis et quasi architecto beatae ",
"vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia ",
"voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur ",
"magni dolores eos qui ratione voluptatem sequi nesciunt. Neque ",
"porro quisquam est, qui dolorem ipsum quia dolor sit amet, ",
"consectetur, adipisci velit, sed quia non numquam eius modi tempora ",
"incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut ",
"enim ad minima veniam, quis nostrum exercitationem ullam ",
"corporis suscipit laboriosam, nisi ut aliquid ex ea commodi ",
"consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate ",
"velit esse quam nihil molestiae consequatur, vel illum qui dolorem ",
"eum fugiat quo voluptas nulla pariatur?",
];
let mut text_buffer = TextBuffer::new();
for fragment in text_fragments {
text_buffer.insert_text(fragment, Point::End);
}
let mut target = TextBufferReader::new(&text_buffer);
let mut bytes_buffer = Vec::new();
target.read_to_end(&mut bytes_buffer).unwrap();
let target_string = String::from_utf8(bytes_buffer).unwrap();
let expected_string: String = text_fragments.iter().flat_map(|s| s.chars()).collect();
assert_eq!(target_string, expected_string);
}
}

View File

@ -225,14 +225,6 @@ impl Rope {
} }
} }
/// Returns true if this node is a leaf node, false otherwise
pub fn is_leaf(&self) -> bool {
match &self {
Rope::Branch { .. } => false,
Rope::Leaf { .. } => true,
}
}
/// Returns an iterator over the chars of the text /// Returns an iterator over the chars of the text
pub fn iter_chars(self: &Rc<Self>) -> CharIterator { pub fn iter_chars(self: &Rc<Self>) -> CharIterator {
CharIterator::new(self) CharIterator::new(self)
@ -260,7 +252,7 @@ impl Rope {
} }
/// Returns an iterater over the leaf nodes /// Returns an iterater over the leaf nodes
fn iter_nodes(self: Rc<Self>) -> NodeIterator { pub fn iter_nodes(self: Rc<Self>) -> NodeIterator {
NodeIterator::new(self) NodeIterator::new(self)
} }
} }
@ -277,7 +269,7 @@ fn merge(leaf_nodes: &[Rc<Rope>]) -> Rc<Rope> {
} }
} }
struct NodeIterator { pub struct NodeIterator {
stack: Vec<Rc<Rope>>, stack: Vec<Rc<Rope>>,
} }

View File

@ -45,12 +45,6 @@ fn small_test_rope_with_multibyte_chars() -> Rc<Rope> {
) )
} }
fn small_test_rope_with_multibyte_chars_leaf_strings() -> Vec<&'static str> {
vec![
"aあbc", "deえf", "g", "hiい", "jklm", "n", "おop", "qr", "stuうv", "wxyz",
]
}
fn small_test_rope_with_multibyte_chars_full_string() -> &'static str { fn small_test_rope_with_multibyte_chars_full_string() -> &'static str {
"aあbcdeえfghiいjklmnおopqrstuうvwxyz" "aあbcdeえfghiいjklmnおopqrstuうvwxyz"
} }

View File

@ -0,0 +1,42 @@
use super::{Point, TextBuffer};
pub struct TextBufferWriter<'a> {
text_buffer: &'a mut TextBuffer,
}
impl<'a> TextBufferWriter<'a> {
pub fn new(text_buffer: &'a mut TextBuffer) -> Self {
Self { text_buffer }
}
}
impl<'a> std::io::Write for TextBufferWriter<'a> {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.text_buffer.insert_text(
str::from_utf8(buf).map_err(std::io::Error::other)?,
Point::End,
);
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;
#[test]
fn write_string_into_text_buffer() {
let text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
let mut text_buffer = TextBuffer::new();
let mut target = TextBufferWriter::new(&mut text_buffer);
target.write_all(text.as_bytes()).unwrap();
let output_text: String = text_buffer.iter_chars().collect();
assert_eq!(text, output_text);
}
}

6
tui/Cargo.toml Normal file
View File

@ -0,0 +1,6 @@
[package]
name = "ged-tui"
version = "0.1.0"
edition = "2024"
[dependencies]

3
tui/src/main.rs Normal file
View File

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}