2021-03-05 16:36:39 -05:00
|
|
|
use crate::Terminal;
|
2021-01-28 16:47:40 -05:00
|
|
|
use termion::event::Key;
|
2021-03-05 16:36:39 -05:00
|
|
|
|
|
|
|
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
2021-01-28 16:47:40 -05:00
|
|
|
|
2021-03-08 09:50:15 -05:00
|
|
|
pub struct Position {
|
|
|
|
pub x: usize,
|
|
|
|
pub y: usize,
|
|
|
|
}
|
|
|
|
|
2021-01-28 16:47:40 -05:00
|
|
|
pub struct Editor {
|
|
|
|
should_quit: bool,
|
2021-03-05 16:36:39 -05:00
|
|
|
terminal: Terminal,
|
2021-03-08 09:50:15 -05:00
|
|
|
cursor_position: Position,
|
2021-01-28 16:47:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Editor {
|
|
|
|
pub fn run(&mut self) {
|
|
|
|
loop {
|
2021-03-05 16:36:39 -05:00
|
|
|
if let Err(error) = self.refresh_screen() {
|
2021-01-28 16:47:40 -05:00
|
|
|
die(error);
|
|
|
|
}
|
|
|
|
if self.should_quit {
|
|
|
|
break;
|
|
|
|
}
|
2021-03-05 16:36:39 -05:00
|
|
|
if let Err(error) = self.process_keypress() {
|
|
|
|
die(error);
|
|
|
|
}
|
2021-01-28 16:47:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn default() -> Self {
|
2021-03-05 16:36:39 -05:00
|
|
|
Self {
|
|
|
|
should_quit: false,
|
|
|
|
terminal: Terminal::default().expect("Failed to initialize terminal"),
|
2021-03-08 09:50:15 -05:00
|
|
|
cursor_position: Position { x: 0, y: 0 }
|
2021-03-05 16:36:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn refresh_screen(&self) -> Result<(), std::io::Error> {
|
|
|
|
Terminal::cursor_hide();
|
2021-03-08 09:50:15 -05:00
|
|
|
Terminal::cursor_position(&Position { x: 0, y: 0});
|
2021-03-05 16:36:39 -05:00
|
|
|
if self.should_quit {
|
|
|
|
Terminal::clear_screen();
|
|
|
|
println!("Goodbye.\r");
|
|
|
|
} else {
|
|
|
|
self.draw_rows();
|
2021-03-08 09:50:15 -05:00
|
|
|
Terminal::cursor_position(&self.cursor_position);
|
2021-03-05 16:36:39 -05:00
|
|
|
}
|
|
|
|
Terminal::cursor_show();
|
|
|
|
Terminal::flush()
|
2021-01-28 16:47:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn process_keypress(&mut self) -> Result<(), std::io::Error> {
|
2021-03-05 16:36:39 -05:00
|
|
|
let pressed_key = Terminal::read_key()?;
|
2021-01-28 16:47:40 -05:00
|
|
|
match pressed_key {
|
|
|
|
Key::Ctrl('q') => self.should_quit = true,
|
2021-03-08 09:50:15 -05:00
|
|
|
Key::Up | Key::Down | Key::Left | Key::Right | Key::PageUp | Key::PageDown | Key::End | Key::Home => self.move_cursor(pressed_key),
|
2021-01-28 16:47:40 -05:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-03-08 09:50:15 -05:00
|
|
|
fn move_cursor(&mut self, key: Key) {
|
|
|
|
let Position { mut y, mut x } = self.cursor_position;
|
|
|
|
let size = self.terminal.size();
|
|
|
|
let height = size.height.saturating_sub(1) as usize;
|
|
|
|
let width = size.width.saturating_sub(1) as usize;
|
|
|
|
|
|
|
|
match key {
|
|
|
|
Key::Up => y = y.saturating_sub(1),
|
|
|
|
Key::Down => {
|
|
|
|
if y < height {
|
|
|
|
y = y.saturating_add(1);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Key::Left => x = x.saturating_sub(1),
|
|
|
|
Key::Right => {
|
|
|
|
if x < width {
|
|
|
|
x = x.saturating_add(1);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Key::PageUp => y = 0,
|
|
|
|
Key::PageDown => y = height,
|
|
|
|
Key::Home => x = 0,
|
|
|
|
Key::End => x = width,
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
self.cursor_position = Position { x, y }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn draw_welcome_message(&self) {
|
|
|
|
let mut welcome_message = format!("Hecto editor -- version {}", VERSION);
|
|
|
|
let width = self.terminal.size().width as usize;
|
|
|
|
let len = welcome_message.len();
|
|
|
|
let padding = width.saturating_sub(len) / 2;
|
|
|
|
let spaces = " ".repeat(padding.saturating_sub(1));
|
|
|
|
welcome_message = format!("~{}{}", spaces, welcome_message);
|
|
|
|
welcome_message.truncate(width);
|
|
|
|
println!("{}\r", welcome_message);
|
|
|
|
}
|
|
|
|
|
2021-03-05 16:36:39 -05:00
|
|
|
fn draw_rows(&self) {
|
|
|
|
let height = self.terminal.size().height;
|
|
|
|
|
|
|
|
for row in 0..height - 1 {
|
|
|
|
Terminal::clear_current_line();
|
|
|
|
|
|
|
|
if row == height / 3 {
|
2021-03-08 09:50:15 -05:00
|
|
|
self.draw_welcome_message();
|
2021-03-05 16:36:39 -05:00
|
|
|
} else {
|
|
|
|
println!("~\r");
|
|
|
|
}
|
2021-01-28 16:47:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn die(e: std::io::Error) {
|
2021-03-05 16:36:39 -05:00
|
|
|
Terminal::clear_screen();
|
2021-01-28 16:47:40 -05:00
|
|
|
panic!(e);
|
|
|
|
}
|