use std::sync::Arc;
use tokio::sync::Mutex;
use ratatui::text::Line;
use ratatui_image::protocol::StatefulProtocol;
pub enum PreviewContent {
Text(Vec<Line<'static>>),
Image(Arc<Mutex<StatefulProtocol>>),
}
impl PreviewContent {
pub fn text(lines: Vec<Line<'static>>) -> Self {
Self::Text(lines)
}
pub fn image(protocol: Arc<Mutex<StatefulProtocol>>) -> Self {
Self::Image(protocol)
}
pub fn is_text(&self) -> bool {
matches!(self, Self::Text(_))
}
pub fn is_image(&self) -> bool {
matches!(self, Self::Image(_))
}
pub fn as_text(&self) -> Option<&Vec<Line<'static>>> {
match self {
Self::Text(lines) => Some(lines),
Self::Image(_) => None,
}
}
pub fn as_image(&self) -> Option<&Arc<Mutex<StatefulProtocol>>> {
match self {
Self::Text(_) => None,
Self::Image(protocol) => Some(protocol),
}
}
pub fn as_image_mut(&mut self) -> Option<&mut Arc<Mutex<StatefulProtocol>>> {
match self {
Self::Text(_) => None,
Self::Image(protocol) => Some(protocol),
}
}
}
impl PreviewContent {
pub fn len(&self) -> usize {
match self {
Self::Text(lines) => lines.len(),
Self::Image(_) => 1, }
}
pub fn is_empty(&self) -> bool {
match self {
Self::Text(lines) => lines.is_empty(),
Self::Image(_) => false, }
}
}
impl Clone for PreviewContent {
fn clone(&self) -> Self {
match self {
Self::Text(lines) => Self::Text(lines.clone()),
Self::Image(image) => Self::Image(image.clone()),
}
}
}
impl Default for PreviewContent {
fn default() -> Self {
Self::Text(Vec::new())
}
}
impl std::fmt::Debug for PreviewContent {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Text(lines) => f
.debug_tuple("Text")
.field(&format!("{} lines", lines.len()))
.finish(),
Self::Image(_) => f.debug_tuple("Image").field(&"StatefulProtocol").finish(),
}
}
}