use crate::tui::component::{Action, ClideComponent}; use edtui::{ EditorEventHandler, EditorState, EditorTheme, EditorView, LineNumbers, SyntaxHighlighter, }; use ratatui::buffer::Buffer; use ratatui::crossterm::event::KeyEvent; use ratatui::layout::{Alignment, Rect}; use ratatui::prelude::{Color, Style}; use ratatui::widgets::{Block, Borders, Padding, Widget}; // TODO: Consider using editor-command https://docs.rs/editor-command/latest/editor_command/ // TODO: Title should be detected programming language name // TODO: Content should be file contents // TODO: Vimrc should be used pub struct Editor { pub state: EditorState, pub event_handler: EditorEventHandler, } impl Editor { pub fn new() -> Self { Editor { state: EditorState::default(), event_handler: EditorEventHandler::default(), } } } impl Widget for &mut Editor { fn render(self, area: Rect, buf: &mut Buffer) where Self: Sized, { // TODO: Use current file extension for syntax highlighting here. EditorView::new(&mut self.state) .wrap(true) .theme( EditorTheme::default().block( Block::default() .title("Rust") .title_style(Style::default().fg(Color::Yellow)) .title_alignment(Alignment::Right) .borders(Borders::ALL) .padding(Padding::new(0, 0, 0, 1)), ), ) .syntax_highlighter(SyntaxHighlighter::new("dracula", "rs").ok()) .tab_width(2) .line_numbers(LineNumbers::Absolute) .render(area, buf); } } impl ClideComponent for Editor { fn handle_key_events(&mut self, key: KeyEvent) -> Action { self.event_handler.on_key_event(key, &mut self.state); Action::Pass } }