Skip to content

Commit

Permalink
improved error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Znackt committed Jul 11, 2024
1 parent 1d731df commit c24b6c7
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 23 deletions.
24 changes: 15 additions & 9 deletions src/editor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crossterm::event::{read, Event, Event::Key, KeyCode::Char, KeyEvent, KeyModifiers};
use std::io::Error;
mod terminal;
use terminal::Terminal;
use terminal::{Position, Size, Terminal};

pub struct Editor {
should_quit: bool,
}
Expand All @@ -16,7 +18,7 @@ impl Editor {
result.unwrap();
}

fn repl(&mut self) -> Result<(), std::io::Error> {
fn repl(&mut self) -> Result<(), Error> {
loop {
self.refresh_screen()?;
if self.should_quit {
Expand All @@ -40,22 +42,26 @@ impl Editor {
}
}
}
fn refresh_screen(&self) -> Result<(), std::io::Error> {
fn refresh_screen(&self) -> Result<(), Error> {
Terminal::hide_cursor()?;
if self.should_quit {
Terminal::clear_screen()?;
print!("Goodbye.\r\n");
Terminal::print("Goodbye.\r\n")?;
} else {
Self::draw_rows()?;
Terminal::move_cursor_to(0, 0)?;
Terminal::move_cursor_to(Position { x: 0, y: 0 })?;
}
Terminal::show_cursor()?;
Terminal::execute()?;
Ok(())
}
fn draw_rows() -> Result<(), std::io::Error> {
let height = Terminal::size()?.1;
fn draw_rows() -> Result<(), Error> {
let Size { height, .. } = Terminal::size()?;
for current_row in 0..height {
print!("~");
Terminal::clear_line()?;
Terminal::print("~")?;
if current_row + 1 < height {
print!("\r\n");
Terminal::print("\r\n")?;
}
}
Ok(())
Expand Down
71 changes: 58 additions & 13 deletions src/editor/terminal.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,75 @@
use crossterm::cursor::MoveTo;
use crossterm::execute;
use crossterm::cursor::{Hide, MoveTo, Show};
use crossterm::queue;
use crossterm::style::Print;
use crossterm::terminal::{disable_raw_mode, enable_raw_mode, size, Clear, ClearType};
use std::io::stdout;
use std::io::{stdout, Error, Write};

pub struct Terminal {}
#[derive(Copy, Clone)]
pub struct Size {
pub height: u16,
pub width: u16,

Check warning on line 10 in src/editor/terminal.rs

View workflow job for this annotation

GitHub Actions / Check

field `width` is never read

Check failure on line 10 in src/editor/terminal.rs

View workflow job for this annotation

GitHub Actions / Lints

field `width` is never read

Check warning on line 10 in src/editor/terminal.rs

View workflow job for this annotation

GitHub Actions / Test Suite

field `width` is never read
}

#[derive(Copy, Clone)]
pub struct Position {
pub x: u16,
pub y: u16,
}

pub struct Terminal;

impl Terminal {
pub fn terminate() -> Result<(), std::io::Error> {
pub fn terminate() -> Result<(), Error> {
Self::execute()?;
disable_raw_mode()?;
Ok(())
}
pub fn initialize() -> Result<(), std::io::Error> {

pub fn initialize() -> Result<(), Error> {
enable_raw_mode()?;
Self::clear_screen()?;
Self::move_cursor_to(0, 0)?;
Self::move_cursor_to(Position { x: 0, y: 0 })?;
Self::execute()?;
Ok(())
}

pub fn clear_screen() -> Result<(), Error> {
queue!(stdout(), Clear(ClearType::All))?;
Ok(())
}
pub fn clear_screen() -> Result<(), std::io::Error> {
execute!(stdout(), Clear(ClearType::All))?;

pub fn clear_line() -> Result<(), Error> {
queue!(stdout(), Clear(ClearType::CurrentLine))?;
Ok(())
}
pub fn move_cursor_to(x: u16, y: u16) -> Result<(), std::io::Error> {
execute!(stdout(), MoveTo(x, y))?;

pub fn move_cursor_to(position: Position) -> Result<(), Error> {
queue!(stdout(), MoveTo(position.x, position.y))?;
Ok(())
}
pub fn size() -> Result<(u16, u16), std::io::Error> {
size()

pub fn hide_cursor() -> Result<(), Error> {
queue!(stdout(), Hide)?;
Ok(())
}

pub fn show_cursor() -> Result<(), Error> {
queue!(stdout(), Show)?;
Ok(())
}

pub fn print(string: &str) -> Result<(), Error> {
queue!(stdout(), Print(string))?;
Ok(())
}

pub fn size() -> Result<Size, Error> {
let (width, height) = size()?;
Ok(Size { height, width })
}

pub fn execute() -> Result<(), Error> {
stdout().flush()?;
Ok(())
}
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![warn(clippy::all, clippy::pedantic)]
#![warn(clippy::all, clippy::pedantic, clippy::print_stdout)]
mod editor;
use editor::Editor;
fn main() {
Expand Down

0 comments on commit c24b6c7

Please sign in to comment.