pub struct ImageView { /* private fields */ }
Expand description
an imageview can display an image in the terminal with a ratio of one pixel per char in width.
Implementations§
source§impl ImageView
impl ImageView
sourcepub fn new(path: &Path) -> Result<Self, ProgramError>
pub fn new(path: &Path) -> Result<Self, ProgramError>
Examples found in repository?
src/preview/preview.rs (line 45)
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
pub fn new(
path: &Path,
prefered_mode: Option<PreviewMode>,
con: &AppContext,
) -> Self {
match prefered_mode {
Some(PreviewMode::Hex) => Self::hex(path),
Some(PreviewMode::Image) => Self::image(path),
Some(PreviewMode::Text) => Self::unfiltered_text(path, con),
None => {
// automatic behavior: image, text, hex
ImageView::new(path)
.map(Self::Image)
.unwrap_or_else(|_| Self::unfiltered_text(path, con))
}
}
}
/// try to build a preview with the designed mode, return an error
/// if that wasn't possible
pub fn with_mode(
path: &Path,
mode: PreviewMode,
con: &AppContext,
) -> Result<Self, ProgramError> {
match mode {
PreviewMode::Hex => {
Ok(HexView::new(path.to_path_buf()).map(Self::Hex)?)
}
PreviewMode::Image => {
ImageView::new(path).map(Self::Image)
}
PreviewMode::Text => {
Ok(
SyntacticView::new(path, InputPattern::none(), &mut Dam::unlimited(), con, false)
.transpose()
.expect("syntactic view without pattern shouldn't be none")
.map(Self::Syntactic)?,
)
}
}
}
/// build an image view, unless the file can't be interpreted
/// as an image, in which case a hex view is used
pub fn image(path: &Path) -> Self {
ImageView::new(path)
.ok()
.map(Self::Image)
.unwrap_or_else(|| Self::hex(path))
}
pub fn is_png(&self) -> bool
sourcepub fn display(
&mut self,
w: &mut W,
disc: &DisplayContext<'_>,
area: &Area
) -> Result<(), ProgramError>
pub fn display(
&mut self,
w: &mut W,
disc: &DisplayContext<'_>,
area: &Area
) -> Result<(), ProgramError>
Examples found in repository?
src/preview/preview.rs (line 255)
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
pub fn display(
&mut self,
w: &mut W,
disc: &DisplayContext,
area: &Area,
) -> Result<(), ProgramError> {
let panel_skin = &disc.panel_skin;
let screen = disc.screen;
let con = &disc.con;
match self {
Self::Image(iv) => iv.display(w, disc, area),
Self::Syntactic(sv) => sv.display(w, screen, panel_skin, area, con),
Self::ZeroLen(zlv) => zlv.display(w, screen, panel_skin, area),
Self::Hex(hv) => hv.display(w, screen, panel_skin, area),
Self::IoError(err) => {
let mut y = area.top;
w.queue(cursor::MoveTo(area.left, y))?;
let mut cw = CropWriter::new(w, area.width as usize);
cw.queue_str(&panel_skin.styles.default, "An error prevents the preview:")?;
cw.fill(&panel_skin.styles.default, &SPACE_FILLING)?;
y += 1;
w.queue(cursor::MoveTo(area.left, y))?;
let mut cw = CropWriter::new(w, area.width as usize);
cw.queue_g_string(&panel_skin.styles.status_error, err.to_string())?;
cw.fill(&panel_skin.styles.default, &SPACE_FILLING)?;
y += 1;
while y < area.top + area.height {
w.queue(cursor::MoveTo(area.left, y))?;
let mut cw = CropWriter::new(w, area.width as usize);
cw.fill(&panel_skin.styles.default, &SPACE_FILLING)?;
y += 1;
}
Ok(())
}
}
}
sourcepub fn display_info(
&mut self,
w: &mut W,
_screen: Screen,
panel_skin: &PanelSkin,
area: &Area
) -> Result<(), ProgramError>
pub fn display_info(
&mut self,
w: &mut W,
_screen: Screen,
panel_skin: &PanelSkin,
area: &Area
) -> Result<(), ProgramError>
Examples found in repository?
src/preview/preview.rs (line 289)
281 282 283 284 285 286 287 288 289 290 291 292 293 294
pub fn display_info(
&mut self,
w: &mut W,
screen: Screen,
panel_skin: &PanelSkin,
area: &Area,
) -> Result<(), ProgramError> {
match self {
Self::Image(iv) => iv.display_info(w, screen, panel_skin, area),
Self::Syntactic(sv) => sv.display_info(w, screen, panel_skin, area),
Self::Hex(hv) => hv.display_info(w, screen, panel_skin, area),
_ => Ok(()),
}
}