nanorust 0.1.0

A terminal-based text editor written in Rust, inspired by GNU nano
Documentation
/// Represents a single edit operation for undo/redo functionality
#[derive(Debug, Clone)]
pub enum Edit {
    /// Insert a character at a specific position
    InsertChar { row: usize, col: usize, ch: char },
    /// Delete a character at a specific position
    DeleteChar { row: usize, col: usize, ch: char },
    /// Insert a new line at a specific position
    InsertLine { row: usize, content: String },
    /// Delete a line at a specific position
    DeleteLine { row: usize, content: String },
    /// Split a line at a specific position
    SplitLine { row: usize, col: usize },
    /// Join two lines at a specific position
    JoinLines { row: usize },
}

/// Manages edit history for undo/redo operations
pub struct EditHistory {
    /// Stack of edits that can be undone
    undo_stack: Vec<Edit>,
    /// Stack of edits that can be redone
    redo_stack: Vec<Edit>,
}

impl EditHistory {
    /// Create a new empty edit history
    pub fn new() -> Self {
        Self {
            undo_stack: Vec::new(),
            redo_stack: Vec::new(),
        }
    }

    /// Push a new edit to the undo stack and clear the redo stack
    pub fn push(&mut self, edit: Edit) {
        self.undo_stack.push(edit);
        self.redo_stack.clear();
    }

    /// Pop an edit from the undo stack and push it to the redo stack
    /// Returns None if the undo stack is empty
    pub fn undo(&mut self) -> Option<Edit> {
        if let Some(edit) = self.undo_stack.pop() {
            self.redo_stack.push(edit.clone());
            Some(edit)
        } else {
            None
        }
    }

    /// Pop an edit from the redo stack and push it to the undo stack
    /// Returns None if the redo stack is empty
    pub fn redo(&mut self) -> Option<Edit> {
        if let Some(edit) = self.redo_stack.pop() {
            self.undo_stack.push(edit.clone());
            Some(edit)
        } else {
            None
        }
    }

    /// Clear the redo stack (called when new edits are made)
    pub fn clear_redo(&mut self) {
        self.redo_stack.clear();
    }
}

impl Default for EditHistory {
    fn default() -> Self {
        Self::new()
    }
}