Compare commits

...

2 Commits

Author SHA1 Message Date
4d81cd51a6 [tui] Add ComponentOf trait.
I think it will help with fetching a component by type from the
Components vector attached to App?
2026-01-20 20:50:36 -05:00
7149ad0118 [tui] Add debug console.
The input will not be handled correctly until #8 is complete, but the
input logic is there and was tested.

Fixes #5.
2026-01-20 20:50:27 -05:00
7 changed files with 333 additions and 69 deletions

129
Cargo.lock generated
View File

@ -23,6 +23,15 @@ version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923"
[[package]]
name = "android_system_properties"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
dependencies = [
"libc",
]
[[package]]
name = "anstream"
version = "0.6.21"
@ -216,6 +225,17 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
[[package]]
name = "chrono"
version = "0.4.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fac4744fb15ae8337dc853fee7fb3f4e48c0fbaa23d0afe49c447b4fab126118"
dependencies = [
"iana-time-zone",
"num-traits",
"windows-link",
]
[[package]]
name = "clang-format"
version = "0.3.0"
@ -280,6 +300,7 @@ dependencies = [
"log",
"ratatui",
"syntect",
"tui-logger",
"tui-tree-widget",
]
@ -351,6 +372,12 @@ dependencies = [
"unicode-segmentation",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
[[package]]
name = "cpufeatures"
version = "0.2.17"
@ -729,6 +756,16 @@ dependencies = [
"syn 2.0.114",
]
[[package]]
name = "env_filter"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bf3c259d255ca70051b30e2e95b5446cdb8949ac4cd22c0d7fd634d89f568e2"
dependencies = [
"log",
"regex",
]
[[package]]
name = "equivalent"
version = "1.0.2"
@ -927,6 +964,30 @@ version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "iana-time-zone"
version = "0.1.64"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33e57f83510bb73707521ebaffa789ec8caf86f9657cad665b092b581d40e9fb"
dependencies = [
"android_system_properties",
"core-foundation-sys",
"iana-time-zone-haiku",
"js-sys",
"log",
"wasm-bindgen",
"windows-core",
]
[[package]]
name = "iana-time-zone-haiku"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
dependencies = [
"cc",
]
[[package]]
name = "ident_case"
version = "1.0.1"
@ -2151,6 +2212,21 @@ dependencies = [
"time-core",
]
[[package]]
name = "tui-logger"
version = "0.18.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9384df20a5244a6ab204bc4b6959b41f37f0ee7b5e0f2feb7a8a78f58e684d06"
dependencies = [
"chrono",
"env_filter",
"lazy_static",
"log",
"parking_lot",
"ratatui",
"unicode-segmentation",
]
[[package]]
name = "tui-tree-widget"
version = "0.24.0"
@ -2421,12 +2497,65 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows-core"
version = "0.62.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb"
dependencies = [
"windows-implement",
"windows-interface",
"windows-link",
"windows-result",
"windows-strings",
]
[[package]]
name = "windows-implement"
version = "0.60.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.114",
]
[[package]]
name = "windows-interface"
version = "0.59.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.114",
]
[[package]]
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
[[package]]
name = "windows-result"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5"
dependencies = [
"windows-link",
]
[[package]]
name = "windows-strings"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091"
dependencies = [
"windows-link",
]
[[package]]
name = "windows-sys"
version = "0.60.2"

View File

@ -14,6 +14,7 @@ clap = { version = "4.5.54", features = ["derive"] }
ratatui = "0.30.0"
anyhow = "1.0.100"
tui-tree-widget = "0.24.0"
tui-logger = "0.18.1"
edtui = "0.11.1"
[build-dependencies]

View File

@ -2,8 +2,10 @@ mod app;
mod component;
mod editor;
mod explorer;
mod logger;
use anyhow::{Context, Result};
use log::{LevelFilter, debug};
use ratatui::Terminal;
use ratatui::backend::CrosstermBackend;
use ratatui::crossterm::event::{
@ -12,7 +14,11 @@ use ratatui::crossterm::event::{
use ratatui::crossterm::terminal::{
EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode,
};
use std::env;
use std::io::{Stdout, stdout};
use tui_logger::{
TuiLoggerFile, TuiLoggerLevelOutput, init_logger, set_default_level, set_log_file,
};
pub struct Tui {
terminal: Terminal<CrosstermBackend<Stdout>>,
@ -21,6 +27,19 @@ pub struct Tui {
impl Tui {
pub fn new(root_path: std::path::PathBuf) -> Result<Self> {
init_logger(LevelFilter::Trace)?;
set_default_level(LevelFilter::Trace);
debug!(target:"Tui", "Logging initialized");
let mut dir = env::temp_dir();
dir.push("clide.log");
let file_options = TuiLoggerFile::new(dir.to_str().unwrap())
.output_level(Some(TuiLoggerLevelOutput::Abbreviated))
.output_file(false)
.output_separator(':');
set_log_file(file_options);
debug!(target:"Tui", "Logging to file: {}", dir.to_str().unwrap());
Ok(Self {
terminal: Terminal::new(CrosstermBackend::new(stdout()))?,
root_path,

View File

@ -1,7 +1,9 @@
use crate::tui::component::{Action, Component};
use crate::tui::editor::Editor;
use crate::tui::explorer::Explorer;
use crate::tui::logger::Logger;
use anyhow::{Context, Result, anyhow, bail};
use log::{debug, error, info, trace, warn};
use ratatui::buffer::Buffer;
use ratatui::crossterm::event;
use ratatui::crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
@ -12,12 +14,69 @@ use ratatui::{DefaultTerminal, symbols};
use std::path::PathBuf;
use std::time::Duration;
// 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?
// TODO: Need a way to dynamically run Component methods on all widgets.
pub enum AppComponents<'a> {
AppEditor(Editor),
AppExplorer(Explorer<'a>),
AppLogger(Logger),
AppComponent(Box<dyn Component>),
}
/// Usage: get_component_mut::<Editor>() OR get_component::<Editor>()
///
/// Implementing this trait for each AppComponent allows for easy lookup in the vector.
trait ComponentOf<T> {
fn as_ref(&self) -> Option<&T>;
fn as_mut(&mut self) -> Option<&mut T>;
}
impl<'a> ComponentOf<Logger> for AppComponents<'a> {
fn as_ref(&self) -> Option<&Logger> {
if let AppComponents::AppLogger(ref e) = *self {
return Some(e);
}
None
}
fn as_mut(&mut self) -> Option<&mut Logger> {
if let AppComponents::AppLogger(ref mut e) = *self {
return Some(e);
}
None
}
}
impl<'a> ComponentOf<Editor> for AppComponents<'a> {
fn as_ref(&self) -> Option<&Editor> {
if let AppComponents::AppEditor(ref e) = *self {
return Some(e);
}
None
}
fn as_mut(&mut self) -> Option<&mut Editor> {
if let AppComponents::AppEditor(ref mut e) = *self {
return Some(e);
}
None
}
}
impl<'a> ComponentOf<Explorer<'a>> for AppComponents<'a> {
fn as_ref(&self) -> Option<&Explorer<'a>> {
if let AppComponents::AppExplorer(ref e) = *self {
return Some(e);
}
None
}
fn as_mut(&mut self) -> Option<&mut Explorer<'a>> {
if let AppComponents::AppExplorer(ref mut e) = *self {
return Some(e);
}
None
}
}
pub struct App<'a> {
components: Vec<AppComponents<'a>>,
}
@ -28,9 +87,10 @@ impl<'a> App<'a> {
components: vec![
AppComponents::AppExplorer(Explorer::new(&root_path)?),
AppComponents::AppEditor(Editor::new()),
AppComponents::AppLogger(Logger::new()),
],
};
app.get_editor_mut()
app.get_component_mut::<Editor>()
.unwrap()
.set_contents(&root_path.join("src/tui/app.rs"))
.context(format!(
@ -40,44 +100,18 @@ impl<'a> App<'a> {
Ok(app)
}
fn get_explorer(&self) -> Result<&Explorer<'a>> {
for component in &self.components {
if let AppComponents::AppExplorer(explorer) = component {
return Ok(explorer);
}
}
bail!("Failed to find project explorer widget.")
fn get_component<T>(&self) -> Option<&T>
where
AppComponents<'a>: ComponentOf<T>,
{
self.components.iter().find_map(|c| c.as_ref())
}
fn get_explorer_mut(&mut self) -> Result<&mut Explorer<'a>> {
for component in &mut self.components {
if let AppComponents::AppExplorer(explorer) = component {
return Ok(explorer);
}
}
bail!("Failed to find project explorer widget.")
}
fn get_editor(&self) -> Option<&Editor> {
for component in &self.components {
if let AppComponents::AppEditor(editor) = component {
return Some(editor);
}
}
// There is no editor currently opened.
None
}
fn get_editor_mut(&mut self) -> Option<&mut Editor> {
for component in &mut self.components {
if let AppComponents::AppEditor(editor) = component {
return Some(editor);
}
}
// There is no editor currently opened.
None
fn get_component_mut<T>(&mut self) -> Option<&mut T>
where
AppComponents<'a>: ComponentOf<T>,
{
self.components.iter_mut().find_map(|c| c.as_mut())
}
pub fn run(mut self, mut terminal: DefaultTerminal) -> Result<()> {
@ -114,7 +148,7 @@ impl<'a> App<'a> {
fn draw_tabs(&self, area: Rect, buf: &mut Buffer) {
// Determine the tab title from the current file (or use a fallback).
let mut title: Option<&str> = None;
if let Some(editor) = self.get_editor() {
if let Some(editor) = self.get_component::<Editor>() {
title = editor
.file_path
.as_ref()
@ -133,31 +167,16 @@ impl<'a> App<'a> {
.render(area, buf);
}
fn draw_terminal(&self, area: Rect, buf: &mut Buffer) {
// TODO: Title should be detected shell name
// TODO: Contents should be shell output
Paragraph::new("shaun@pc:~/Code/clide$ ")
.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);
}
/// 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) -> Result<()> {
// Use the currently selected TreeItem or get an absolute path to this source file.
let selected_pathbuf = match self.get_explorer()?.selected() {
let selected_pathbuf = match self.get_component::<Explorer>().unwrap().selected() {
Ok(path) => PathBuf::from(path),
Err(_) => PathBuf::from(std::path::absolute(file!())?.to_string_lossy().to_string()),
};
let editor = self
.get_editor_mut()
.get_component_mut::<Editor>()
.context("Failed to get active editor while refreshing contents.")?;
if let Some(current_file_path) = editor.file_path.clone() {
if selected_pathbuf == current_file_path || !selected_pathbuf.is_file() {
@ -201,22 +220,27 @@ impl<'a> Widget for &mut App<'a> {
.split(horizontal[1]);
self.draw_status(vertical[0], buf);
self.draw_terminal(vertical[2], buf);
if let Ok(explorer) = self.get_explorer_mut() {
// TODO: What to do about errors during rendering?
// Once there is a debug console, maybe log it and discard? Panic isn't great.
explorer
.render(horizontal[0], buf)
.expect("Failed to render Explorer");
}
self.draw_tabs(editor_layout[0], buf);
self.get_editor_mut().unwrap().render(editor_layout[1], buf);
let id = self.id().to_string();
for component in &mut self.components {
match component {
AppComponents::AppEditor(editor) => editor.render(editor_layout[1], buf),
AppComponents::AppExplorer(explorer) => {
explorer
.render(horizontal[0], buf)
.context("Failed to render Explorer")
.unwrap_or_else(|e| error!(target:id.as_str(), "{}", e));
}
AppComponents::AppLogger(logger) => logger.render(vertical[2], buf),
AppComponents::AppComponent(_) => {}
}
}
}
}
impl<'a> Component for App<'a> {
fn id(&self) -> &str {
"app"
"App"
}
/// TODO: Get active widget with some Component trait function helper?
@ -245,6 +269,7 @@ impl<'a> Component for App<'a> {
AppComponents::AppEditor(editor) => editor.handle_event(event.clone())?,
AppComponents::AppExplorer(explorer) => explorer.handle_event(event.clone())?,
AppComponents::AppComponent(comp) => comp.handle_event(event.clone())?,
AppComponents::AppLogger(logger) => logger.handle_event(event.clone())?,
};
// Actions returned here abort the input handling iteration.
match action {
@ -258,6 +283,20 @@ impl<'a> Component for App<'a> {
/// Handles key events for the App Component only.
fn handle_key_events(&mut self, key: KeyEvent) -> Result<Action> {
match key {
KeyEvent {
code: KeyCode::Char('l'),
modifiers: KeyModifiers::CONTROL,
kind: KeyEventKind::Press,
state: _state,
} => {
// Some example logs for testing.
error!(target:self.id(), "an error");
warn!(target:self.id(), "a warning");
info!(target:self.id(), "a two line info\nsecond line");
debug!(target:self.id(), "a debug");
trace!(target:self.id(), "a trace");
Ok(Action::Noop)
}
KeyEvent {
code: KeyCode::Char('c'),
modifiers: KeyModifiers::CONTROL,

View File

@ -1,6 +1,6 @@
use crate::tui::component::{Action, Component};
use anyhow::{bail, Context, Result};
use anyhow::{Context, Result, bail};
use edtui::{
EditorEventHandler, EditorState, EditorTheme, EditorView, LineNumbers, Lines, SyntaxHighlighter,
};
@ -87,7 +87,7 @@ impl Widget for &mut Editor {
impl Component for Editor {
fn id(&self) -> &str {
"editor"
"Editor"
}
fn handle_event(&mut self, event: Event) -> Result<Action> {

View File

@ -1,5 +1,5 @@
use crate::tui::component::{Action, Component};
use anyhow::{bail, Context, Result};
use anyhow::{Context, Result, bail};
use ratatui::buffer::Buffer;
use ratatui::crossterm::event::{Event, KeyCode, KeyEvent, MouseEvent, MouseEventKind};
use ratatui::layout::{Alignment, Position, Rect};
@ -113,7 +113,7 @@ impl<'a> Explorer<'a> {
impl<'a> Component for Explorer<'a> {
fn id(&self) -> &str {
"explorer"
"Explorer"
}
fn handle_event(&mut self, event: Event) -> Result<Action> {
if let Some(key_event) = event.as_key_event() {

76
src/tui/logger.rs Normal file
View File

@ -0,0 +1,76 @@
use crate::tui::component::{Action, Component};
use ratatui::buffer::Buffer;
use ratatui::crossterm::event::{Event, KeyCode, KeyEvent};
use ratatui::layout::Rect;
use ratatui::style::{Color, Style};
use ratatui::widgets::Widget;
use tui_logger::{TuiLoggerLevelOutput, TuiLoggerSmartWidget, TuiWidgetEvent, TuiWidgetState};
/// Any log written as info!(target:self.id(), "message") will work with this logger.
/// The logger is bound to info!, debug!, error!, trace! macros within Tui::new().
pub struct Logger {
state: TuiWidgetState,
}
impl Logger {
pub fn new() -> Self {
Self {
state: TuiWidgetState::new(),
}
}
}
impl Widget for &Logger {
fn render(self, area: Rect, buf: &mut Buffer)
where
Self: Sized,
{
// TODO: Use output_file?
TuiLoggerSmartWidget::default()
.style_error(Style::default().fg(Color::Red))
.style_debug(Style::default().fg(Color::Green))
.style_warn(Style::default().fg(Color::Yellow))
.style_trace(Style::default().fg(Color::Magenta))
.style_info(Style::default().fg(Color::Cyan))
.output_separator(':')
.output_timestamp(Some("%H:%M:%S".to_string()))
.output_level(Some(TuiLoggerLevelOutput::Abbreviated))
.output_target(true)
.output_file(true)
.output_line(true)
.state(&self.state)
.render(area, buf);
}
}
impl Component for Logger {
fn id(&self) -> &str {
"Logger"
}
fn handle_event(&mut self, event: Event) -> anyhow::Result<Action> {
if let Some(key_event) = event.as_key_event() {
return self.handle_key_events(key_event);
}
Ok(Action::Noop)
}
fn handle_key_events(&mut self, key: KeyEvent) -> anyhow::Result<Action> {
match key.code {
KeyCode::Char(' ') => self.state.transition(TuiWidgetEvent::SpaceKey),
KeyCode::Esc => self.state.transition(TuiWidgetEvent::EscapeKey),
KeyCode::PageUp => self.state.transition(TuiWidgetEvent::PrevPageKey),
KeyCode::PageDown => self.state.transition(TuiWidgetEvent::NextPageKey),
KeyCode::Up => self.state.transition(TuiWidgetEvent::UpKey),
KeyCode::Down => self.state.transition(TuiWidgetEvent::DownKey),
KeyCode::Left => self.state.transition(TuiWidgetEvent::LeftKey),
KeyCode::Right => self.state.transition(TuiWidgetEvent::RightKey),
KeyCode::Char('+') => self.state.transition(TuiWidgetEvent::PlusKey),
KeyCode::Char('-') => self.state.transition(TuiWidgetEvent::MinusKey),
KeyCode::Char('h') => self.state.transition(TuiWidgetEvent::HideKey),
KeyCode::Char('f') => self.state.transition(TuiWidgetEvent::FocusKey),
_ => (),
}
Ok(Action::Pass)
}
}