Internal implementation of the interactive editor, which interacts with the `console`.
(&mut self, console: &mut dyn Console)
| 272 | |
| 273 | /// Internal implementation of the interactive editor, which interacts with the `console`. |
| 274 | async fn edit_interactively(&mut self, console: &mut dyn Console) -> io::Result<()> { |
| 275 | let console_size = console.size_chars()?; |
| 276 | |
| 277 | if self.content.is_empty() { |
| 278 | self.content.push(LineBuffer::default()); |
| 279 | } |
| 280 | |
| 281 | let mut refresh = RefreshKind::Full; |
| 282 | loop { |
| 283 | // The key handling below only deals with moving the insertion position within the file |
| 284 | // but does not bother to update the viewport. Adjust it now, if necessary. |
| 285 | let width = usize::from(console_size.x); |
| 286 | let height = usize::from(console_size.y); |
| 287 | if self.file_pos.line < self.viewport_pos.line { |
| 288 | self.viewport_pos.line = self.file_pos.line; |
| 289 | refresh.upgrade(RefreshKind::Full); |
| 290 | } else if self.file_pos.line > self.viewport_pos.line + height - 2 { |
| 291 | if self.file_pos.line > height - 2 { |
| 292 | self.viewport_pos.line = self.file_pos.line - (height - 2); |
| 293 | } else { |
| 294 | self.viewport_pos.line = 0; |
| 295 | } |
| 296 | refresh.upgrade(RefreshKind::Full); |
| 297 | } |
| 298 | |
| 299 | if self.file_pos.col < self.viewport_pos.col { |
| 300 | self.viewport_pos.col = self.file_pos.col; |
| 301 | refresh.upgrade(RefreshKind::Full); |
| 302 | } else if self.file_pos.col >= self.viewport_pos.col + width { |
| 303 | self.viewport_pos.col = self.file_pos.col - width + 1; |
| 304 | refresh.upgrade(RefreshKind::Full); |
| 305 | } |
| 306 | |
| 307 | // TODO(jmmv): We must handle the cursor visibility outside of the non-sync block |
| 308 | // because the current console implementation forces the cursor to be invisible |
| 309 | // when syncing is disabled. This is suboptimal and should be fixed by decoupling |
| 310 | // the two properties... |
| 311 | console.hide_cursor()?; |
| 312 | match refresh { |
| 313 | RefreshKind::Full => self.refresh(console, console_size)?, |
| 314 | RefreshKind::CurrentLine => { |
| 315 | self.refresh_status(console, console_size)?; |
| 316 | self.refresh_current_line(console, console_size)?; |
| 317 | } |
| 318 | RefreshKind::None => { |
| 319 | self.refresh_status(console, console_size)?; |
| 320 | console.set_color(TEXT_COLOR.0, TEXT_COLOR.1)?; |
| 321 | } |
| 322 | } |
| 323 | refresh = RefreshKind::None; |
| 324 | let cursor_pos = { |
| 325 | let x = self.file_pos.col - self.viewport_pos.col; |
| 326 | let y = self.file_pos.line - self.viewport_pos.line; |
| 327 | if cfg!(debug_assertions) { |
| 328 | CharsXY::new( |
| 329 | u16::try_from(x).expect("Computed x must have fit on screen"), |
| 330 | u16::try_from(y).expect("Computed y must have fit on screen"), |
| 331 | ) |
no test coverage detected