Compare commits

..

No commits in common. "00e49590552abc4b36166cf0f80391da7141eec2" and "7b7e307a51c45270c5f909fc49ad27c9d020b870" have entirely different histories.

10 changed files with 20 additions and 202 deletions

View File

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

View File

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

View File

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

View File

@ -2,13 +2,6 @@ use std::rc::Rc;
mod rope;
use rope::Rope;
pub use rope::{CharIterator, CharWithPointIterator};
mod reader;
pub use reader::TextBufferReader;
mod writer;
pub use writer::TextBufferWriter;
pub struct TextBuffer {
contents: Rc<Rope>,
@ -38,7 +31,7 @@ impl TextBuffer {
if num_chars == 0 {
0
} else {
self.contents.total_lines()
dbg!(self.contents.total_lines())
+ if self
.contents
.get_char_at_index(self.contents.total_chars() - 1)
@ -51,16 +44,6 @@ 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) {
match point {
Point::End => {
@ -70,18 +53,6 @@ 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 {
@ -170,29 +141,4 @@ mod tests {
assert_eq!(16, target.num_chars());
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

@ -1,91 +0,0 @@
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,6 +225,14 @@ 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
pub fn iter_chars(self: &Rc<Self>) -> CharIterator {
CharIterator::new(self)
@ -252,7 +260,7 @@ impl Rope {
}
/// Returns an iterater over the leaf nodes
pub fn iter_nodes(self: Rc<Self>) -> NodeIterator {
fn iter_nodes(self: Rc<Self>) -> NodeIterator {
NodeIterator::new(self)
}
}
@ -269,7 +277,7 @@ fn merge(leaf_nodes: &[Rc<Rope>]) -> Rc<Rope> {
}
}
pub struct NodeIterator {
struct NodeIterator {
stack: Vec<Rc<Rope>>,
}

View File

@ -45,6 +45,12 @@ 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 {
"aあbcdeえfghiいjklmnおopqrstuうvwxyz"
}

View File

@ -1,42 +0,0 @@
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);
}
}

View File

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

View File

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