Add macros for logging.
This commit is contained in:
@@ -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.")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user