2026-01-24 10:47:29 -05:00
|
|
|
use crate::tui::app::AppComponent::{AppEditor, AppExplorer, AppLogger};
|
2026-01-22 19:23:21 -05:00
|
|
|
use crate::tui::component::{Action, Component, Focus, FocusState};
|
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-20 20:14:25 -05:00
|
|
|
use crate::tui::logger::Logger;
|
2026-01-24 14:22:42 -05:00
|
|
|
use crate::tui::title_bar::TitleBar;
|
|
|
|
|
use AppComponent::AppTitleBar;
|
2026-01-24 10:47:29 -05:00
|
|
|
use anyhow::{Context, Result};
|
2026-01-20 20:14:25 -05:00
|
|
|
use log::{debug, error, info, trace, warn};
|
2026-01-17 14:04:02 -05:00
|
|
|
use ratatui::buffer::Buffer;
|
2026-01-18 10:09:28 -05:00
|
|
|
use ratatui::crossterm::event;
|
2026-01-24 14:22:42 -05:00
|
|
|
use ratatui::crossterm::event::{
|
|
|
|
|
Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers, MouseButton, MouseEventKind,
|
|
|
|
|
};
|
2026-01-18 10:09:28 -05:00
|
|
|
use ratatui::layout::{Constraint, Direction, Layout, Rect};
|
2026-01-17 14:04:02 -05:00
|
|
|
use ratatui::prelude::{Color, Style, Widget};
|
2026-01-22 20:35:40 -05:00
|
|
|
use ratatui::widgets::{Block, Borders, Padding, Paragraph, Tabs, Wrap};
|
2026-01-17 14:04:02 -05:00
|
|
|
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-20 20:14:25 -05:00
|
|
|
// TODO: Need a way to dynamically run Widget::render on all widgets.
|
|
|
|
|
// TODO: + Need a way to map Rect to Component::id() to position each widget?
|
2026-01-24 10:47:29 -05:00
|
|
|
// TODO: Need a good way to dynamically run Component methods on all widgets.
|
|
|
|
|
#[derive(PartialEq)]
|
|
|
|
|
pub enum AppComponent {
|
|
|
|
|
AppEditor,
|
|
|
|
|
AppExplorer,
|
|
|
|
|
AppLogger,
|
2026-01-24 14:22:42 -05:00
|
|
|
AppTitleBar,
|
2026-01-20 20:43:01 -05:00
|
|
|
}
|
|
|
|
|
|
2026-01-17 14:04:02 -05:00
|
|
|
pub struct App<'a> {
|
2026-01-24 10:47:29 -05:00
|
|
|
editor: Editor,
|
|
|
|
|
explorer: Explorer<'a>,
|
|
|
|
|
logger: Logger,
|
2026-01-24 14:22:42 -05:00
|
|
|
title_bar: TitleBar,
|
2026-01-24 10:47:29 -05:00
|
|
|
last_active: AppComponent,
|
2026-01-17 14:04:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> App<'a> {
|
2026-01-24 10:47:29 -05:00
|
|
|
pub fn id() -> &'static str {
|
|
|
|
|
"App"
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 17:19:13 -05:00
|
|
|
pub fn new(root_path: PathBuf) -> Result<Self> {
|
2026-01-24 10:47:29 -05:00
|
|
|
let app = Self {
|
|
|
|
|
editor: Editor::new(),
|
|
|
|
|
explorer: Explorer::new(&root_path)?,
|
|
|
|
|
logger: Logger::new(),
|
2026-01-24 14:22:42 -05:00
|
|
|
title_bar: TitleBar::new(),
|
2026-01-24 10:47:29 -05:00
|
|
|
last_active: AppEditor,
|
2026-01-19 15:03:50 -05:00
|
|
|
};
|
2026-01-24 10:47:29 -05:00
|
|
|
Ok(app)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Logic that should be executed once on application startup.
|
|
|
|
|
pub fn start(&mut self) -> Result<()> {
|
|
|
|
|
let root_path = self.explorer.root_path.clone();
|
|
|
|
|
self.editor
|
2026-01-19 15:03:50 -05:00
|
|
|
.set_contents(&root_path.join("src/tui/app.rs"))
|
2026-01-20 17:19:13 -05:00
|
|
|
.context(format!(
|
2026-01-21 20:28:24 -05:00
|
|
|
"Failed to initialize editor contents to path: {root_path:?}"
|
2026-01-20 17:19:13 -05:00
|
|
|
))?;
|
2026-01-24 10:47:29 -05:00
|
|
|
self.editor.component_state.set_focus(Focus::Active);
|
|
|
|
|
Ok(())
|
2026-01-20 16:03:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn run(mut self, mut terminal: DefaultTerminal) -> Result<()> {
|
2026-01-24 10:47:29 -05:00
|
|
|
self.start()?;
|
2026-01-17 14:04:02 -05:00
|
|
|
loop {
|
2026-01-20 17:19:13 -05:00
|
|
|
self.refresh_editor_contents()
|
|
|
|
|
.context("Failed to refresh editor contents.")?;
|
|
|
|
|
|
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-20 17:19:13 -05:00
|
|
|
if event::poll(Duration::from_millis(250)).context("event poll failed")? {
|
|
|
|
|
match self.handle_event(event::read()?)? {
|
2026-01-18 11:02:41 -05:00
|
|
|
Action::Quit => break,
|
2026-01-19 15:03:50 -05:00
|
|
|
Action::Handled => {}
|
|
|
|
|
_ => {
|
2026-01-20 17:37:15 -05:00
|
|
|
// bail!("Unhandled event: {:?}", event);
|
2026-01-19 15:03:50 -05:00
|
|
|
}
|
2026-01-18 10:09:28 -05:00
|
|
|
}
|
2026-01-17 14:04:02 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-22 20:35:40 -05:00
|
|
|
fn draw_bottom_status(&self, area: Rect, buf: &mut Buffer) {
|
2026-01-24 10:47:29 -05:00
|
|
|
// Determine help text from the most recently focused component.
|
|
|
|
|
let help = match self.last_active {
|
|
|
|
|
AppEditor => self.editor.component_state.help_text.clone(),
|
|
|
|
|
AppExplorer => self.explorer.component_state.help_text.clone(),
|
|
|
|
|
AppLogger => self.logger.component_state.help_text.clone(),
|
2026-01-24 14:22:42 -05:00
|
|
|
AppTitleBar => self.title_bar.component_state.help_text.clone(),
|
2026-01-24 10:47:29 -05:00
|
|
|
};
|
2026-01-24 12:29:24 -05:00
|
|
|
Paragraph::new(
|
|
|
|
|
concat!(
|
|
|
|
|
"ALT+Q: Focus project explorer | ALT+W: Focus editor | ALT+E: Focus logger |",
|
|
|
|
|
" CTRL+C: Quit\n"
|
|
|
|
|
)
|
|
|
|
|
.to_string()
|
|
|
|
|
+ help.as_str(),
|
|
|
|
|
)
|
|
|
|
|
.style(Color::Gray)
|
|
|
|
|
.wrap(Wrap { trim: false })
|
|
|
|
|
.centered()
|
|
|
|
|
.render(area, buf);
|
2026-01-22 20:35:40 -05:00
|
|
|
}
|
|
|
|
|
|
2026-01-17 17:09:42 -05:00
|
|
|
fn draw_tabs(&self, area: Rect, buf: &mut Buffer) {
|
2026-01-20 12:00:24 -05:00
|
|
|
// Determine the tab title from the current file (or use a fallback).
|
2026-01-24 10:47:29 -05:00
|
|
|
if let Some(title) = self.editor.file_path.clone() {
|
|
|
|
|
Tabs::new(vec![
|
|
|
|
|
title
|
|
|
|
|
.file_name()
|
|
|
|
|
.map(|f| f.to_str())
|
|
|
|
|
.unwrap_or(Some("Unknown"))
|
|
|
|
|
.unwrap(),
|
|
|
|
|
])
|
2026-01-17 14:04:02 -05:00
|
|
|
.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-24 10:47:29 -05:00
|
|
|
} else {
|
|
|
|
|
error!(target:Self::id(), "Failed to get Editor file_path while drawing Tabs widget.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn change_focus(&mut self, focus: AppComponent) {
|
|
|
|
|
if self.last_active == AppEditor {
|
|
|
|
|
self.editor.state.cursor.row = 0;
|
|
|
|
|
self.editor.state.cursor.col = 0;
|
|
|
|
|
}
|
|
|
|
|
match focus {
|
|
|
|
|
AppEditor => self.editor.component_state.set_focus(Focus::Active),
|
|
|
|
|
AppExplorer => self.explorer.component_state.set_focus(Focus::Active),
|
|
|
|
|
AppLogger => self.logger.component_state.set_focus(Focus::Active),
|
2026-01-24 14:22:42 -05:00
|
|
|
AppTitleBar => self.title_bar.component_state.set_focus(Focus::Active),
|
2026-01-24 10:47:29 -05:00
|
|
|
}
|
|
|
|
|
self.last_active = focus;
|
2026-01-17 14:04:02 -05:00
|
|
|
}
|
|
|
|
|
|
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.
|
2026-01-20 16:03:38 -05:00
|
|
|
fn refresh_editor_contents(&mut self) -> Result<()> {
|
|
|
|
|
// Use the currently selected TreeItem or get an absolute path to this source file.
|
2026-01-24 10:47:29 -05:00
|
|
|
let selected_pathbuf = match self.explorer.selected() {
|
2026-01-20 16:03:38 -05:00
|
|
|
Ok(path) => PathBuf::from(path),
|
|
|
|
|
Err(_) => PathBuf::from(std::path::absolute(file!())?.to_string_lossy().to_string()),
|
|
|
|
|
};
|
2026-01-24 10:47:29 -05:00
|
|
|
let current_file_path = self
|
|
|
|
|
.editor
|
|
|
|
|
.file_path
|
|
|
|
|
.clone()
|
|
|
|
|
.context("Failed to get Editor current file_path")?;
|
|
|
|
|
if selected_pathbuf == current_file_path || !selected_pathbuf.is_file() {
|
|
|
|
|
return Ok(());
|
2026-01-19 18:01:35 -05:00
|
|
|
}
|
2026-01-24 10:47:29 -05:00
|
|
|
self.editor.set_contents(&selected_pathbuf)
|
2026-01-19 18:01:35 -05:00
|
|
|
}
|
2026-01-17 14:04:02 -05:00
|
|
|
}
|
|
|
|
|
|
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([
|
2026-01-22 20:35:40 -05:00
|
|
|
Constraint::Length(3), // top status bar
|
2026-01-17 14:04:02 -05:00
|
|
|
Constraint::Percentage(70), // horizontal layout
|
|
|
|
|
Constraint::Percentage(30), // terminal
|
2026-01-22 20:35:40 -05:00
|
|
|
Constraint::Length(3), // bottom status bar
|
2026-01-17 14:04:02 -05:00
|
|
|
])
|
|
|
|
|
.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]);
|
|
|
|
|
|
2026-01-22 20:35:40 -05:00
|
|
|
self.draw_bottom_status(vertical[3], buf);
|
2026-01-17 14:04:02 -05:00
|
|
|
self.draw_tabs(editor_layout[0], buf);
|
2026-01-24 10:47:29 -05:00
|
|
|
let id = App::id().to_string();
|
|
|
|
|
self.editor.render(editor_layout[1], buf);
|
|
|
|
|
self.explorer
|
|
|
|
|
.render(horizontal[0], buf)
|
|
|
|
|
.context("Failed to render Explorer")
|
|
|
|
|
.unwrap_or_else(|e| error!(target:id.as_str(), "{}", e));
|
|
|
|
|
self.logger.render(vertical[2], buf);
|
2026-01-24 15:33:48 -05:00
|
|
|
|
|
|
|
|
// The title bar is rendered last to overlay any popups created for drop-down menus.
|
|
|
|
|
self.title_bar.render(vertical[0], 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-20 16:03:38 -05:00
|
|
|
/// Handles events for the App and delegates to attached Components.
|
2026-01-20 17:19:13 -05:00
|
|
|
fn handle_event(&mut self, event: Event) -> Result<Action> {
|
2026-01-19 15:03:50 -05:00
|
|
|
// Handle events in the primary application.
|
|
|
|
|
if let Some(key_event) = event.as_key_event() {
|
2026-01-20 17:19:13 -05:00
|
|
|
let res = self
|
|
|
|
|
.handle_key_events(key_event)
|
|
|
|
|
.context("Failed to handle key events for primary App Component.");
|
|
|
|
|
match res {
|
|
|
|
|
Ok(Action::Quit) | Ok(Action::Handled) => return res,
|
2026-01-19 15:03:50 -05:00
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-24 12:47:17 -05:00
|
|
|
// Components should always handle mouse events for click interaction.
|
|
|
|
|
if let Some(mouse) = event.as_mouse_event() {
|
2026-01-24 14:22:42 -05:00
|
|
|
if mouse.kind == MouseEventKind::Down(MouseButton::Left) {
|
|
|
|
|
self.editor.handle_mouse_events(mouse)?;
|
|
|
|
|
self.explorer.handle_mouse_events(mouse)?;
|
|
|
|
|
self.logger.handle_mouse_events(mouse)?;
|
|
|
|
|
}
|
2026-01-24 12:47:17 -05:00
|
|
|
}
|
2026-01-19 15:03:50 -05:00
|
|
|
|
|
|
|
|
// Handle events for all components.
|
2026-01-24 10:47:29 -05:00
|
|
|
let action = match self.last_active {
|
|
|
|
|
AppEditor => self.editor.handle_event(event)?,
|
|
|
|
|
AppExplorer => self.explorer.handle_event(event)?,
|
|
|
|
|
AppLogger => self.logger.handle_event(event)?,
|
2026-01-24 14:22:42 -05:00
|
|
|
AppTitleBar => self.title_bar.handle_event(event)?,
|
2026-01-24 10:47:29 -05:00
|
|
|
};
|
|
|
|
|
match action {
|
|
|
|
|
Action::Quit | Action::Handled => return Ok(action),
|
|
|
|
|
_ => {}
|
2026-01-20 16:03:38 -05:00
|
|
|
}
|
2026-01-20 17:19:13 -05:00
|
|
|
Ok(Action::Noop)
|
2026-01-19 15:03:50 -05:00
|
|
|
}
|
|
|
|
|
|
2026-01-20 16:03:38 -05:00
|
|
|
/// Handles key events for the App Component only.
|
2026-01-20 17:19:13 -05:00
|
|
|
fn handle_key_events(&mut self, key: KeyEvent) -> Result<Action> {
|
2026-01-18 10:09:28 -05:00
|
|
|
match key {
|
2026-01-22 19:23:21 -05:00
|
|
|
KeyEvent {
|
|
|
|
|
code: KeyCode::Char('q'),
|
|
|
|
|
modifiers: KeyModifiers::ALT,
|
|
|
|
|
kind: KeyEventKind::Press,
|
|
|
|
|
state: _state,
|
|
|
|
|
} => {
|
2026-01-24 10:47:29 -05:00
|
|
|
self.change_focus(AppExplorer);
|
2026-01-22 19:23:21 -05:00
|
|
|
Ok(Action::Handled)
|
|
|
|
|
}
|
|
|
|
|
KeyEvent {
|
|
|
|
|
code: KeyCode::Char('w'),
|
|
|
|
|
modifiers: KeyModifiers::ALT,
|
|
|
|
|
kind: KeyEventKind::Press,
|
|
|
|
|
state: _state,
|
|
|
|
|
} => {
|
2026-01-24 10:47:29 -05:00
|
|
|
self.change_focus(AppEditor);
|
2026-01-22 19:23:21 -05:00
|
|
|
Ok(Action::Handled)
|
|
|
|
|
}
|
|
|
|
|
KeyEvent {
|
|
|
|
|
code: KeyCode::Char('e'),
|
|
|
|
|
modifiers: KeyModifiers::ALT,
|
|
|
|
|
kind: KeyEventKind::Press,
|
|
|
|
|
state: _state,
|
|
|
|
|
} => {
|
2026-01-24 10:47:29 -05:00
|
|
|
self.change_focus(AppLogger);
|
2026-01-22 19:23:21 -05:00
|
|
|
Ok(Action::Handled)
|
|
|
|
|
}
|
2026-01-24 14:22:42 -05:00
|
|
|
KeyEvent {
|
|
|
|
|
code: KeyCode::Char('r'),
|
|
|
|
|
modifiers: KeyModifiers::ALT,
|
|
|
|
|
kind: KeyEventKind::Press,
|
|
|
|
|
state: _state,
|
|
|
|
|
} => {
|
|
|
|
|
self.change_focus(AppTitleBar);
|
|
|
|
|
Ok(Action::Handled)
|
|
|
|
|
}
|
2026-01-20 20:14:25 -05:00
|
|
|
KeyEvent {
|
|
|
|
|
code: KeyCode::Char('l'),
|
2026-01-22 19:23:21 -05:00
|
|
|
modifiers: KeyModifiers::ALT,
|
2026-01-20 20:14:25 -05:00
|
|
|
kind: KeyEventKind::Press,
|
|
|
|
|
state: _state,
|
|
|
|
|
} => {
|
2026-01-24 10:47:29 -05:00
|
|
|
error!(target:App::id(), "an error");
|
|
|
|
|
warn!(target:App::id(), "a warning");
|
|
|
|
|
info!(target:App::id(), "a two line info\nsecond line");
|
|
|
|
|
debug!(target:App::id(), "a debug");
|
|
|
|
|
trace!(target:App::id(), "a trace");
|
2026-01-22 19:23:21 -05:00
|
|
|
Ok(Action::Handled)
|
2026-01-20 20:14:25 -05:00
|
|
|
}
|
2026-01-18 10:09:28 -05:00
|
|
|
KeyEvent {
|
|
|
|
|
code: KeyCode::Char('c'),
|
|
|
|
|
modifiers: KeyModifiers::CONTROL,
|
|
|
|
|
kind: KeyEventKind::Press,
|
|
|
|
|
state: _state,
|
2026-01-20 17:19:13 -05:00
|
|
|
} => Ok(Action::Quit),
|
|
|
|
|
_ => Ok(Action::Noop),
|
2026-01-18 10:09:28 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|