Compare commits

..

9 Commits

Author SHA1 Message Date
itsjunetime 3b130af6df Prevent zoomout in both dimensions using double-pass system 2026-05-17 13:05:49 -05:00
Adam Martinez 95c24206ca Fix zoom-out bounds when reaching fit-screen layout (#144) 2026-05-17 10:38:46 -05:00
June 5e4fbff9cd Prevent page clear/show flash on kitty when pages have been cleared from memory. (#148)
* wip; start working on making it not flash when transferring new images

* don't delete the images, just clear them

* add changelog entry

* fmt
2026-04-24 09:56:19 -05:00
June 421401c7a7 update deps and work with kittage 0.4 (#147)
* update deps and work with kittage 0.4

* fix: fmt

* also update msrv now that we have if-let arms
2026-04-19 12:01:13 -05:00
Adam Martinez 7448fc73c5 Minor refactor with if-let guards (#145)
`if let` guards were stabilized in 1.95. The code concerning the TODO has been refactored.
2026-04-19 11:19:58 -05:00
Adam Martinez 1ed03e94fb Patch pathfinder_simd while upstream updates (#146)
While #583 in servo/pathfiner gets merged, and font-kit updates its patfinder_simd (and mupdf updates font-kit,) this should serve as a build fix.
2026-04-13 10:21:53 -05:00
June cf9d295d95 inline pnm decoder instead of using generalized load fn (#143) 2026-03-16 17:29:02 -05:00
June 725c6c6bfd Simpler tokio runtime (#140)
* Simpler tokio runtime; 3 threads and no io driver

* switch benches over to other runtime
2026-03-05 15:54:55 -06:00
June fca61d55b5 Remove tip workflow (#141) 2026-03-05 15:54:36 -06:00
9 changed files with 527 additions and 484 deletions
-37
View File
@@ -11,7 +11,6 @@ on:
env:
CARGO_TERM_COLOR: always
tip_release_path: RELEASE.md
jobs:
test-build:
@@ -107,39 +106,3 @@ jobs:
generate_release_notes: true
files: release-artifacts/*
prerelease: ${{ contains(github.ref, 'alpha') || contains(github.ref, 'beta') || contains(github.ref, 'rc') }}
tip-release:
name: Release tip on push to main
runs-on: ubuntu-slim
if: github.event_name == 'push' && github.ref_name == 'main'
needs: test-build
steps:
- uses: actions/checkout@v6
- name: Download prebuilt artifacts
uses: actions/download-artifact@v7
with:
path: release-artifacts
merge-multiple: true
- name: Generate release notes
run: |
tree release-artifacts/
echo "From commit: $(git rev-parse --short HEAD)" > ${{ env.tip_release_path }}
echo "Generated on: $(date -u +"%Y-%m-%d %H:%M") UTC" >> ${{ env.tip_release_path }}
cat ${{ env.tip_release_path }}
- name: Update the tip tag
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git tag --force tip && git push --force origin tag tip
- name: Update the draft tip release
uses: softprops/action-gh-release@v2
with:
prerelease: true
files: release-artifacts/*
tag_name: tip
name: Tip Build
body_path: ${{ env.tip_release_path }}
+1
View File
@@ -2,6 +2,7 @@
- Added windows support! (thank you to [@jarjk](https://github.com/jarjk) for helping out!)
- Added keybindings (`0`/`$`) to scroll to left or right side of zoomed-in image ([#131](https://github.com/itsjunetime/tdf/pull/131), thank you [@IshDeshpa](https://github.com/IshDeshpa)!)
- Fixed issue with images clearing/flashing after displaying a certain number on kitty
- (Internal) decreased runtime footprint of tokio runtime
# v0.5.0
Generated
+203 -210
View File
File diff suppressed because it is too large Load Diff
+6 -2
View File
@@ -11,7 +11,7 @@ license = "AGPL-3.0-only"
keywords = ["pdf", "tui", "cli", "terminal"]
categories = ["command-line-utilities", "text-processing", "visualization"]
default-run = "tdf"
rust-version = "1.86"
rust-version = "1.95"
[[bin]]
name = "tdf"
@@ -41,10 +41,11 @@ nix = { version = "0.31.0", features = ["signal"] }
mupdf = { git = "https://github.com/messense/mupdf-rs.git", rev = "d7441b9998c92135e329559c0aa71d9dc92cf4de", default-features = false, features = ["svg", "system-fonts", "img"] }
rayon = { version = "1", default-features = false }
# kittage = { path = "../kittage/", features = ["crossterm-tokio", "image-crate", "log"] }
kittage = { version = "0.3.4", features = ["crossterm-tokio", "image-crate", "log"] }
kittage = { version = "0.4.0", features = ["crossterm-tokio", "image-crate", "log"] }
memmap2 = "0"
csscolorparser = { version = "0.8.0", default-features = false }
debounce = "0.2.2"
smallvec = "1"
# logging
log = "0.4.27"
@@ -53,6 +54,9 @@ flexi_logger = "0.31"
# for tracing with tokio-console
console-subscriber = { version = "0.5.0", optional = true }
[patch.crates-io]
pathfinder_simd = { git = "https://github.com/itsjunetime/pathfinder.git", rev = "814671e162a1829e074521446317b915311d3d4d" }
[profile.production]
inherits = "release"
lto = "fat"
+26 -19
View File
@@ -1,12 +1,13 @@
use std::{
num::NonZeroUsize,
io::Cursor,
num::{NonZeroU32, NonZeroUsize},
time::{SystemTime, UNIX_EPOCH}
};
use flume::{Receiver, SendError, Sender, TryRecvError};
use futures_util::stream::StreamExt as _;
use image::DynamicImage;
use kittage::{NumberOrId, action::NONZERO_ONE};
use image::{DynamicImage, codecs::pnm::PnmDecoder};
use kittage::NumberOrId;
use ratatui::layout::Rect;
use ratatui_image::{
Resize,
@@ -111,22 +112,26 @@ pub async fn run_conversion_loop(
return Ok(None);
};
let mut dyn_img = image::load_from_memory_with_format(
&page_info.img_data.pixels,
image::ImageFormat::Pnm
)
.map_err(|e| RenderError::Converting(format!("Can't load image: {e}")))?;
let decoder = PnmDecoder::new(Cursor::new(&page_info.img_data.pixels)).map_err(|e| {
RenderError::Converting(format!(
"The image data provided from mupdf was not in pnm format ({e}); don't know how to convert"
))
})?;
match dyn_img {
DynamicImage::ImageRgb8(ref mut img) =>
for quad in &*page_info.result_rects {
img.par_enumerate_pixels_mut()
.filter(|(x, y, _)| {
*x > quad.ul_x && *x < quad.lr_x && *y > quad.ul_y && *y < quad.lr_y
})
.for_each(|(_, _, px)| px.0[2] = px.0[2].saturating_sub(u8::MAX / 2));
},
_ => unreachable!()
// The image we get should always already be `ImageRgb8`, so this `into` shouldn't do any
// conversions or anything, but just in case some underlying detail of mupdf or image
// changes, we do the `into` instead of just `match + unreachable!()` to avoid panicking
let mut dyn_img = DynamicImage::from_decoder(decoder)
.map_err(|e| RenderError::Converting(format!("Can't load image: {e}")))?
.into_rgb8();
for quad in &*page_info.result_rects {
dyn_img
.par_enumerate_pixels_mut()
.filter(|(x, y, _)| {
*x > quad.ul_x && *x < quad.lr_x && *y > quad.ul_y && *y < quad.lr_y
})
.for_each(|(_, _, px)| px.0[2] = px.0[2].saturating_sub(u8::MAX / 2));
}
let img_area = Rect {
@@ -136,6 +141,8 @@ pub async fn run_conversion_loop(
y: 0
};
let dyn_img = DynamicImage::ImageRgb8(dyn_img);
let txt_img = match picker.protocol_type() {
ProtocolType::Kitty => {
let rn = SystemTime::now()
@@ -157,7 +164,7 @@ pub async fn run_conversion_loop(
};
// if ur pdf has 4 billion pages then you deserve to suffer
img.num_or_id = NumberOrId::Id(NONZERO_ONE.saturating_add(page_num as u32));
img.num_or_id = NumberOrId::Id(NonZeroU32::MIN.saturating_add(page_num as u32));
ConvertedImage::Kitty {
img: MaybeTransferred::NotYet(img),
+87 -39
View File
@@ -1,3 +1,4 @@
use core::fmt::Display;
use std::{io::Write, num::NonZeroU32};
use crossterm::{
@@ -17,6 +18,7 @@ use kittage::{
medium::Medium
};
use ratatui::layout::Position;
use smallvec::SmallVec;
use crate::converter::MaybeTransferred;
@@ -63,7 +65,7 @@ impl<W: Write> Write for DbgWriter<W> {
pub async fn run_action<'es>(
action: Action<'_, '_>,
ev_stream: &'es mut EventStream
) -> Result<ImageId, TransmitError<<&'es mut EventStream as AsyncInputReader>::Error>> {
) -> Result<Option<ImageId>, TransmitError<<&'es mut EventStream as AsyncInputReader>::Error>> {
let writer = DbgWriter {
w: std::io::stdout().lock(),
#[cfg(debug_assertions)]
@@ -99,21 +101,51 @@ pub async fn do_shms_work(ev_stream: &mut EventStream) -> bool {
res.is_ok()
}
type ESTransErr<'es> = TransmitError<<&'es mut EventStream as AsyncInputReader>::Error>;
pub struct DisplayErr<'es> {
pub failed_pages: SmallVec<[usize; 2]>,
pub user_facing_err: &'static str,
pub source: DisplayErrSource<'es>
}
impl<'es> DisplayErr<'es> {
fn empty(user_facing_err: &'static str, source: ESTransErr<'es>) -> Self {
Self {
failed_pages: SmallVec::new(),
user_facing_err,
source: DisplayErrSource::Transmission(source)
}
}
}
#[derive(Debug)]
pub enum DisplayErrSource<'es> {
KittageReturnedNoId,
Transmission(ESTransErr<'es>)
}
impl Display for DisplayErrSource<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::KittageReturnedNoId => write!(
f,
"Kittage returned no ID when we asked it to display an image. This is a bug in kittage, please report it."
),
Self::Transmission(t) => write!(f, "Error with talking to the terminal: {t}")
}
}
}
pub async fn display_kitty_images<'es>(
display: KittyDisplay<'_>,
ev_stream: &'es mut EventStream
) -> Result<
(),
(
Vec<usize>,
&'static str,
TransmitError<<&'es mut EventStream as AsyncInputReader>::Error>
)
> {
ev_stream: &'es mut EventStream,
last_z_index: &mut i32
) -> Result<(), DisplayErr<'es>> {
let images = match display {
KittyDisplay::NoChange => return Ok(()),
KittyDisplay::DisplayImages(_) | KittyDisplay::ClearImages => {
run_action(
KittyDisplay::ClearImages =>
return run_action(
Action::Delete(DeleteConfig {
effect: ClearOrDelete::Clear,
which: WhichToDelete::All
@@ -121,24 +153,23 @@ pub async fn display_kitty_images<'es>(
ev_stream
)
.await
.map_err(|e| (vec![], "Couldn't clear previous images", e))?;
let KittyDisplay::DisplayImages(images) = display else {
return Ok(());
};
images
}
.map_err(|e| DisplayErr::empty("Couldn't clear previous images", e))
.map(|_: Option<ImageId>| ()),
KittyDisplay::DisplayImages(imgs) => imgs
};
let mut err = None;
let new_z_index = last_z_index.wrapping_add_unsigned(1);
let mut err = Ok::<(), (SmallVec<[usize; 2]>, DisplayErrSource<'es>)>(());
for KittyReadyToDisplay {
img,
page_num,
pos,
display_loc
mut display_loc
} in images
{
display_loc.z_index = new_z_index;
let config = DisplayConfig {
location: display_loc,
cursor_movement: CursorMovementPolicy::DontMove,
@@ -168,7 +199,7 @@ pub async fn display_kitty_images<'es>(
};
std::mem::swap(image, &mut fake_image);
let res = run_action(
run_action(
Action::TransmitAndDisplay {
image: fake_image,
config,
@@ -176,15 +207,13 @@ pub async fn display_kitty_images<'es>(
},
ev_stream
)
.await;
match res {
Ok(img_id) => {
*img = MaybeTransferred::Transferred(img_id);
Ok(())
}
Err(e) => Err((page_num, e))
}
.await
.map_err(DisplayErrSource::Transmission)
.and_then(|img_id| {
img_id
.map(|id| *img = MaybeTransferred::Transferred(id))
.ok_or(DisplayErrSource::KittageReturnedNoId)
})
}
MaybeTransferred::Transferred(image_id) => run_action(
Action::Display {
@@ -195,20 +224,39 @@ pub async fn display_kitty_images<'es>(
ev_stream
)
.await
.map(|_| ())
.map_err(|e| (page_num, e))
// don't need the return id 'cause we already know it
.map(|_: Option<ImageId>| ())
.map_err(DisplayErrSource::Transmission)
};
log::debug!("this_err is {this_err:#?}");
if let Err((id, e)) = this_err {
let e = err.get_or_insert_with(|| (vec![], e));
e.0.push(id);
if let Err(e) = this_err {
match err.as_mut() {
Ok(()) => err = Err((SmallVec::from([page_num].as_slice()), e)),
Err((v, _)) => v.push(page_num)
}
}
}
let z_idxes_to_remove = *last_z_index;
*last_z_index = new_z_index;
match err {
Some((replace, e)) => Err((replace, "Couldn't transfer image to the terminal", e)),
None => Ok(())
Err((failed_pages, source)) => Err(DisplayErr {
failed_pages,
user_facing_err: "Couldn't transfer image to the terminal",
source
}),
Ok(()) => run_action(
Action::Delete(DeleteConfig {
effect: ClearOrDelete::Clear,
which: WhichToDelete::PlacementsWithZIndex(z_idxes_to_remove)
}),
ev_stream
)
.await
.map_err(|e| DisplayErr::empty("Couldn't clear previously-sent images", e))
.map(|_| ())
}
}
+19 -7
View File
@@ -38,7 +38,9 @@ use ratatui_image::{
use tdf::{
PrerenderLimit,
converter::{ConvertedPage, ConverterMsg, run_conversion_loop},
kitty::{KittyDisplay, display_kitty_images, do_shms_work, run_action},
kitty::{
DisplayErr, DisplayErrSource, KittyDisplay, display_kitty_images, do_shms_work, run_action
},
renderer::{self, MUPDF_BLACK, MUPDF_WHITE, RenderError, RenderInfo, RenderNotif},
tui::{BottomMessage, InputAction, MessageSetting, Tui}
};
@@ -388,6 +390,8 @@ async fn enter_redraw_loop(
mut main_area: tdf::tui::RenderLayout,
font_size: FontSize
) -> Result<(), Box<dyn Error>> {
let mut kitty_z_idx = i32::MIN;
loop {
let mut needs_redraw = true;
let next_ev = ev_stream.next().fuse();
@@ -460,10 +464,16 @@ async fn enter_redraw_loop(
to_display = tui.render(f, &main_area, font_size);
})?;
let maybe_err = display_kitty_images(to_display, &mut ev_stream).await;
let maybe_err =
display_kitty_images(to_display, &mut ev_stream, &mut kitty_z_idx).await;
if let Err((to_replace, err_desc, enum_err)) = maybe_err {
match enum_err {
if let Err(DisplayErr {
failed_pages,
user_facing_err,
source
}) = maybe_err
{
match source {
// This is the error that kitty & ghostty provide us when they delete an
// image due to memory constraints, so if we get it, we just fix it by
// re-rendering so it don't display it to the user
@@ -471,13 +481,15 @@ async fn enter_redraw_loop(
// [TODO] maybe when we detect that an image was deleted, we probe the
// terminal for the pages around it to see if they were deleted too and if
// they were, we re-render them? idk
TransmitError::Terminal(TerminalError::NoEntity(_)) => (),
DisplayErrSource::Transmission(TransmitError::Terminal(
TerminalError::NoEntity(_)
)) => (),
_ => tui.set_msg(MessageSetting::Some(BottomMessage::Error(format!(
"{err_desc}: {enum_err}"
"{user_facing_err}: {source}"
))))
}
for page_num in to_replace {
for page_num in failed_pages {
tui.page_failed_display(page_num);
// So that they get re-rendered and sent over again
to_renderer.send(RenderNotif::PageNeedsReRender(page_num))?;
+184 -169
View File
@@ -72,7 +72,7 @@ struct PageConstraints {
r_to_l: bool
}
#[derive(Default, Debug)]
#[derive(Default, Debug, Clone, Copy)]
struct Zoom {
// just how much 'zoom' you have. 0 means it fills the screen (instead of fits), such
// that one axis is fully on-screen
@@ -86,7 +86,7 @@ struct Zoom {
}
impl Zoom {
/// Returns the zoom factor, where 1 is the default and means fill-screen
fn factor(&self) -> f32 {
fn factor(self) -> f32 {
// TODO: Make these configurable once we have a good way to set options after startup
const ZOOM_RATE: f32 = 1.1;
const ZOOM_RATE_GRANULAR: f32 = 1.05;
@@ -262,7 +262,7 @@ impl Tui {
let img_page_w_ratio = img_section_w / initial_page_w;
let img_page_h_ratio = img_section_h / initial_page_h;
let shrink_move_page = |dim: &mut u16, pos: &mut u16, axis_zoom_factor: f32| {
fn shrink_move_page(dim: &mut u16, pos: &mut u16, axis_zoom_factor: f32) {
let old_dim = *dim;
// The axis zoom factor tells us what portion of the axis
// we need to show.
@@ -272,26 +272,56 @@ impl Tui {
.checked_sub(*dim)
.expect("zooming out should shrink the image")
/ 2;
};
// TODO: Detect max zoom-out in zoom levels
if img_page_w_ratio < img_page_h_ratio {
// vertical scroll / tall image. zooming out means decreasing the width of the page area
shrink_move_page(
&mut img_area.width,
&mut img_area.x,
// disallow zooming out past fit-screen
zoom_factor.max(1.0 / img_page_h_ratio)
);
} else {
// horizontal scroll / wide image. zooming out means decreasing the width of the page area
shrink_move_page(
&mut img_area.height,
&mut img_area.y,
// disallow zooming out past fit-screen
zoom_factor.max(1.0 / img_page_w_ratio)
);
}
fn shrink_move_two_pass(
zoom: &mut Zoom,
dim: &mut u16,
pos: &mut u16,
zoom_factor: f32,
axis_zoom_factor: f32
) {
// To detect if we're already at fit-screen, we perform a first
// pass on `img_area` that emulates the last call to
// `render_zoomed` by subtracting from the `zoom`'s level. This
// holds because the `img_area` that gets passed is always the
// same.
let mut first_pass_dim = *dim;
let mut first_pass_pos = *pos;
let mut first_pass_zoom = *zoom;
if first_pass_zoom.level.is_negative() {
first_pass_zoom.step_in();
}
let first_pass_zoom_factor = first_pass_zoom.factor();
shrink_move_page(
&mut first_pass_dim,
&mut first_pass_pos,
first_pass_zoom_factor.max(1.0 / axis_zoom_factor)
);
log::debug!("first_pass_dim: {first_pass_dim}, first_pass_pos: {first_pass_pos}");
// vertical scroll / tall image. zooming out means decreasing
// the width of the page area
// use `max` to disallow zooming out past fit-screen
shrink_move_page(dim, pos, zoom_factor.max(1.0 / axis_zoom_factor));
log::debug!("new dim: {dim}, new pos: {pos}");
// The moment the image area is left unmodified, we've hit
// fit-screen and the zoom level ought be normalized.
if first_pass_dim == *dim && first_pass_pos == *pos {
zoom.step_in();
}
}
let (dim, pos, axis_zoom_factor) = if img_page_w_ratio < img_page_h_ratio {
(&mut img_area.width, &mut img_area.x, img_page_h_ratio)
} else {
// horizontal scroll / wide image. zooming out means decreasing
// the height of the page area
(&mut img_area.height, &mut img_area.y, img_page_w_ratio)
};
shrink_move_two_pass(zoom, dim, pos, zoom_factor, axis_zoom_factor);
}
log::debug!("after adjustment, page area is {img_area:#?}");
@@ -701,156 +731,141 @@ impl Tui {
match ev {
Event::Key(key) => {
match key.code {
KeyCode::Char(c) => {
// TODO: refactor back to `if let` arm guards when those are stabilized
KeyCode::Char(c)
if let BottomMessage::Input(InputCommand::Search(ref mut term)) =
self.bottom_msg
{
term.push(c);
return Some(InputAction::Redraw);
}
if let BottomMessage::Input(InputCommand::GoToPage(ref mut page)) =
self.bottom_msg
{
if c == 'g' && self.is_kitty {
self.update_zoom(Zoom::pan_bottom);
self.set_msg(MessageSetting::Pop);
return Some(InputAction::Redraw);
}
return c.to_digit(10).map(|input_num| {
*page = (*page * 10) + input_num as usize;
InputAction::Redraw
});
}
match c {
'l' => self.change_page(PageChange::Next, ChangeAmount::Single),
'j' => self.change_page(PageChange::Next, ChangeAmount::WholeScreen),
'h' => self.change_page(PageChange::Prev, ChangeAmount::Single),
'k' => self.change_page(PageChange::Prev, ChangeAmount::WholeScreen),
'q' => Some(InputAction::QuitApp),
'g' => {
self.set_msg(MessageSetting::Some(BottomMessage::Input(
InputCommand::GoToPage(0)
)));
Some(InputAction::Redraw)
}
'/' => {
self.set_msg(MessageSetting::Some(BottomMessage::Input(
InputCommand::Search(String::new())
)));
Some(InputAction::Redraw)
}
'i' => Some(InputAction::Invert),
'?' => {
self.showing_help_msg = true;
Some(InputAction::Redraw)
}
'f' => Some(InputAction::Fullscreen),
'n' if self.page < self.rendered.len() - 1 => {
// TODO: If we can't find one, then maybe like block until we've verified
// all the pages have been checked?
self.rendered[(self.page + 1)..]
.iter()
.enumerate()
.find_map(|(idx, p)| {
p.num_results
.is_some_and(|num| num > 0)
.then_some(self.page + 1 + idx)
})
.map(|next_page| {
jump_to_page(
&mut self.page,
&mut self.last_render.rect,
next_page
)
})
}
'N' if self.page > 0 => self.rendered[..(self.page)]
.iter()
.rev()
.enumerate()
.find_map(|(idx, p)| {
p.num_results
.is_some_and(|num| num > 0)
.then_some(self.page - (idx + 1))
})
.map(|prev_page| {
jump_to_page(
&mut self.page,
&mut self.last_render.rect,
prev_page
)
}),
'z' if key.modifiers.contains(KeyModifiers::CONTROL) => {
// [todo] better error handling here?
let mut backend = stdout();
execute!(
&mut backend,
LeaveAlternateScreen,
crossterm::cursor::Show,
crossterm::event::DisableMouseCapture
)
.unwrap();
disable_raw_mode().unwrap();
#[cfg(unix)]
{
// This process will hang after the SIGSTOP call until we get
// foregrounded again by something else, at which point we need to
// re-setup everything so that it all gets drawn again.
nix::sys::signal::kill(
nix::unistd::Pid::this(),
nix::sys::signal::Signal::SIGSTOP
)
.unwrap();
}
enable_raw_mode().unwrap();
execute!(
&mut backend,
EnterAlternateScreen,
crossterm::cursor::Hide,
crossterm::event::EnableMouseCapture
)
.unwrap();
self.last_render.rect = Rect::default();
Some(InputAction::Redraw)
}
'z' if self.is_kitty => {
let (zoom, f_or_f) = match self.zoom {
None => (Some(Zoom::default()), FitOrFill::Fill),
Some(_) => (None, FitOrFill::Fit)
};
self.zoom = zoom;
self.last_render.rect = Rect::default();
Some(InputAction::SwitchRenderZoom(f_or_f))
}
'o' if can_zoom => self.update_zoom(Zoom::step_in),
'O' if can_zoom => self.update_zoom(Zoom::step_out),
'L' if can_zoom => self.update_zoom(|z| z.pan(Direction::Right)),
'H' if can_zoom => self.update_zoom(|z| z.pan(Direction::Left)),
'J' if can_zoom => self.update_zoom(|z| z.pan(Direction::Down)),
'K' if can_zoom => self.update_zoom(|z| z.pan(Direction::Up)),
'G' if can_zoom => self.update_zoom(Zoom::pan_top),
'0' if can_zoom => self.update_zoom(Zoom::pan_left),
'$' if can_zoom => self.update_zoom(Zoom::pan_right),
'r' => Some(InputAction::Rotate),
_ => None
}
self.bottom_msg =>
{
term.push(c);
InputAction::Redraw.into()
}
KeyCode::Backspace => {
if let BottomMessage::Input(InputCommand::Search(ref mut term)) =
self.bottom_msg
{
term.pop();
return Some(InputAction::Redraw);
KeyCode::Char(c)
if let BottomMessage::Input(InputCommand::GoToPage(ref mut page)) =
self.bottom_msg && matches!(c, 'g' if self.is_kitty) =>
c.to_digit(10).map(|input_num| {
*page = (*page * 10) + input_num as usize;
InputAction::Redraw
}),
KeyCode::Char(_)
if let BottomMessage::Input(InputCommand::GoToPage(_)) =
self.bottom_msg =>
{
self.set_msg(MessageSetting::Pop);
self.update_zoom(Zoom::pan_bottom)
}
KeyCode::Char(c) => match c {
'l' => self.change_page(PageChange::Next, ChangeAmount::Single),
'j' => self.change_page(PageChange::Next, ChangeAmount::WholeScreen),
'h' => self.change_page(PageChange::Prev, ChangeAmount::Single),
'k' => self.change_page(PageChange::Prev, ChangeAmount::WholeScreen),
'q' => Some(InputAction::QuitApp),
'g' => {
self.set_msg(MessageSetting::Some(BottomMessage::Input(
InputCommand::GoToPage(0)
)));
Some(InputAction::Redraw)
}
None
'/' => {
self.set_msg(MessageSetting::Some(BottomMessage::Input(
InputCommand::Search(String::new())
)));
Some(InputAction::Redraw)
}
'i' => Some(InputAction::Invert),
'?' => {
self.showing_help_msg = true;
Some(InputAction::Redraw)
}
'f' => Some(InputAction::Fullscreen),
// TODO: If we can't find one, then maybe like block until we've verified
// all the pages have been checked?
'n' if self.page < self.rendered.len() - 1 => self.rendered
[(self.page + 1)..]
.iter()
.enumerate()
.find_map(|(idx, p)| {
p.num_results
.is_some_and(|num| num > 0)
.then_some(self.page + 1 + idx)
})
.map(|next_page| {
jump_to_page(&mut self.page, &mut self.last_render.rect, next_page)
}),
'N' if self.page > 0 => self.rendered[..(self.page)]
.iter()
.rev()
.enumerate()
.find_map(|(idx, p)| {
p.num_results
.is_some_and(|num| num > 0)
.then_some(self.page - (idx + 1))
})
.map(|prev_page| {
jump_to_page(&mut self.page, &mut self.last_render.rect, prev_page)
}),
'z' if key.modifiers.contains(KeyModifiers::CONTROL) => {
// [todo] better error handling here?
let mut backend = stdout();
execute!(
&mut backend,
LeaveAlternateScreen,
crossterm::cursor::Show,
crossterm::event::DisableMouseCapture
)
.unwrap();
disable_raw_mode().unwrap();
#[cfg(unix)]
{
// This process will hang after the SIGSTOP call until we get
// foregrounded again by something else, at which point we need to
// re-setup everything so that it all gets drawn again.
nix::sys::signal::kill(
nix::unistd::Pid::this(),
nix::sys::signal::Signal::SIGSTOP
)
.unwrap();
}
enable_raw_mode().unwrap();
execute!(
&mut backend,
EnterAlternateScreen,
crossterm::cursor::Hide,
crossterm::event::EnableMouseCapture
)
.unwrap();
self.last_render.rect = Rect::default();
Some(InputAction::Redraw)
}
'z' if self.is_kitty => {
let (zoom, f_or_f) = match self.zoom {
None => (Some(Zoom::default()), FitOrFill::Fill),
Some(_) => (None, FitOrFill::Fit)
};
self.zoom = zoom;
self.last_render.rect = Rect::default();
Some(InputAction::SwitchRenderZoom(f_or_f))
}
'o' if can_zoom => self.update_zoom(Zoom::step_in),
'O' if can_zoom => self.update_zoom(Zoom::step_out),
'L' if can_zoom => self.update_zoom(|z| z.pan(Direction::Right)),
'H' if can_zoom => self.update_zoom(|z| z.pan(Direction::Left)),
'J' if can_zoom => self.update_zoom(|z| z.pan(Direction::Down)),
'K' if can_zoom => self.update_zoom(|z| z.pan(Direction::Up)),
'G' if can_zoom => self.update_zoom(Zoom::pan_top),
'0' if can_zoom => self.update_zoom(Zoom::pan_left),
'$' if can_zoom => self.update_zoom(Zoom::pan_right),
'r' => Some(InputAction::Rotate),
_ => None
},
KeyCode::Backspace
if let BottomMessage::Input(InputCommand::Search(ref mut term)) =
self.bottom_msg =>
{
term.pop();
InputAction::Redraw.into()
}
KeyCode::Right => self.change_page(PageChange::Next, ChangeAmount::Single),
KeyCode::Down | KeyCode::PageDown =>