curd-editor 0.1.2

A tiny terminal text editor
use std::fs;

#[cfg(test)]
mod tests;

#[derive(Default)]
pub struct Document {
    pub lines: Vec<String>,
}

impl Document {
    pub fn open(path: &str) -> std::io::Result<Self> {
        let text = fs::read_to_string(path)?;
        let lines = text.lines().map(String::from).collect();
        Ok(Self { lines })
    }

    pub fn is_empty(&self) -> bool {
        self.lines.is_empty()
    }

    pub fn len(&self) -> usize {
        self.lines.len()
    }

    pub fn line_len(&self, y: usize) -> usize {
        match self.lines.get(y) {
            Some(line) => line.chars().count(),
            None => 0,
        }
    }
}