mirror of
https://github.com/itsjunetime/tdf.git
synced 2026-06-02 08:01:47 -04:00
it almost basically works
This commit is contained in:
+126
-3
@@ -1,7 +1,18 @@
|
||||
use std::io::Write;
|
||||
use std::{io::Write, num::NonZeroU32};
|
||||
|
||||
use crossterm::event::EventStream;
|
||||
use kittage::{AsyncInputReader, ImageId, action::Action, error::TransmitError};
|
||||
use crossterm::{cursor::MoveTo, event::EventStream, execute};
|
||||
use kittage::{
|
||||
AsyncInputReader, ImageDimensions, ImageId, PixelFormat,
|
||||
action::Action,
|
||||
delete::{ClearOrDelete, DeleteConfig, WhichToDelete},
|
||||
display::DisplayConfig,
|
||||
error::TransmitError,
|
||||
image::Image,
|
||||
medium::Medium
|
||||
};
|
||||
use ratatui::prelude::Rect;
|
||||
|
||||
use crate::converter::MaybeTransferred;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct DbgWriter<W: Write> {
|
||||
@@ -46,3 +57,115 @@ pub async fn run_action<'image, 'data, 'es>(
|
||||
.await
|
||||
.map(|(_, i)| i)
|
||||
}
|
||||
|
||||
pub async fn display_kitty_images(
|
||||
images: Vec<(usize, &mut MaybeTransferred, Rect)>,
|
||||
ev_stream: &mut EventStream
|
||||
) -> Result<(), (Vec<usize>, String)> {
|
||||
run_action(
|
||||
Action::Delete(DeleteConfig {
|
||||
effect: ClearOrDelete::Clear,
|
||||
which: WhichToDelete::All
|
||||
}),
|
||||
ev_stream
|
||||
)
|
||||
.await
|
||||
.map_err(|e| (vec![], format!("Couldn't clear previous images: {e}")))?;
|
||||
|
||||
let mut err = None;
|
||||
for (page_num, img, area) in images {
|
||||
let config = DisplayConfig::default();
|
||||
|
||||
execute!(std::io::stdout(), MoveTo(area.x, area.y)).unwrap();
|
||||
|
||||
log::debug!("looking at (area {area:?}) img {img:#?}");
|
||||
|
||||
let this_err = match img {
|
||||
MaybeTransferred::NotYet(image, _map) => {
|
||||
let mut fake_image = Image {
|
||||
num_or_id: image.num_or_id,
|
||||
format: PixelFormat::Rgb24(
|
||||
ImageDimensions {
|
||||
width: 0,
|
||||
height: 0
|
||||
},
|
||||
None
|
||||
),
|
||||
medium: Medium::Direct {
|
||||
chunk_size: None,
|
||||
data: (&[]).into()
|
||||
}
|
||||
};
|
||||
std::mem::swap(image, &mut fake_image);
|
||||
|
||||
log::debug!("Actually trying to display an image now: {fake_image:?}...");
|
||||
|
||||
let res = run_action(
|
||||
Action::TransmitAndDisplay {
|
||||
image: fake_image,
|
||||
config,
|
||||
placement_id: None
|
||||
},
|
||||
ev_stream
|
||||
)
|
||||
.await;
|
||||
|
||||
log::debug!("And it should've gone through: {res:?}!...");
|
||||
|
||||
match res {
|
||||
Ok(img_id) => {
|
||||
// We need the `_map` to be dropped here, but can't explicitly carry it
|
||||
// over to here. So we're just relying on the overwrite of `img` to
|
||||
// drop `_map` (and thus unmap the memory) for us
|
||||
*img = MaybeTransferred::Transferred(img_id);
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => Err(match e {
|
||||
TransmitError::Writing(action, e) => {
|
||||
let num = if let Action::TransmitAndDisplay {
|
||||
image: failed_img, ..
|
||||
} = *action
|
||||
{
|
||||
*image = failed_img;
|
||||
None
|
||||
} else {
|
||||
Some(page_num)
|
||||
};
|
||||
|
||||
(num, e.to_string())
|
||||
}
|
||||
_ => (Some(page_num), e.to_string())
|
||||
})
|
||||
}
|
||||
}
|
||||
MaybeTransferred::Transferred(image_id) => {
|
||||
let e = run_action(
|
||||
Action::Display {
|
||||
image_id: *image_id,
|
||||
placement_id: NonZeroU32::new(1).unwrap(),
|
||||
config
|
||||
},
|
||||
ev_stream
|
||||
)
|
||||
.await
|
||||
.map(|_| ())
|
||||
.map_err(|e| (None, e.to_string()));
|
||||
|
||||
log::debug!("Just tried to display: {e:?}");
|
||||
e
|
||||
}
|
||||
};
|
||||
|
||||
if let Err((id, e)) = this_err {
|
||||
let e = err.get_or_insert_with(|| (vec![], e));
|
||||
if let Some(id) = id {
|
||||
e.0.push(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
match err {
|
||||
Some(e) => Err(e),
|
||||
None => Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user