| 1 |
use std::io::{Write, stdout}; |
| 2 |
|
| 3 |
use anyhow::Ok; |
| 4 |
use crossterm::{ExecutableCommand, QueueableCommand, cursor, event::{self, read}, style::{self, Color, Stylize}, terminal}; |
| 5 |
|
| 6 |
use crate::{buffer::Buffer}; |
| 7 |
|
| 8 |
enum Action { |
| 9 |
Quit, |
| 10 |
|
| 11 |
MoveUp, |
| 12 |
MoveDown, |
| 13 |
MoveLeft, |
| 14 |
MoveRight, |
| 15 |
|
| 16 |
AddChar(char), |
| 17 |
NewLine, |
| 18 |
|
| 19 |
EnterMode(Mode), |
| 20 |
} |
| 21 |
|
| 22 |
#[derive(Debug)] |
| 23 |
enum Mode { |
| 24 |
Normal, |
| 25 |
Insert, |
| 26 |
} |
| 27 |
|
| 28 |
pub struct Editor { |
| 29 |
stdout: std::io::Stdout, |
| 30 |
buffer: Buffer, |
| 31 |
size: (u16, u16), |
| 32 |
vtop: u16, |
| 33 |
vleft: u16, |
| 34 |
cx: u16, |
| 35 |
cy: u16, |
| 36 |
mode: Mode, |
| 37 |
} |
| 38 |
|
| 39 |
impl Editor { |
| 40 |
pub fn new(buffer: Buffer) -> anyhow::Result<Self> { |
| 41 |
let mut stdout = stdout(); |
| 42 |
terminal::enable_raw_mode()?; |
| 43 |
stdout |
| 44 |
.execute(terminal::EnterAlternateScreen)? |
| 45 |
.execute(terminal::Clear(terminal::ClearType::All))?; |
| 46 |
|
| 47 |
Ok(Editor { |
| 48 |
stdout, |
| 49 |
buffer, |
| 50 |
size: terminal::size()?, |
| 51 |
vtop: 0, |
| 52 |
vleft: 0, |
| 53 |
cx: 0, |
| 54 |
cy: 0, |
| 55 |
mode: Mode::Normal, |
| 56 |
}) |
| 57 |
} |
| 58 |
|
| 59 |
pub fn vwidth(&self) -> u16 { |
| 60 |
self.size.0 |
| 61 |
} |
| 62 |
|
| 63 |
pub fn vheight(&self) -> u16 { |
| 64 |
self.size.1 - 2 |
| 65 |
} |
| 66 |
|
| 67 |
pub fn viewport_line(&self, n: u16) -> Option<String> { |
| 68 |
let buffer_line = self.vtop + n; |
| 69 |
self.buffer.get(buffer_line as usize) |
| 70 |
} |
| 71 |
|
| 72 |
pub fn draw(&mut self) -> anyhow::Result<()> { |
| 73 |
self.draw_viewport()?; |
| 74 |
self.draw_statuline()?; |
| 75 |
self.stdout.queue(cursor::MoveTo(self.cx, self.cy))?; |
| 76 |
self.stdout.flush()?; |
| 77 |
|
| 78 |
Ok(()) |
| 79 |
} |
| 80 |
|
| 81 |
pub fn draw_viewport(&mut self) -> anyhow::Result<()> { |
| 82 |
let vwidth = self.vwidth() as usize; |
| 83 |
for i in 0..self.vheight() { |
| 84 |
let line = match self.viewport_line(i) { |
| 85 |
None => String::new(), |
| 86 |
Some(s) => s, |
| 87 |
}; |
| 88 |
|
| 89 |
self.stdout |
| 90 |
.queue(cursor::MoveTo(0, i))? |
| 91 |
.queue(style::Print(format!("{line:<width$}", width = vwidth)))?; |
| 92 |
} |
| 93 |
Ok(()) |
| 94 |
} |
| 95 |
|
| 96 |
pub fn draw_statuline(&mut self) -> anyhow::Result<()> { |
| 97 |
|
| 98 |
let mode = format!(" {:?} ", self.mode).to_uppercase(); |
| 99 |
let file = format!(" {} ", self.buffer.file.as_deref().unwrap_or("No Name")); |
| 100 |
let pos = format!(" {}:{} ", self.cy, self.cx); |
| 101 |
|
| 102 |
let file_width = self.size.0 - mode.len() as u16 - pos.len() as u16 - 2; |
| 103 |
|
| 104 |
self.stdout.queue(cursor::MoveTo(0, self.size.1 -2))?; |
| 105 |
self.stdout.queue(style::PrintStyledContent( |
| 106 |
mode.with(Color::Black).on(Color::White).bold(), |
| 107 |
))?; |
| 108 |
self.stdout.queue(style::PrintStyledContent( |
| 109 |
"".with(Color::White).on(Color::Grey) |
| 110 |
))?; |
| 111 |
self.stdout.queue(style::PrintStyledContent( |
| 112 |
format!("{:<width$}", file, width = file_width as usize).with(Color::Black).on(Color::Grey) |
| 113 |
))?; |
| 114 |
self.stdout.queue(style::PrintStyledContent( |
| 115 |
"".with(Color::White).on(Color::Grey) |
| 116 |
))?; |
| 117 |
self.stdout.queue(style::PrintStyledContent( |
| 118 |
pos.with(Color::Black).on(Color::White).bold(), |
| 119 |
))?; |
| 120 |
|
| 121 |
Ok(()) |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
pub fn run(&mut self) -> anyhow::Result<()> { |
| 126 |
loop { |
| 127 |
self.draw()?; |
| 128 |
if let Some(action) = self.handle_event(read()?)? { |
| 129 |
match action { |
| 130 |
Action::Quit => break, |
| 131 |
Action::MoveUp => { |
| 132 |
self.cy = self.cy.saturating_sub(1) |
| 133 |
}, |
| 134 |
Action::MoveDown => { |
| 135 |
self.cy += 1u16 |
| 136 |
}, |
| 137 |
Action::MoveLeft => { |
| 138 |
self.cx = self.cx.saturating_sub(1) |
| 139 |
}, |
| 140 |
Action::MoveRight => { |
| 141 |
self.cx += 1u16 |
| 142 |
}, |
| 143 |
Action::EnterMode(new_mode) => { |
| 144 |
self.mode = new_mode; |
| 145 |
}, |
| 146 |
Action::AddChar(c) => { |
| 147 |
self.stdout.queue(cursor::MoveTo(self.cx, self.cy))?; |
| 148 |
self.stdout.queue(style::Print(c))?; |
| 149 |
self.cx += 1; |
| 150 |
}, |
| 151 |
Action::NewLine => { |
| 152 |
self.cx = 0; |
| 153 |
self.cy += 1; |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
Ok(()) |
| 161 |
} |
| 162 |
|
| 163 |
fn handle_event(&mut self, ev: event::Event) -> anyhow::Result<Option<Action>> { |
| 164 |
if matches!(ev, event::Event::Resize(_, _)) { |
| 165 |
self.size = terminal::size()?; |
| 166 |
} |
| 167 |
|
| 168 |
match self.mode { |
| 169 |
Mode::Normal => self.handle_normal_event(ev), |
| 170 |
Mode::Insert => self.handle_insert_event(ev), |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
fn handle_normal_event(&self, ev: event::Event) -> anyhow::Result<Option<Action>> { |
| 175 |
let action = match ev { |
| 176 |
event::Event::Key(event) => match event.code { |
| 177 |
event::KeyCode::Char('q') => Ok(Some(Action::Quit)), |
| 178 |
event::KeyCode::Up | event::KeyCode::Char('k') => Ok(Some(Action::MoveUp)), |
| 179 |
event::KeyCode::Down | event::KeyCode::Char('j') => Ok(Some(Action::MoveDown)), |
| 180 |
event::KeyCode::Left | event::KeyCode::Char('h') => Ok(Some(Action::MoveLeft)), |
| 181 |
event::KeyCode::Right | event::KeyCode::Char('l') => Ok(Some(Action::MoveRight)), |
| 182 |
event::KeyCode::Char('i') => Ok(Some(Action::EnterMode(Mode::Insert))), |
| 183 |
_ => Ok(None), |
| 184 |
} |
| 185 |
_ => Ok(None), |
| 186 |
}; |
| 187 |
Ok(action)? |
| 188 |
} |
| 189 |
|
| 190 |
fn handle_insert_event(&self, ev: event::Event) -> anyhow::Result<Option<Action>> { |
| 191 |
let action = match ev { |
| 192 |
event::Event::Key(event) => match event.code { |
| 193 |
event::KeyCode::Esc => Ok(Some(Action::EnterMode(Mode::Normal))), |
| 194 |
event::KeyCode::Char(c) => Ok(Some(Action::AddChar(c))), |
| 195 |
event::KeyCode::Enter => Ok(Some(Action::NewLine)), |
| 196 |
_ => Ok(None), |
| 197 |
}, |
| 198 |
_ => Ok(None), |
| 199 |
}; |
| 200 |
Ok(action)? |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
impl Drop for Editor { |
| 205 |
fn drop(&mut self) { |
| 206 |
_ = self.stdout.flush(); |
| 207 |
_ = self.stdout.execute(terminal::LeaveAlternateScreen); |
| 208 |
_ = terminal::disable_raw_mode(); |
| 209 |
} |
| 210 |
} |