|
| 1 | +use crossterm::{ |
| 2 | + cursor, |
| 3 | + event::{self, Event, KeyEventKind}, |
| 4 | + execute, |
| 5 | + terminal::{self, ClearType}, |
| 6 | +}; |
| 7 | +use std::fs; |
| 8 | +use std::io::{self, stdout}; |
| 9 | +use std::path::Path; |
| 10 | + |
| 11 | +use crate::editor::display::Display; |
| 12 | +use crate::editor::input::InputHandler; |
| 13 | + |
| 14 | +pub struct Editor { |
| 15 | + pub content: Vec<String>, |
| 16 | + pub cursor_x: usize, |
| 17 | + pub cursor_y: usize, |
| 18 | + pub offset_y: usize, |
| 19 | + pub filename: Option<String>, |
| 20 | + pub modified: bool, |
| 21 | + pub terminal_height: usize, |
| 22 | + pub terminal_width: usize, |
| 23 | +} |
| 24 | + |
| 25 | +impl Editor { |
| 26 | + pub fn new() -> io::Result<Self> { |
| 27 | + let (width, height) = terminal::size()?; |
| 28 | + Ok(Editor { |
| 29 | + content: vec![String::new()], |
| 30 | + cursor_x: 0, |
| 31 | + cursor_y: 0, |
| 32 | + offset_y: 0, |
| 33 | + filename: None, |
| 34 | + modified: false, |
| 35 | + terminal_height: height as usize, |
| 36 | + terminal_width: width as usize, |
| 37 | + }) |
| 38 | + } |
| 39 | + |
| 40 | + pub fn open_file(filename: &str) -> io::Result<Self> { |
| 41 | + let mut editor = Self::new()?; |
| 42 | + editor.filename = Some(filename.to_string()); |
| 43 | + |
| 44 | + if Path::new(filename).exists() { |
| 45 | + let content = fs::read_to_string(filename)?; |
| 46 | + editor.content = if content.is_empty() { |
| 47 | + vec![String::new()] |
| 48 | + } else { |
| 49 | + content.lines().map(|s| s.to_string()).collect() |
| 50 | + }; |
| 51 | + } |
| 52 | + |
| 53 | + Ok(editor) |
| 54 | + } |
| 55 | + |
| 56 | + pub fn run(&mut self) -> io::Result<()> { |
| 57 | + terminal::enable_raw_mode()?; |
| 58 | + execute!(stdout(), terminal::Clear(ClearType::All))?; |
| 59 | + |
| 60 | + let mut input_handler = InputHandler::new(); |
| 61 | + |
| 62 | + loop { |
| 63 | + let display_result = { |
| 64 | + let mut display = Display::new(); |
| 65 | + display.refresh_screen(self) |
| 66 | + }; |
| 67 | + display_result?; |
| 68 | + |
| 69 | + if let Event::Key(key_event) = event::read()? { |
| 70 | + if key_event.kind == KeyEventKind::Press { |
| 71 | + if input_handler.process_key(self, key_event)? { |
| 72 | + break; |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + terminal::disable_raw_mode()?; |
| 79 | + execute!(stdout(), terminal::Clear(ClearType::All))?; |
| 80 | + execute!(stdout(), cursor::MoveTo(0, 0))?; |
| 81 | + Ok(()) |
| 82 | + } |
| 83 | + |
| 84 | + pub fn clamp_cursor_x(&mut self) { |
| 85 | + let line_len = self.content[self.cursor_y].len(); |
| 86 | + if self.cursor_x > line_len { |
| 87 | + self.cursor_x = line_len; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + pub fn insert_char(&mut self, c: char) { |
| 92 | + self.content[self.cursor_y].insert(self.cursor_x, c); |
| 93 | + self.cursor_x += 1; |
| 94 | + self.modified = true; |
| 95 | + } |
| 96 | + |
| 97 | + pub fn insert_newline(&mut self) { |
| 98 | + let current_line = &self.content[self.cursor_y]; |
| 99 | + let new_line = current_line[self.cursor_x..].to_string(); |
| 100 | + self.content[self.cursor_y].truncate(self.cursor_x); |
| 101 | + self.content.insert(self.cursor_y + 1, new_line); |
| 102 | + self.cursor_y += 1; |
| 103 | + self.cursor_x = 0; |
| 104 | + self.modified = true; |
| 105 | + } |
| 106 | + |
| 107 | + pub fn delete_char(&mut self) { |
| 108 | + if self.cursor_x > 0 { |
| 109 | + self.content[self.cursor_y].remove(self.cursor_x - 1); |
| 110 | + self.cursor_x -= 1; |
| 111 | + self.modified = true; |
| 112 | + } else if self.cursor_y > 0 { |
| 113 | + let current_line = self.content.remove(self.cursor_y); |
| 114 | + self.cursor_y -= 1; |
| 115 | + self.cursor_x = self.content[self.cursor_y].len(); |
| 116 | + self.content[self.cursor_y].push_str(¤t_line); |
| 117 | + self.modified = true; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + pub fn delete_char_forward(&mut self) { |
| 122 | + if self.cursor_x < self.content[self.cursor_y].len() { |
| 123 | + self.content[self.cursor_y].remove(self.cursor_x); |
| 124 | + self.modified = true; |
| 125 | + } else if self.cursor_y < self.content.len().saturating_sub(1) { |
| 126 | + let next_line = self.content.remove(self.cursor_y + 1); |
| 127 | + self.content[self.cursor_y].push_str(&next_line); |
| 128 | + self.modified = true; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + pub fn move_cursor_up(&mut self) { |
| 133 | + if self.cursor_y > 0 { |
| 134 | + self.cursor_y -= 1; |
| 135 | + if self.cursor_y < self.offset_y { |
| 136 | + self.offset_y = self.cursor_y; |
| 137 | + } |
| 138 | + self.clamp_cursor_x(); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + pub fn move_cursor_down(&mut self) { |
| 143 | + if self.cursor_y < self.content.len().saturating_sub(1) { |
| 144 | + self.cursor_y += 1; |
| 145 | + // Prevent overflow by checking if terminal_height is large enough |
| 146 | + if self.terminal_height > 2 && self.cursor_y >= self.offset_y + self.terminal_height - 2 { |
| 147 | + self.offset_y = self.cursor_y.saturating_sub(self.terminal_height - 3); |
| 148 | + } |
| 149 | + self.clamp_cursor_x(); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + pub fn move_cursor_left(&mut self) { |
| 154 | + if self.cursor_x > 0 { |
| 155 | + self.cursor_x -= 1; |
| 156 | + } else if self.cursor_y > 0 { |
| 157 | + self.cursor_y -= 1; |
| 158 | + self.cursor_x = self.content[self.cursor_y].len(); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + pub fn move_cursor_right(&mut self) { |
| 163 | + if self.cursor_x < self.content[self.cursor_y].len() { |
| 164 | + self.cursor_x += 1; |
| 165 | + } else if self.cursor_y < self.content.len() - 1 { |
| 166 | + self.cursor_y += 1; |
| 167 | + self.cursor_x = 0; |
| 168 | + } |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +pub fn start_editor(filename: Option<&str>) -> io::Result<()> { |
| 173 | + let mut editor = if let Some(file) = filename { |
| 174 | + Editor::open_file(file)? |
| 175 | + } else { |
| 176 | + Editor::new()? |
| 177 | + }; |
| 178 | + |
| 179 | + editor.run() |
| 180 | +} |
0 commit comments