index : yed
VimLike Text Editor in Rust
about summary refs log tree commit diff

Diffstat (limited to 'src/editor.rs')

-rw-r--r-- src/editor.rs 18
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/src/editor.rs b/src/editor.rs
index 613c625..fcddf41 100644
--- a/src/editor.rs
+++ b/src/editor.rs
@@ -3,6 +3,8 @@
use anyhow::Ok;
use crossterm::{ExecutableCommand, QueueableCommand, cursor, event::{self, read}, style::{self, Color, Stylize}, terminal};

use crate::buffer::Buffer;

enum Action {
    Quit,

@@ -25,6 +27,7 @@

pub struct Editor {
    stdout: std::io::Stdout,
    buffer: Buffer,
    size: (u16, u16),
    cx: u16,
    cy: u16,
@@ -40,7 +43,7 @@
}

impl Editor {
    pub fn new() -> anyhow::Result<Self> {
    pub fn new(buffer: Buffer) -> anyhow::Result<Self> {
        let mut stdout = stdout();
        terminal::enable_raw_mode()?;
        stdout
@@ -48,6 +51,7 @@
            .execute(terminal::Clear(terminal::ClearType::All))?;
        Ok(Editor {
            stdout,
            buffer,
            size: terminal::size()?,
            cx: 0,
            cy: 0,
@@ -56,9 +60,19 @@
    }

    pub fn draw(&mut self) -> anyhow::Result<()> {
        self.draw_buffer()?;
        self.draw_statuline()?;
        self.stdout.queue(cursor::MoveTo(self.cx, self.cy))?;
        self.stdout.flush()?;

        Ok(())
    }

    pub fn draw_buffer(&mut self) -> anyhow::Result<()> {
        for (i, line) in self.buffer.lines.iter().enumerate() {
            self.stdout.queue(cursor::MoveTo(0, i as u16))?;
            self.stdout.queue(style::Print(line))?;
        }

        Ok(())
    }
@@ -66,7 +80,7 @@
    pub fn draw_statuline(&mut self) -> anyhow::Result<()> {
        // let (bg, fg) = ("#fff", "#000");
        let mode = format!(" {:?} ", self.mode).to_uppercase();
        let file = " src/main.rs ";
        let file = format!(" {} ", self.buffer.file.as_deref().unwrap_or("No Name"));
        let pos = format!(" {}:{} ", self.cy, self.cx);

        let file_width = self.size.0 - mode.len() as u16 - pos.len() as u16 - 2;