Add macros for logging.
This commit is contained in:
@@ -2,19 +2,18 @@
|
||||
//
|
||||
// SPDX-License-Identifier: GNU General Public License v3.0 or later
|
||||
|
||||
use libclide_macros::log_id;
|
||||
use ratatui::buffer::Buffer;
|
||||
use ratatui::layout::{Constraint, Direction, Layout, Rect};
|
||||
use ratatui::text::{Line, Span};
|
||||
use ratatui::widgets::{Block, Borders, Clear, Padding, Paragraph, Widget, Wrap};
|
||||
|
||||
#[log_id]
|
||||
pub struct About {}
|
||||
|
||||
impl About {
|
||||
#[allow(unused)]
|
||||
pub const ID: &str = "About";
|
||||
|
||||
pub fn new() -> Self {
|
||||
// trace!(target:Self::id(), "Building {}", Self::id());
|
||||
// libclide::trace!("Building {}", Self::id());
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ use crate::tui::explorer::Explorer;
|
||||
use crate::tui::logger::Logger;
|
||||
use crate::tui::menu_bar::MenuBar;
|
||||
use anyhow::{Context, Result};
|
||||
use log::{error, info, trace};
|
||||
use libclide_macros::log_id;
|
||||
use ratatui::DefaultTerminal;
|
||||
use ratatui::buffer::Buffer;
|
||||
use ratatui::crossterm::event;
|
||||
@@ -30,6 +30,7 @@ pub enum AppComponent {
|
||||
MenuBar,
|
||||
}
|
||||
|
||||
#[log_id]
|
||||
pub struct App<'a> {
|
||||
editor_tab: EditorTab,
|
||||
explorer: Explorer<'a>,
|
||||
@@ -40,10 +41,8 @@ pub struct App<'a> {
|
||||
}
|
||||
|
||||
impl<'a> App<'a> {
|
||||
pub const ID: &'static str = "App";
|
||||
|
||||
pub fn new(root_path: PathBuf) -> Result<Self> {
|
||||
trace!(target:Self::ID, "Building {}", Self::ID);
|
||||
libclide::trace!(target:Self::ID, "Building {}", Self::ID);
|
||||
let app = Self {
|
||||
editor_tab: EditorTab::new(),
|
||||
explorer: Explorer::new(&root_path)?,
|
||||
@@ -57,13 +56,13 @@ impl<'a> App<'a> {
|
||||
|
||||
/// Logic that should be executed once on application startup.
|
||||
pub fn start(&mut self) -> Result<()> {
|
||||
trace!(target:Self::ID, "Starting App");
|
||||
libclide::trace!(target:Self::ID, "Starting App");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn run(mut self, mut terminal: DefaultTerminal) -> Result<()> {
|
||||
self.start()?;
|
||||
trace!(target:Self::ID, "Entering App run loop");
|
||||
libclide::trace!(target:Self::ID, "Entering App run loop");
|
||||
loop {
|
||||
terminal.draw(|f| {
|
||||
f.render_widget(&mut self, f.area());
|
||||
@@ -89,7 +88,7 @@ impl<'a> App<'a> {
|
||||
Some(editor) => editor.component_state.help_text.clone(),
|
||||
None => {
|
||||
if !self.editor_tab.is_empty() {
|
||||
error!(target:Self::ID, "Failed to get Editor while drawing bottom status bar");
|
||||
libclide::error!(target:Self::ID, "Failed to get Editor while drawing bottom status bar");
|
||||
}
|
||||
"Failed to get current Editor while getting widget help text".to_string()
|
||||
}
|
||||
@@ -113,26 +112,26 @@ impl<'a> App<'a> {
|
||||
}
|
||||
|
||||
fn clear_focus(&mut self) {
|
||||
info!(target:Self::ID, "Clearing all widget focus");
|
||||
libclide::info!(target:Self::ID, "Clearing all widget focus");
|
||||
self.explorer.component_state.set_focus(Focus::Inactive);
|
||||
self.explorer.component_state.set_focus(Focus::Inactive);
|
||||
self.logger.component_state.set_focus(Focus::Inactive);
|
||||
self.menu_bar.component_state.set_focus(Focus::Inactive);
|
||||
match self.editor_tab.current_editor_mut() {
|
||||
None => {
|
||||
error!(target:Self::ID, "Failed to get current Editor while clearing focus")
|
||||
libclide::error!(target:Self::ID, "Failed to get current Editor while clearing focus")
|
||||
}
|
||||
Some(editor) => editor.component_state.set_focus(Focus::Inactive),
|
||||
}
|
||||
}
|
||||
|
||||
fn change_focus(&mut self, focus: AppComponent) {
|
||||
info!(target:Self::ID, "Changing widget focus to {:?}", focus);
|
||||
libclide::info!(target:Self::ID, "Changing widget focus to {:?}", focus);
|
||||
self.clear_focus();
|
||||
match focus {
|
||||
AppComponent::Editor => match self.editor_tab.current_editor_mut() {
|
||||
None => {
|
||||
error!(target:Self::ID, "Failed to get current Editor while changing focus")
|
||||
libclide::error!(target:Self::ID, "Failed to get current Editor while changing focus")
|
||||
}
|
||||
Some(editor) => editor.component_state.set_focus(Focus::Active),
|
||||
},
|
||||
@@ -275,13 +274,13 @@ impl<'a> Component for App<'a> {
|
||||
Action::Quit | Action::Handled => Ok(action),
|
||||
Action::Save => match self.editor_tab.current_editor_mut() {
|
||||
None => {
|
||||
error!(target:Self::ID, "Failed to get current editor while handling App Action::Save");
|
||||
libclide::error!(target:Self::ID, "Failed to get current editor while handling App Action::Save");
|
||||
Ok(Action::Noop)
|
||||
}
|
||||
Some(editor) => match editor.save() {
|
||||
Ok(_) => Ok(Action::Handled),
|
||||
Err(e) => {
|
||||
error!(target:Self::ID, "Failed to save editor contents: {e}");
|
||||
libclide::error!(target:Self::ID, "Failed to save editor contents: {e}");
|
||||
Ok(Action::Noop)
|
||||
}
|
||||
},
|
||||
@@ -300,14 +299,14 @@ impl<'a> Component for App<'a> {
|
||||
Err(_) => Ok(Action::Noop),
|
||||
},
|
||||
Action::ReloadFile => {
|
||||
trace!(target:Self::ID, "Reloading file for current editor");
|
||||
libclide::trace!(target:Self::ID, "Reloading file for current editor");
|
||||
if let Some(editor) = self.editor_tab.current_editor_mut() {
|
||||
editor
|
||||
.reload_contents()
|
||||
.map(|_| Action::Handled)
|
||||
.context("Failed to handle Action::ReloadFile")
|
||||
} else {
|
||||
error!(target:Self::ID, "Failed to get current editor while handling App Action::ReloadFile");
|
||||
libclide::error!(target:Self::ID, "Failed to get current editor while handling App Action::ReloadFile");
|
||||
Ok(Action::Noop)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ use crate::tui::component::Focus::Inactive;
|
||||
use Focus::Active;
|
||||
use anyhow::Result;
|
||||
use libclide::theme::colors::Colors;
|
||||
use log::trace;
|
||||
use libclide_macros::log_id;
|
||||
use ratatui::crossterm::event::{Event, KeyEvent, MouseEvent};
|
||||
use ratatui::style::Color;
|
||||
|
||||
@@ -63,6 +63,7 @@ pub trait Component {
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
#[log_id]
|
||||
pub struct ComponentState {
|
||||
pub(crate) focus: Focus,
|
||||
pub(crate) vis: Visibility,
|
||||
@@ -75,7 +76,7 @@ impl ComponentState {
|
||||
}
|
||||
|
||||
fn new() -> Self {
|
||||
trace!(target:Self::id(), "Building {}", Self::id());
|
||||
libclide::trace!(target:Self::id(), "Building {}", Self::id());
|
||||
Self {
|
||||
focus: Active,
|
||||
vis: Visibility::Visible,
|
||||
|
||||
@@ -7,7 +7,7 @@ use anyhow::{Context, Result, bail};
|
||||
use edtui::{
|
||||
EditorEventHandler, EditorState, EditorTheme, EditorView, LineNumbers, Lines, SyntaxHighlighter,
|
||||
};
|
||||
use log::{error, trace};
|
||||
use libclide_macros::log_id;
|
||||
use ratatui::buffer::Buffer;
|
||||
use ratatui::crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
|
||||
use ratatui::layout::{Alignment, Rect};
|
||||
@@ -16,6 +16,7 @@ use ratatui::widgets::{Block, Borders, Padding, Widget};
|
||||
use std::path::PathBuf;
|
||||
use syntect::parsing::SyntaxSet;
|
||||
|
||||
#[log_id]
|
||||
pub struct Editor {
|
||||
pub state: EditorState,
|
||||
pub event_handler: EditorEventHandler,
|
||||
@@ -25,10 +26,8 @@ pub struct Editor {
|
||||
}
|
||||
|
||||
impl Editor {
|
||||
pub const ID: &str = "Editor";
|
||||
|
||||
pub fn new(path: &std::path::Path) -> Self {
|
||||
trace!(target:Self::ID, "Building {}", Self::ID);
|
||||
libclide::trace!(target:Self::ID, "Building {}", Self::ID);
|
||||
Editor {
|
||||
state: EditorState::default(),
|
||||
event_handler: EditorEventHandler::default(),
|
||||
@@ -42,10 +41,10 @@ impl Editor {
|
||||
}
|
||||
|
||||
pub fn reload_contents(&mut self) -> Result<()> {
|
||||
trace!(target:Self::ID, "Reloading editor file contents {:?}", self.file_path);
|
||||
libclide::trace!(target:Self::ID, "Reloading editor file contents {:?}", self.file_path);
|
||||
match self.file_path.clone() {
|
||||
None => {
|
||||
error!(target:Self::ID, "Failed to reload editor contents with None file_path");
|
||||
libclide::error!(target:Self::ID, "Failed to reload editor contents with None file_path");
|
||||
bail!("Failed to reload editor contents with None file_path")
|
||||
}
|
||||
Some(path) => self.set_contents(&path),
|
||||
@@ -53,7 +52,7 @@ impl Editor {
|
||||
}
|
||||
|
||||
pub fn set_contents(&mut self, path: &std::path::Path) -> Result<()> {
|
||||
trace!(target:Self::ID, "Setting Editor contents from path {:?}", path);
|
||||
libclide::trace!(target:Self::ID, "Setting Editor contents from path {:?}", path);
|
||||
if let Ok(contents) = std::fs::read_to_string(path) {
|
||||
let lines: Vec<_> = contents
|
||||
.lines()
|
||||
@@ -69,10 +68,10 @@ impl Editor {
|
||||
|
||||
pub fn save(&self) -> Result<()> {
|
||||
if let Some(path) = &self.file_path {
|
||||
trace!(target:Self::ID, "Saving Editor contents {:?}", path);
|
||||
libclide::trace!(target:Self::ID, "Saving Editor contents {:?}", path);
|
||||
return std::fs::write(path, self.state.lines.to_string()).map_err(|e| e.into());
|
||||
};
|
||||
error!(target:Self::ID, "Failed saving Editor contents; file_path was None");
|
||||
libclide::error!(target:Self::ID, "Failed saving Editor contents; file_path was None");
|
||||
bail!("File not saved. No file path set.")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
use crate::tui::component::{Action, Component, Focus, FocusState};
|
||||
use crate::tui::editor::Editor;
|
||||
use anyhow::{Context, Result, anyhow};
|
||||
use log::{error, info, trace, warn};
|
||||
use libclide_macros::log_id;
|
||||
use ratatui::buffer::Buffer;
|
||||
use ratatui::crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
|
||||
use ratatui::layout::Rect;
|
||||
@@ -16,6 +16,7 @@ use std::collections::HashMap;
|
||||
// Render the tabs with keys as titles
|
||||
// Tab keys can be file names.
|
||||
// Render the editor using the key as a reference for lookup
|
||||
#[log_id]
|
||||
pub struct EditorTab {
|
||||
pub(crate) editors: HashMap<String, Editor>,
|
||||
tab_order: Vec<String>,
|
||||
@@ -23,10 +24,8 @@ pub struct EditorTab {
|
||||
}
|
||||
|
||||
impl EditorTab {
|
||||
pub const ID: &str = "EditorTab";
|
||||
|
||||
pub fn new() -> Self {
|
||||
trace!(target:Self::ID, "Building {}", Self::ID);
|
||||
libclide::trace!(target:Self::ID, "Building {}", Self::ID);
|
||||
Self {
|
||||
editors: HashMap::new(),
|
||||
tab_order: Vec::new(),
|
||||
@@ -36,7 +35,7 @@ impl EditorTab {
|
||||
|
||||
pub fn next_editor(&mut self) {
|
||||
let next = (self.current_editor + 1) % self.tab_order.len();
|
||||
trace!(target:Self::ID, "Moving from {} to next editor tab at {}", self.current_editor, next);
|
||||
libclide::trace!(target:Self::ID, "Moving from {} to next editor tab at {}", self.current_editor, next);
|
||||
self.set_tab_focus(Focus::Active, next);
|
||||
self.current_editor = next;
|
||||
}
|
||||
@@ -46,7 +45,7 @@ impl EditorTab {
|
||||
.current_editor
|
||||
.checked_sub(1)
|
||||
.unwrap_or(self.tab_order.len() - 1);
|
||||
trace!(target:Self::ID, "Moving from {} to previous editor tab at {}", self.current_editor, prev);
|
||||
libclide::trace!(target:Self::ID, "Moving from {} to previous editor tab at {}", self.current_editor, prev);
|
||||
self.set_tab_focus(Focus::Active, prev);
|
||||
self.current_editor = prev;
|
||||
}
|
||||
@@ -55,7 +54,7 @@ impl EditorTab {
|
||||
match self.tab_order.get(index) {
|
||||
None => {
|
||||
if !self.tab_order.is_empty() {
|
||||
error!(target:Self::ID, "Failed to get editor tab key with invalid index {index}");
|
||||
libclide::error!(target:Self::ID, "Failed to get editor tab key with invalid index {index}");
|
||||
}
|
||||
None
|
||||
}
|
||||
@@ -73,15 +72,15 @@ impl EditorTab {
|
||||
}
|
||||
|
||||
pub fn set_current_tab_focus(&mut self, focus: Focus) {
|
||||
trace!(target:Self::ID, "Setting current tab {} focus to {:?}", self.current_editor, focus);
|
||||
libclide::trace!(target:Self::ID, "Setting current tab {} focus to {:?}", self.current_editor, focus);
|
||||
self.set_tab_focus(focus, self.current_editor)
|
||||
}
|
||||
|
||||
pub fn set_tab_focus(&mut self, focus: Focus, index: usize) {
|
||||
trace!(target:Self::ID, "Setting tab {} focus to {:?}", index, focus);
|
||||
libclide::trace!(target:Self::ID, "Setting tab {} focus to {:?}", index, focus);
|
||||
if focus == Focus::Active && index != self.current_editor {
|
||||
// If we are setting another tab to active, disable the current one.
|
||||
trace!(
|
||||
libclide::trace!(
|
||||
target:Self::ID,
|
||||
"New tab {} focus set to Active; Setting current tab {} to Inactive",
|
||||
index,
|
||||
@@ -91,11 +90,11 @@ impl EditorTab {
|
||||
}
|
||||
match self.get_editor_key(index) {
|
||||
None => {
|
||||
error!(target:Self::ID, "Failed setting tab focus for invalid key {index}");
|
||||
libclide::error!(target:Self::ID, "Failed setting tab focus for invalid key {index}");
|
||||
}
|
||||
Some(key) => match self.editors.get_mut(&key) {
|
||||
None => {
|
||||
error!(
|
||||
libclide::error!(
|
||||
target:Self::ID,
|
||||
"Failed to update tab focus at index {} with invalid key: {}",
|
||||
self.current_editor,
|
||||
@@ -108,12 +107,12 @@ impl EditorTab {
|
||||
}
|
||||
|
||||
pub fn open_tab(&mut self, path: &std::path::Path) -> Result<()> {
|
||||
trace!(target:Self::ID, "Opening new EditorTab with path {:?}", path);
|
||||
libclide::trace!(target:Self::ID, "Opening new EditorTab with path {:?}", path);
|
||||
if self
|
||||
.editors
|
||||
.contains_key(&path.to_string_lossy().to_string())
|
||||
{
|
||||
warn!(target:Self::ID, "EditorTab already opened with this file");
|
||||
libclide::warn!(target:Self::ID, "EditorTab already opened with this file");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
@@ -138,12 +137,12 @@ impl EditorTab {
|
||||
.to_owned();
|
||||
match self.editors.remove(&key) {
|
||||
None => {
|
||||
error!(target:Self::ID, "Failed to remove editor tab {key} with invalid index {index}")
|
||||
libclide::error!(target:Self::ID, "Failed to remove editor tab {key} with invalid index {index}")
|
||||
}
|
||||
Some(_) => {
|
||||
self.prev_editor();
|
||||
self.tab_order.remove(index);
|
||||
info!(target:Self::ID, "Closed editor tab {key} at index {index}")
|
||||
libclide::info!(target:Self::ID, "Closed editor tab {key} at index {index}")
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
use crate::tui::component::{Action, Component, ComponentState, Focus, FocusState};
|
||||
use anyhow::{Context, Result, bail};
|
||||
use libclide::fs::entry_meta::EntryMeta;
|
||||
use log::trace;
|
||||
use libclide_macros::log_id;
|
||||
use ratatui::buffer::Buffer;
|
||||
use ratatui::crossterm::event::{Event, KeyCode, KeyEvent, MouseEvent, MouseEventKind};
|
||||
use ratatui::layout::{Alignment, Position, Rect};
|
||||
@@ -17,6 +17,7 @@ use std::path::{Path, PathBuf};
|
||||
use tui_tree_widget::{Tree, TreeItem, TreeState};
|
||||
|
||||
#[derive(Debug)]
|
||||
#[log_id]
|
||||
pub struct Explorer<'a> {
|
||||
root_path: EntryMeta,
|
||||
tree_items: TreeItem<'a, String>,
|
||||
@@ -25,10 +26,8 @@ pub struct Explorer<'a> {
|
||||
}
|
||||
|
||||
impl<'a> Explorer<'a> {
|
||||
pub const ID: &'static str = "Explorer";
|
||||
|
||||
pub fn new(path: &PathBuf) -> Result<Self> {
|
||||
trace!(target:Self::ID, "Building {}", Self::ID);
|
||||
libclide::trace!("Building {}", Self::ID);
|
||||
let explorer = Explorer {
|
||||
root_path: EntryMeta::new(path)?,
|
||||
tree_items: Self::build_tree_from_path(path)?,
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
// SPDX-License-Identifier: GNU General Public License v3.0 or later
|
||||
|
||||
use crate::tui::component::{Action, Component, ComponentState, Focus, FocusState};
|
||||
use log::{LevelFilter, trace};
|
||||
use libclide_macros::log_id;
|
||||
use log::LevelFilter;
|
||||
use ratatui::buffer::Buffer;
|
||||
use ratatui::crossterm::event::{Event, KeyCode, KeyEvent};
|
||||
use ratatui::layout::Rect;
|
||||
@@ -13,16 +14,15 @@ use tui_logger::{TuiLoggerLevelOutput, TuiLoggerSmartWidget, TuiWidgetEvent, Tui
|
||||
|
||||
/// 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().
|
||||
#[log_id]
|
||||
pub struct Logger {
|
||||
state: TuiWidgetState,
|
||||
pub(crate) component_state: ComponentState,
|
||||
}
|
||||
|
||||
impl Logger {
|
||||
pub const ID: &str = "Logger";
|
||||
|
||||
pub fn new() -> Self {
|
||||
trace!(target:Self::ID, "Building {}", Self::ID);
|
||||
libclide::trace!(target:Self::ID, "Building {}", Self::ID);
|
||||
let state = TuiWidgetState::new();
|
||||
state.transition(TuiWidgetEvent::HideKey);
|
||||
Self {
|
||||
|
||||
@@ -7,7 +7,7 @@ use crate::tui::menu_bar::MenuBarItemOption::{
|
||||
About, CloseTab, Exit, Reload, Save, ShowHideExplorer, ShowHideLogger,
|
||||
};
|
||||
use anyhow::Context;
|
||||
use log::trace;
|
||||
use libclide_macros::log_id;
|
||||
use ratatui::buffer::Buffer;
|
||||
use ratatui::crossterm::event::{KeyCode, KeyEvent};
|
||||
use ratatui::layout::Rect;
|
||||
@@ -80,6 +80,7 @@ impl MenuBarItem {
|
||||
}
|
||||
}
|
||||
|
||||
#[log_id]
|
||||
pub struct MenuBar {
|
||||
selected: MenuBarItem,
|
||||
opened: Option<MenuBarItem>,
|
||||
@@ -88,11 +89,9 @@ pub struct MenuBar {
|
||||
}
|
||||
|
||||
impl MenuBar {
|
||||
pub const ID: &str = "MenuBar";
|
||||
|
||||
const DEFAULT_HELP: &str = "(←/h)/(→/l): Select option | Enter: Choose selection";
|
||||
pub fn new() -> Self {
|
||||
trace!(target:Self::ID, "Building {}", Self::ID);
|
||||
libclide::trace!("Building {}", Self::ID);
|
||||
Self {
|
||||
selected: MenuBarItem::File,
|
||||
opened: None,
|
||||
@@ -157,7 +156,7 @@ impl MenuBar {
|
||||
height,
|
||||
}
|
||||
// TODO: X offset for item option? It's fine as-is, but it might look nicer.
|
||||
// trace!(target:Self::ID, "Building Rect under MenuBar popup {}", rect);
|
||||
// trace!("Building Rect under MenuBar popup {}", rect);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user