2026-01-19 09:23:12 -05:00
|
|
|
use crate::tui::component::{Action, Component};
|
2026-01-18 10:09:28 -05:00
|
|
|
use crate::tui::editor::Editor;
|
2026-01-17 17:09:42 -05:00
|
|
|
use crate::tui::explorer::Explorer;
|
2026-01-17 14:04:02 -05:00
|
|
|
use ratatui::buffer::Buffer;
|
2026-01-18 10:09:28 -05:00
|
|
|
use ratatui::crossterm::event;
|
|
|
|
|
use ratatui::crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
|
|
|
|
|
use ratatui::layout::{Constraint, Direction, Layout, Rect};
|
2026-01-17 14:04:02 -05:00
|
|
|
use ratatui::prelude::{Color, Style, Widget};
|
|
|
|
|
use ratatui::widgets::{Block, Borders, Padding, Paragraph, Tabs, Wrap};
|
|
|
|
|
use ratatui::{DefaultTerminal, symbols};
|
2026-01-19 18:01:35 -05:00
|
|
|
use std::path::PathBuf;
|
2026-01-18 10:09:28 -05:00
|
|
|
use std::time::Duration;
|
2026-01-17 15:07:05 -05:00
|
|
|
|
2026-01-17 14:04:02 -05:00
|
|
|
pub struct App<'a> {
|
2026-01-17 15:07:05 -05:00
|
|
|
explorer: Explorer<'a>,
|
2026-01-18 10:09:28 -05:00
|
|
|
editor: Editor,
|
2026-01-17 14:04:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> App<'a> {
|
2026-01-19 18:01:35 -05:00
|
|
|
pub(crate) fn new(root_path: PathBuf) -> Self {
|
2026-01-19 15:03:50 -05:00
|
|
|
let mut app = Self {
|
|
|
|
|
explorer: Explorer::new(&root_path),
|
2026-01-18 10:09:28 -05:00
|
|
|
editor: Editor::new(),
|
2026-01-19 15:03:50 -05:00
|
|
|
};
|
|
|
|
|
app.editor
|
|
|
|
|
.set_contents(&root_path.join("src/tui/app.rs"))
|
|
|
|
|
.expect("Failed to set editor contents.");
|
|
|
|
|
app
|
2026-01-17 14:04:02 -05:00
|
|
|
}
|
|
|
|
|
|
2026-01-18 10:09:28 -05:00
|
|
|
fn get_event(&mut self) -> Option<Event> {
|
|
|
|
|
if !event::poll(Duration::from_millis(250)).expect("event poll failed") {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
event::read().ok()
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-17 14:04:02 -05:00
|
|
|
pub fn run(mut self, mut terminal: DefaultTerminal) -> anyhow::Result<()> {
|
|
|
|
|
loop {
|
2026-01-19 15:03:50 -05:00
|
|
|
// TODO: Handle events based on which component is active.
|
2026-01-17 17:09:42 -05:00
|
|
|
terminal.draw(|f| {
|
2026-01-18 10:09:28 -05:00
|
|
|
f.render_widget(&mut self, f.area());
|
2026-01-17 17:09:42 -05:00
|
|
|
})?;
|
|
|
|
|
|
2026-01-18 10:09:28 -05:00
|
|
|
if let Some(event) = self.get_event() {
|
2026-01-18 11:02:41 -05:00
|
|
|
match self.handle_event(event) {
|
|
|
|
|
Action::Quit => break,
|
2026-01-19 15:03:50 -05:00
|
|
|
Action::Handled => {}
|
|
|
|
|
_ => {
|
|
|
|
|
// panic!("Unhandled event: {:?}", event);
|
|
|
|
|
}
|
2026-01-18 10:09:28 -05:00
|
|
|
}
|
2026-01-17 14:04:02 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-17 17:09:42 -05:00
|
|
|
fn draw_status(&self, area: Rect, buf: &mut Buffer) {
|
|
|
|
|
// TODO: Status bar should have drop down menus
|
2026-01-17 14:04:02 -05:00
|
|
|
Tabs::new(["File", "Edit", "View", "Help"])
|
|
|
|
|
.style(Style::default())
|
|
|
|
|
.block(Block::default().borders(Borders::ALL))
|
|
|
|
|
.render(area, buf);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-17 17:09:42 -05:00
|
|
|
fn draw_tabs(&self, area: Rect, buf: &mut Buffer) {
|
2026-01-17 14:04:02 -05:00
|
|
|
// TODO: Tabs should be opened from file explorer
|
|
|
|
|
Tabs::new(["file.md", "file.cpp"])
|
|
|
|
|
.divider(symbols::DOT)
|
|
|
|
|
.block(
|
|
|
|
|
Block::default()
|
|
|
|
|
.borders(Borders::NONE)
|
|
|
|
|
.padding(Padding::new(0, 0, 0, 0)),
|
|
|
|
|
)
|
|
|
|
|
.highlight_style(Style::default().fg(Color::LightRed))
|
|
|
|
|
.render(area, buf);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-17 17:09:42 -05:00
|
|
|
fn draw_terminal(&self, area: Rect, buf: &mut Buffer) {
|
2026-01-17 14:04:02 -05:00
|
|
|
// TODO: Title should be detected shell name
|
|
|
|
|
// TODO: Contents should be shell output
|
2026-01-17 15:07:05 -05:00
|
|
|
Paragraph::new("shaun@pc:~/Code/clide$ ")
|
2026-01-17 14:04:02 -05:00
|
|
|
.style(Style::default())
|
|
|
|
|
.block(
|
|
|
|
|
Block::default()
|
|
|
|
|
.title("Bash")
|
|
|
|
|
.title_style(Style::default().fg(Color::DarkGray))
|
|
|
|
|
.borders(Borders::ALL),
|
|
|
|
|
)
|
|
|
|
|
.wrap(Wrap { trim: false })
|
|
|
|
|
.render(area, buf);
|
|
|
|
|
}
|
2026-01-19 18:01:35 -05:00
|
|
|
|
|
|
|
|
/// Refresh the contents of the editor to match the selected TreeItem in the file Explorer.
|
|
|
|
|
/// If the selected item is not a file, this does nothing.
|
|
|
|
|
fn refresh_editor_contents(&mut self) {
|
|
|
|
|
if let Some(current_file_path) = self.editor.file_path.clone() {
|
|
|
|
|
if let Some(selected_path_string) = self.explorer.selected() {
|
|
|
|
|
let selected_pathbuf = PathBuf::from(selected_path_string);
|
|
|
|
|
if std::path::absolute(&selected_pathbuf).unwrap().is_file()
|
|
|
|
|
&& selected_pathbuf != current_file_path
|
|
|
|
|
{
|
|
|
|
|
self.editor.set_contents(&selected_pathbuf.into()).ok();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-17 14:04:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Separate complex components into their own widgets.
|
2026-01-18 10:09:28 -05:00
|
|
|
impl<'a> Widget for &mut App<'a> {
|
2026-01-17 14:04:02 -05:00
|
|
|
fn render(self, area: Rect, buf: &mut Buffer)
|
|
|
|
|
where
|
|
|
|
|
Self: Sized,
|
|
|
|
|
{
|
|
|
|
|
let vertical = Layout::default()
|
|
|
|
|
.direction(Direction::Vertical)
|
|
|
|
|
.constraints([
|
|
|
|
|
Constraint::Length(3), // status bar
|
|
|
|
|
Constraint::Percentage(70), // horizontal layout
|
|
|
|
|
Constraint::Percentage(30), // terminal
|
|
|
|
|
])
|
|
|
|
|
.split(area);
|
|
|
|
|
|
|
|
|
|
let horizontal = Layout::default()
|
|
|
|
|
.direction(Direction::Horizontal)
|
|
|
|
|
.constraints([
|
|
|
|
|
Constraint::Max(30), // File explorer with a max width of 30 characters.
|
|
|
|
|
Constraint::Fill(1), // Editor fills the remaining space.
|
|
|
|
|
])
|
|
|
|
|
.split(vertical[1]);
|
|
|
|
|
|
|
|
|
|
let editor_layout = Layout::default()
|
|
|
|
|
.direction(Direction::Vertical)
|
|
|
|
|
.constraints([
|
|
|
|
|
Constraint::Length(1), // Editor tabs.
|
2026-01-17 17:09:42 -05:00
|
|
|
Constraint::Fill(1), // Editor contents.
|
2026-01-17 14:04:02 -05:00
|
|
|
])
|
|
|
|
|
.split(horizontal[1]);
|
|
|
|
|
|
|
|
|
|
self.draw_status(vertical[0], buf);
|
|
|
|
|
self.draw_terminal(vertical[2], buf);
|
|
|
|
|
|
2026-01-17 17:09:42 -05:00
|
|
|
self.explorer.render(horizontal[0], buf);
|
2026-01-17 14:04:02 -05:00
|
|
|
|
|
|
|
|
self.draw_tabs(editor_layout[0], buf);
|
2026-01-19 18:01:35 -05:00
|
|
|
self.refresh_editor_contents();
|
2026-01-18 10:09:28 -05:00
|
|
|
self.editor.render(editor_layout[1], buf);
|
2026-01-17 14:04:02 -05:00
|
|
|
}
|
|
|
|
|
}
|
2026-01-17 17:09:42 -05:00
|
|
|
|
2026-01-19 09:23:12 -05:00
|
|
|
impl<'a> Component for App<'a> {
|
2026-01-19 15:03:50 -05:00
|
|
|
fn id(&self) -> &str {
|
|
|
|
|
"app"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// TODO: Get active widget with some Component trait function helper?
|
|
|
|
|
/// trait Component { fn get_state() -> ComponentState; }
|
|
|
|
|
/// if component.get_state() = ComponentState::Active { component.handle_event(); }
|
|
|
|
|
///
|
|
|
|
|
/// App could then provide helpers for altering Component state based on TUI grouping..
|
|
|
|
|
/// (such as editor tabs, file explorer, status bars, etc..)
|
|
|
|
|
fn handle_event(&mut self, event: Event) -> Action {
|
|
|
|
|
// Handle events in the primary application.
|
|
|
|
|
if let Some(key_event) = event.as_key_event() {
|
|
|
|
|
match self.handle_key_events(key_event) {
|
|
|
|
|
Action::Quit => return Action::Quit,
|
|
|
|
|
Action::Handled => {
|
2026-01-19 17:41:46 -05:00
|
|
|
// dbg!(format!("Handled event: {:?}", self.id()));
|
2026-01-19 15:03:50 -05:00
|
|
|
return Action::Handled;
|
|
|
|
|
}
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-19 17:41:46 -05:00
|
|
|
self.explorer.handle_event(event.clone());
|
|
|
|
|
self.editor.handle_event(event.clone());
|
2026-01-19 15:03:50 -05:00
|
|
|
|
|
|
|
|
// Handle events for all components.
|
|
|
|
|
// for component in &mut self.components {
|
|
|
|
|
// dbg!(format!("Handling event: {:?}", component.id()));
|
|
|
|
|
// // Actions returned here abort the input handling iteration.
|
|
|
|
|
// match component.handle_event(event.clone()) {
|
|
|
|
|
// Action::Quit => return Action::Quit,
|
|
|
|
|
// Action::Handled => return Action::Handled,
|
|
|
|
|
// _ => continue,
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
Action::Noop
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-18 10:09:28 -05:00
|
|
|
fn handle_key_events(&mut self, key: KeyEvent) -> Action {
|
|
|
|
|
match key {
|
|
|
|
|
KeyEvent {
|
|
|
|
|
code: KeyCode::Char('c'),
|
|
|
|
|
modifiers: KeyModifiers::CONTROL,
|
|
|
|
|
kind: KeyEventKind::Press,
|
|
|
|
|
state: _state,
|
|
|
|
|
} => Action::Quit,
|
|
|
|
|
key_event => {
|
|
|
|
|
// Pass the key event to each component that can handle it.
|
|
|
|
|
self.explorer.handle_key_events(key_event)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|