Skip to content

Commit

Permalink
gap buffer p1
Browse files Browse the repository at this point in the history
  • Loading branch information
Kacperacy committed Jun 10, 2024
1 parent 5e376e0 commit f9dfd8e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/gap_buffer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
struct GapBuffer {
buffer: Vec<char>,
gap_start: usize,
gap_end: usize,
}

impl GapBuffer {
fn new(capacity: usize) -> GapBuffer {
GapBuffer {
buffer: vec![' '; capacity],
gap_start: 0,
gap_end: capacity,
}
}

fn insert(&mut self, c: char) {
if self.gap_start == self.gap_end {
self.resize();
}
self.buffer[self.gap_start] = c;
self.gap_start += 1;
}

fn resize(&mut self) {
let new_capacity = self.buffer.len() * 2;
let mut new_buffer = vec![' '; new_capacity];
let gap_size = self.gap_end - self.gap_start;

new_buffer[..self.gap_start].copy_from_slice(&self.buffer[..self.gap_start]);
new_buffer[new_capacity - gap_size..].copy_from_slice(&self.buffer[self.gap_end..]);

self.gap_end = new_capacity - gap_size;
self.buffer = new_buffer;
}

fn delete(&mut self) {
if self.gap_start > 0 {
self.gap_start -= 1;
}
}

fn move_cursor(&mut self, index: usize) {
if index < self.gap_start {}
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ pub mod ui;
pub mod tui;

pub mod handler;

pub mod gap_buffer;

0 comments on commit f9dfd8e

Please sign in to comment.