diff --git a/src/gui.rs b/src/gui.rs index b80df58..6aefef4 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -1,11 +1,12 @@ use anyhow::Result; use cxx_qt_lib::QString; +use log::trace; pub mod colors; pub mod filesystem; pub fn run(root_path: std::path::PathBuf) -> Result<()> { - println!("Starting the GUI editor at {:?}", root_path); + trace!(target:"gui::run()", "Starting the GUI editor at {root_path:?}"); use cxx_qt_lib::{QGuiApplication, QQmlApplicationEngine, QUrl}; diff --git a/src/gui/filesystem.rs b/src/gui/filesystem.rs index 2c39d7f..557203b 100644 --- a/src/gui/filesystem.rs +++ b/src/gui/filesystem.rs @@ -73,10 +73,10 @@ impl qobject::FileSystem { return QString::default(); } if !fs::metadata(path.to_string()) - .expect(format!("Failed to get file metadata {}", path).as_str()) + .expect(format!("Failed to get file metadata {path:?}").as_str()) .is_file() { - warn!("Attempted to open file {} that is not a valid file", path); + warn!(target:"FileSystem", "Attempted to open file {path:?} that is not a valid file"); return QString::default(); } let ss = SyntaxSet::load_defaults_nonewlines(); @@ -118,7 +118,7 @@ impl qobject::FileSystem { fn set_directory(self: std::pin::Pin<&mut Self>, path: &QString) -> QModelIndex { if !path.is_empty() && fs::metadata(path.to_string()) - .expect(format!("Failed to get metadata for path {}", path).as_str()) + .expect(format!("Failed to get metadata for path {path:?}").as_str()) .is_dir() { self.set_root_path(path) diff --git a/src/main.rs b/src/main.rs index e00528d..b672903 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,8 @@ +use crate::tui::Tui; use anyhow::{Context, Result}; use clap::Parser; -use ratatui::Terminal; -use ratatui::backend::CrosstermBackend; -use std::io::stdout; +use log::{info, trace}; use std::process::{Command, Stdio}; -use crate::tui::Tui; pub mod gui; pub mod tui; @@ -39,14 +37,21 @@ fn main() -> Result<()> { dirs::home_dir().context("Failed to obtain home directory")?, ), }; + info!(target:"main()", "Root path detected: {root_path:?}"); match args.gui { - true => gui::run(root_path), + true => { + trace!(target:"main()", "Starting GUI"); + gui::run(root_path) + } false => match args.tui { // Open the TUI editor if requested, otherwise use the QML GUI by default. - true => Ok(Tui::new(root_path)?.start()?), + true => { + trace!(target:"main()", "Starting TUI"); + Ok(Tui::new(root_path)?.start()?) + } false => { - // Relaunch the CLIDE GUI in a separate process. + trace!(target:"main()", "Starting GUI in a new process"); Command::new(std::env::current_exe()?) .args(&["--gui", root_path.to_str().unwrap()]) .stdout(Stdio::null()) diff --git a/src/tui.rs b/src/tui.rs index 83fc16e..356f9e0 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -5,7 +5,7 @@ mod explorer; mod logger; use anyhow::{Context, Result}; -use log::{LevelFilter, debug}; +use log::{LevelFilter, debug, info}; use ratatui::Terminal; use ratatui::backend::CrosstermBackend; use ratatui::crossterm::event::{ @@ -38,7 +38,7 @@ impl Tui { .output_file(false) .output_separator(':'); set_log_file(file_options); - debug!(target:"Tui", "Logging to file: {}", dir.to_str().unwrap()); + debug!(target:"Tui", "Logging to file: {dir:?}"); Ok(Self { terminal: Terminal::new(CrosstermBackend::new(stdout()))?, @@ -47,7 +47,7 @@ impl Tui { } pub fn start(self) -> Result<()> { - println!("Starting the TUI editor at {:?}", self.root_path); + info!(target:"Tui", "Starting the TUI editor at {:?}", self.root_path); ratatui::crossterm::execute!( stdout(), EnterAlternateScreen, @@ -64,6 +64,7 @@ impl Tui { } fn stop() -> Result<()> { + info!(target:"Tui", "Stopping the TUI editor"); disable_raw_mode()?; ratatui::crossterm::execute!( stdout(), diff --git a/src/tui/app.rs b/src/tui/app.rs index eae2494..55b319e 100644 --- a/src/tui/app.rs +++ b/src/tui/app.rs @@ -2,14 +2,14 @@ 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 anyhow::{Context, Result, 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}; use ratatui::layout::{Constraint, Direction, Layout, Rect}; use ratatui::prelude::{Color, Style, Widget}; -use ratatui::widgets::{Block, Borders, Padding, Paragraph, Tabs, Wrap}; +use ratatui::widgets::{Block, Borders, Padding, Tabs}; use ratatui::{DefaultTerminal, symbols}; use std::path::PathBuf; use std::time::Duration; @@ -27,56 +27,11 @@ pub enum AppComponents<'a> { /// Usage: get_component_mut::() OR get_component::() /// /// Implementing this trait for each AppComponent allows for easy lookup in the vector. -trait ComponentOf { +pub(crate) trait ComponentOf { fn as_ref(&self) -> Option<&T>; fn as_mut(&mut self) -> Option<&mut T>; } -impl<'a> ComponentOf 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 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> 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>, } @@ -94,8 +49,7 @@ impl<'a> App<'a> { .unwrap() .set_contents(&root_path.join("src/tui/app.rs")) .context(format!( - "Failed to initialize editor contents to path: {}", - root_path.to_string_lossy() + "Failed to initialize editor contents to path: {root_path:?}" ))?; Ok(app) } diff --git a/src/tui/editor.rs b/src/tui/editor.rs index 7dbf5fb..d8c695e 100644 --- a/src/tui/editor.rs +++ b/src/tui/editor.rs @@ -1,5 +1,6 @@ use crate::tui::component::{Action, Component}; +use crate::tui::app::{AppComponents, ComponentOf}; use anyhow::{Context, Result, bail}; use edtui::{ EditorEventHandler, EditorState, EditorTheme, EditorView, LineNumbers, Lines, SyntaxHighlighter, @@ -22,6 +23,21 @@ pub struct Editor { syntax_set: SyntaxSet, } +impl<'a> ComponentOf 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 Editor { pub fn new() -> Self { Editor { diff --git a/src/tui/explorer.rs b/src/tui/explorer.rs index 751d725..7e83632 100644 --- a/src/tui/explorer.rs +++ b/src/tui/explorer.rs @@ -1,3 +1,4 @@ +use crate::tui::app::{AppComponents, ComponentOf}; use crate::tui::component::{Action, Component}; use anyhow::{Context, Result, bail}; use ratatui::buffer::Buffer; @@ -16,6 +17,21 @@ pub struct Explorer<'a> { tree_state: TreeState, } +impl<'a> ComponentOf> 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 + } +} + impl<'a> Explorer<'a> { pub fn new(path: &std::path::PathBuf) -> Result { let explorer = Explorer { diff --git a/src/tui/logger.rs b/src/tui/logger.rs index 53f2cd7..70a7d23 100644 --- a/src/tui/logger.rs +++ b/src/tui/logger.rs @@ -1,3 +1,4 @@ +use crate::tui::app::{AppComponents, ComponentOf}; use crate::tui::component::{Action, Component}; use ratatui::buffer::Buffer; use ratatui::crossterm::event::{Event, KeyCode, KeyEvent}; @@ -12,6 +13,21 @@ pub struct Logger { state: TuiWidgetState, } +impl<'a> ComponentOf 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 Logger { pub fn new() -> Self { Self {