Pass application context to GUI. (#11)

This commit was merged in pull request #11.
This commit is contained in:
2026-01-31 04:25:14 +00:00
parent 00f9075d0f
commit 2340fd7652
21 changed files with 937 additions and 311 deletions

View File

@@ -9,6 +9,7 @@ use ratatui::crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
use ratatui::layout::{Alignment, Rect};
use ratatui::prelude::{Color, Style};
use ratatui::widgets::{Block, Borders, Padding, Widget};
use std::path::PathBuf;
use syntect::parsing::SyntaxSet;
pub struct Editor {
@@ -20,12 +21,10 @@ pub struct Editor {
}
impl Editor {
pub fn id() -> &'static str {
"Editor"
}
pub const ID: &str = "Editor";
pub fn new(path: &std::path::PathBuf) -> Self {
trace!(target:Self::id(), "Building {}", Self::id());
pub fn new(path: &std::path::Path) -> Self {
trace!(target:Self::ID, "Building {}", Self::ID);
Editor {
state: EditorState::default(),
event_handler: EditorEventHandler::default(),
@@ -39,24 +38,24 @@ impl Editor {
}
pub fn reload_contents(&mut self) -> Result<()> {
trace!(target:Self::id(), "Reloading editor file contents {:?}", self.file_path);
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");
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),
}
}
pub fn set_contents(&mut self, path: &std::path::PathBuf) -> Result<()> {
trace!(target:Self::id(), "Setting Editor contents from path {:?}", path);
pub fn set_contents(&mut self, path: &std::path::Path) -> Result<()> {
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()
.map(|line| line.chars().collect::<Vec<char>>())
.collect();
self.file_path = Some(path.clone());
self.file_path = Some(PathBuf::from(path));
self.state.lines = Lines::new(lines);
self.state.cursor.row = 0;
self.state.cursor.col = 0;
@@ -66,10 +65,10 @@ impl Editor {
pub fn save(&self) -> Result<()> {
if let Some(path) = &self.file_path {
trace!(target:Self::id(), "Saving Editor contents {:?}", path);
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");
error!(target:Self::ID, "Failed saving Editor contents; file_path was None");
bail!("File not saved. No file path set.")
}
}
@@ -86,7 +85,7 @@ impl Widget for &mut Editor {
.syntax_set
.find_syntax_by_extension(lang)
.map(|s| s.name.to_string())
.unwrap_or("Unknown".to_string());
.unwrap_or_else(|| String::from("Unknown"));
EditorView::new(&mut self.state)
.wrap(true)