clide/src/tui/editor.rs

60 lines
1.9 KiB
Rust
Raw Normal View History

use crate::tui::component::{Action, ClideComponent};
use edtui::{
EditorEventHandler, EditorState, EditorTheme, EditorView, LineNumbers, SyntaxHighlighter,
};
2026-01-17 19:21:14 -05:00
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,
2026-01-17 19:21:14 -05:00
}
impl Editor {
pub fn new() -> Self {
Editor {
state: EditorState::default(),
event_handler: EditorEventHandler::default(),
}
2026-01-17 19:21:14 -05:00
}
}
impl Widget for &mut Editor {
2026-01-17 19:21:14 -05:00
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);
2026-01-17 19:21:14 -05:00
}
}
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
2026-01-17 19:21:14 -05:00
}
}