Files
zoeae/src/file.rs
T

81 lines
1.9 KiB
Rust
Raw Normal View History

2026-01-24 19:53:46 -06:00
use std::path::PathBuf;
2026-01-24 19:53:46 -06:00
use iced::widget::{markdown, text_editor};
2026-01-24 19:53:46 -06:00
pub struct File {
content: text_editor::Content,
path: Option<PathBuf>,
markdown: Vec<markdown::Item>,
}
impl Default for File {
fn default() -> Self {
File {
content: text_editor::Content::new(),
path: None,
markdown: Vec::new(),
}
}
}
2026-01-24 19:53:46 -06:00
impl File {
pub fn from(content: &str, path: Option<PathBuf>) -> Self {
let text_editor_content = text_editor::Content::with_text(content);
let markdown = markdown::parse(content).collect();
2026-01-24 19:53:46 -06:00
File {
content: text_editor_content,
path,
markdown,
}
}
2026-01-24 19:53:46 -06:00
pub fn content(&self) -> &text_editor::Content {
&self.content
}
2026-01-24 19:53:46 -06:00
pub fn content_mut(&mut self) -> &mut text_editor::Content {
&mut self.content
}
2026-01-24 19:53:46 -06:00
pub fn set_content(&mut self, content: &str) {
self.content = text_editor::Content::with_text(content);
}
2026-01-24 19:53:46 -06:00
pub fn markdown(&self) -> Vec<&markdown::Item> {
self.markdown.iter().collect()
}
2026-01-24 19:53:46 -06:00
pub fn update_markdown(&mut self) {
self.markdown = markdown::parse(&self.content.text()).collect();
}
2026-01-24 19:53:46 -06:00
pub fn path(&self) -> Option<&PathBuf> {
self.path.as_ref()
}
2026-01-24 19:53:46 -06:00
pub fn set_path(&mut self, path: Option<PathBuf>) {
self.path = path;
}
2026-01-24 19:53:46 -06:00
pub fn display_name(&self) -> &str {
self.path
.as_deref()
.and_then(|p| p.file_name())
.and_then(|n| n.to_str())
.unwrap_or("New file")
}
2026-01-24 19:53:46 -06:00
pub fn position_summary(&self) -> String {
let pos = self.content.cursor().position;
format!("Ln {}, Col {}", pos.line, pos.column)
}
2026-01-24 19:53:46 -06:00
pub fn path_summary(&self) -> String {
self.path
.as_deref()
.map(|p| p.to_string_lossy().to_string())
.unwrap_or_default()
}
}