Skip to content

Commit

Permalink
added better code readablity
Browse files Browse the repository at this point in the history
  • Loading branch information
Znackt committed Jul 9, 2024
1 parent 27e5df5 commit 4a9335b
Showing 1 changed file with 46 additions and 28 deletions.
74 changes: 46 additions & 28 deletions src/editor.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,63 @@
use crossterm::event::{read, Event::Key, KeyCode::Char, KeyEvent, KeyModifiers};
use crossterm::terminal::{disable_raw_mode, enable_raw_mode};
use crossterm::event::{read, Event, Event::Key, KeyCode::Char, KeyEvent, KeyModifiers};
mod terminal;

Check failure on line 2 in src/editor.rs

View workflow job for this annotation

GitHub Actions / Test Suite

file not found for module `terminal`

Check failure on line 2 in src/editor.rs

View workflow job for this annotation

GitHub Actions / Check

file not found for module `terminal`
use terminal::Terminal;

Check failure on line 3 in src/editor.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unresolved import `terminal::Terminal`

Check failure on line 3 in src/editor.rs

View workflow job for this annotation

GitHub Actions / Check

unresolved import `terminal::Terminal`
pub struct Editor {
should_quit: bool,
}

impl Editor {
pub fn default() -> Self {
Editor { should_quit: false }
pub const fn default() -> Self {
Self { should_quit: false }
}
pub fn run(&mut self) {
if let Err(err) = self.repl() {
panic!("{err:#?}");
}
print!("Goodbye.\r\n");
Terminal::initialize().unwrap();
let result = self.repl();
Terminal::terminate().unwrap();
result.unwrap();
}

fn repl(&mut self) -> Result<(), std::io::Error> {
enable_raw_mode()?;
loop {
if let Key(KeyEvent {
code,
modifiers,
kind,
state,
}) = read()?
{
println!(
"Code: {code:?} Modifiers: {modifiers:?} Kind: {kind:?} State: {state:?} \r"
);
match code {
Char('q') if modifiers == KeyModifiers::CONTROL => {
self.should_quit = true;
}
_ => (),
}
if self.should_quit {
break;
self.refresh_screen()?;
if self.should_quit {
break;
}
let event = read()?;
self.evaluate_event(&event);
}
Ok(())
}
fn evaluate_event(&mut self, event: &Event) {
if let Key(KeyEvent {
code, modifiers, ..
}) = event
{
match code {
Char('q') if *modifiers == KeyModifiers::CONTROL => {
self.should_quit = true;
}
_ => (),
}
}
}
fn refresh_screen(&self) -> Result<(), std::io::Error> {
if self.should_quit {
Terminal::clear_screen()?;
print!("Goodbye.\r\n");
} else {
Self::draw_rows()?;
Terminal::move_cursor_to(0, 0)?;
}
Ok(())
}
fn draw_rows() -> Result<(), std::io::Error> {
let height = Terminal::size()?.1;
for current_row in 0..height {
print!("~");
if current_row + 1 < height {
print!("\r\n");
}
}
disable_raw_mode()?;
Ok(())
}
}

0 comments on commit 4a9335b

Please sign in to comment.