|
| 1 | +use crate::app; |
| 2 | +use crate::config::get_config; |
| 3 | +use crate::render::{render_code_file, render_image_file, render_markdown}; |
| 4 | +use crate::utils::detect_language; |
| 5 | +use devicons::{icon_for_file, File, Theme}; |
| 6 | +use std::collections::HashMap; |
| 7 | +use std::io::{self, Write}; |
| 8 | +use std::path::Path; |
| 9 | +use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; |
| 10 | + |
| 11 | +pub struct ViewerManager { |
| 12 | + viewers: HashMap<String, Box<dyn Viewer>>, |
| 13 | +} |
| 14 | + |
| 15 | +impl ViewerManager { |
| 16 | + pub fn new() -> Self { |
| 17 | + let mut viewer_manager = ViewerManager { |
| 18 | + viewers: HashMap::new(), |
| 19 | + }; |
| 20 | + // Register default viewers |
| 21 | + viewer_manager.register_viewer("markdown", Box::new(MarkdownViewer)); |
| 22 | + viewer_manager.register_viewer("code", Box::new(CodeViewer)); |
| 23 | + viewer_manager.register_viewer("image", Box::new(ImageViewer)); |
| 24 | + viewer_manager |
| 25 | + } |
| 26 | + |
| 27 | + pub fn register_viewer(&mut self, name: &str, viewer: Box<dyn Viewer>) { |
| 28 | + self.viewers.insert(name.to_string(), viewer); |
| 29 | + } |
| 30 | + |
| 31 | + pub fn visualize( |
| 32 | + &self, |
| 33 | + viewer_names: &[String], |
| 34 | + content: &str, |
| 35 | + file_path: Option<&str>, |
| 36 | + ) -> io::Result<()> { |
| 37 | + let mut stdout = StandardStream::stdout(ColorChoice::Always); |
| 38 | + let config = get_config(); |
| 39 | + |
| 40 | + // Display file name if available and show_filename is true |
| 41 | + if config.show_filename { |
| 42 | + if let Some(path) = file_path { |
| 43 | + let file_name = Path::new(path) |
| 44 | + .file_name() |
| 45 | + .unwrap_or_default() |
| 46 | + .to_string_lossy(); |
| 47 | + |
| 48 | + let file = File::new(Path::new(path)); |
| 49 | + let icon = icon_for_file(&file, Some(Theme::Dark)); |
| 50 | + stdout.set_color(ColorSpec::new().set_fg(Some(Color::Blue)).set_bold(true))?; |
| 51 | + writeln!(stdout)?; |
| 52 | + stdout.set_color(ColorSpec::new().set_fg(Some(Color::Cyan)))?; |
| 53 | + writeln!(stdout, "{} {}", icon.icon, file_name)?; |
| 54 | + stdout.reset()?; |
| 55 | + writeln!(stdout)?; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + for (index, viewer_name) in viewer_names.iter().enumerate() { |
| 60 | + if index > 0 { |
| 61 | + writeln!(stdout)?; |
| 62 | + } |
| 63 | + if let Some(viewer) = self.viewers.get(viewer_name) { |
| 64 | + viewer.visualize(content, file_path)?; |
| 65 | + } else { |
| 66 | + eprintln!("Unknown viewer: {}", viewer_name); |
| 67 | + } |
| 68 | + } |
| 69 | + Ok(()) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +pub trait Viewer { |
| 74 | + fn visualize(&self, content: &str, file_path: Option<&str>) -> io::Result<()>; |
| 75 | +} |
| 76 | + |
| 77 | +struct MarkdownViewer; |
| 78 | + |
| 79 | +impl Viewer for MarkdownViewer { |
| 80 | + fn visualize(&self, content: &str, _file_path: Option<&str>) -> io::Result<()> { |
| 81 | + let json = app::parse_and_process_markdown(content)?; |
| 82 | + render_markdown(&json) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +struct CodeViewer; |
| 87 | + |
| 88 | +impl Viewer for CodeViewer { |
| 89 | + fn visualize(&self, content: &str, file_path: Option<&str>) -> io::Result<()> { |
| 90 | + let language = file_path |
| 91 | + .map(|path| detect_language(path)) |
| 92 | + .unwrap_or_else(|| "txt".to_string()); |
| 93 | + render_code_file(content, &language) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +struct ImageViewer; |
| 98 | + |
| 99 | +impl Viewer for ImageViewer { |
| 100 | + fn visualize(&self, _content: &str, file_path: Option<&str>) -> io::Result<()> { |
| 101 | + if let Some(path) = file_path { |
| 102 | + render_image_file(path) |
| 103 | + } else { |
| 104 | + Err(io::Error::new( |
| 105 | + io::ErrorKind::InvalidInput, |
| 106 | + "No file path provided for image rendering", |
| 107 | + )) |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +pub fn determine_viewer(file_path: &Path) -> Vec<String> { |
| 113 | + let extension = file_path |
| 114 | + .extension() |
| 115 | + .and_then(std::ffi::OsStr::to_str) |
| 116 | + .unwrap_or("") |
| 117 | + .to_lowercase(); |
| 118 | + match extension.as_str() { |
| 119 | + "md" => vec!["markdown".to_string()], |
| 120 | + "jpg" | "jpeg" | "png" | "gif" | "bmp" | "webp" => vec!["image".to_string()], |
| 121 | + _ => vec!["code".to_string()], |
| 122 | + } |
| 123 | +} |
0 commit comments