Compare commits

..

2 Commits

Author SHA1 Message Date
itsjunetime 9e9053acbc Add note to README that contributions will be licensed as MPL-2.0 2024-12-01 11:30:08 -07:00
itsjunetime e67a2ec421 Relicense to GPLv3 since poppler is GPL and we must not violate that license 2024-11-15 23:14:53 -07:00
16 changed files with 888 additions and 1998 deletions
-38
View File
@@ -1,38 +0,0 @@
name: Rust
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
env:
CARGO_TERM_COLOR: always
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Setup sccache
if: github.event_name != 'release' && github.event_name != 'workflow_dispatch'
uses: mozilla-actions/sccache-action@v0.0.6
- name: Configure sccache
if: github.event_name != 'release' && github.event_name != 'workflow_dispatch'
run: |
echo "SCCACHE_GHA_ENABLED=true" >> $GITHUB_ENV
echo "RUSTC_WRAPPER=sccache" >> $GITHUB_ENV
- name: Install build dependencies
run: |
sudo apt-get update
sudo apt-get install -y libfontconfig1-dev libgoogle-perftools-dev google-perftools
- uses: actions/checkout@v4
- name: Clippy
run: cargo clippy -- -D warnings
- name: Check fmt
run: cargo fmt -- --check
- name: Run tests
run: cargo test --benches -- adobe_example
- name: Build
run: cargo build
-11
View File
@@ -1,21 +1,10 @@
# Unreleased
- Update ratatui(-image) dependencies
- Enable Ctrl+Z/Suspend functionality
- Rewrite with mupdf as the backend for much better performance and rendering quality
# v0.2.0
- Add `--r-to-l` flag to support displaying pdfs that read from right to left
- Add `--max-wide` flag to restrict amount of pages that can appear on the screen at a time
- Small internal changes to accomodate a few more clippy lints
- Update `ratatui` and `ratatui-image` git dependencies to latest upstream
- Move `ratatui-image/vb64` support under `nightly` feature, enabled by default
- Fixed a bug where jumping to a page out of range could result in weird `esc` key behavior
- Added CI ([#31](https://github.com/itsjunetime/tdf/pull/31), thank you [@Kriejstal](https://github.com/Kreijstal))
- Changed global allocator to [`mimalloc`](https://github.com/purpleprotocol/mimalloc_rust) for slightly improved performance
- Fixed issue with document reloading not working when files are intermedially deleted
- Fixed a lot of weirdness with bottom message layering/updating
# v0.1.0
Generated
+502 -1416
View File
File diff suppressed because it is too large Load Diff
+9 -106
View File
@@ -1,44 +1,42 @@
[package]
name = "tdf-viewer"
version = "0.2.0"
name = "tdf"
version = "0.1.0"
authors = ["June Welker <junewelker@gmail.com>"]
edition = "2021"
description = "A terminal viewer for PDFs"
readme = "README.md"
homepage = "https://github.com/itsjunetime/tdf"
repository = "https://github.com/itsjunetime/tdf"
license = "AGPL-3.0-only"
license = "GPL-3.0-or-later"
keywords = ["pdf", "tui", "cli", "terminal"]
categories = ["command-line-utilities", "text-processing", "visualization"]
default-run = "tdf"
[[bin]]
name = "tdf"
path = "src/main.rs"
# lib only exists for benching
[lib]
name = "tdf"
[dependencies]
poppler-rs = { version = "0.24.1", default-features = false, features = ["v23_7"] }
cairo-rs = { version = "0.20.0", default-features = false, features = ["png"] }
# we're using this branch because it has significant performance fixes that I'm waiting on responses from the upstream devs to get upstreamed. See https://github.com/ratatui-org/ratatui/issues/1116
ratatui = { git = "https://github.com/itsjunetime/ratatui.git" }
# ratatui = { path = "./ratatui/ratatui" }
# We're using this to have the vb64 feature (for faster base64 encoding, since that does take up a good bit of time when converting images to the `Protocol`. It also just includes a few more features that I'm waiting on main to upstream
# We're using this to have the vb64 feature (for faster base64 encoding, since that does take up a good bit of time when converting images to the Box<dyn ratatui_image::Protocol>. It also just includes a few more features that I'm waiting on main to upstream
ratatui-image = { git = "https://github.com/itsjunetime/ratatui-image.git", branch = "vb64_on_personal", default-features = false }
# ratatui-image = { path = "./ratatui-image", features = ["vb64"], default-features = false }
crossterm = { version = "0.28.1", features = ["event-stream"] }
image = { version = "0.25.1", features = ["pnm", "rayon"], default-features = false }
notify = { version = "8.0.0", features = ["crossbeam-channel"] }
image = { version = "0.25.1", features = ["png", "rayon"], default-features = false }
notify = { version = "7.0.0", features = ["crossbeam-channel"] }
tokio = { version = "1.37.0", features = ["rt-multi-thread", "macros"] }
futures-util = { version = "0.3.30", default-features = false }
glib = "0.20.0"
itertools = "*"
flume = { version = "0.11.0", default-features = false, features = ["async"] }
xflags = "0.4.0-pre.2"
mimalloc = "0.1.43"
nix = { version = "0.29.0", features = ["signal"] }
mupdf = { git = "https://github.com/itsjunetime/mupdf-rs", branch = "remove_debug_print", default-features = false, features = ["svg", "system-fonts", "img"] }
rayon = { version = "*", default-features = false }
# for tracing with tokio-console
console-subscriber = { version = "0.4.0", optional = true }
@@ -51,8 +49,6 @@ lto = "fat"
default = ["nightly"]
nightly = ["ratatui-image/vb64"]
tracing = ["tokio/tracing", "dep:console-subscriber"]
epub = ["mupdf/epub"]
cbz = ["mupdf/cbz"]
[dev-dependencies]
criterion = { version = "0.5.1", features = ["async_tokio"] }
@@ -65,96 +61,3 @@ harness = false
[[bin]]
name = "for_profiling"
path = "./benches/for_profiling.rs"
[lints.clippy]
uninlined_format_args = "warn"
redundant_closure_for_method_calls = "warn"
cast_lossless = "warn"
single_char_pattern = "warn"
manual_let_else = "warn"
ignored_unit_patterns = "warn"
range_plus_one = "warn"
unreadable_literal = "warn"
redundant_else = "warn"
assigning_clones = "warn"
bool_to_int_with_if = "warn"
borrow_as_ptr = "warn"
cast_ptr_alignment = "warn"
checked_conversions = "warn"
copy_iterator = "warn"
default_trait_access = "warn"
doc_link_with_quotes = "warn"
empty_enum = "warn"
explicit_into_iter_loop = "warn"
explicit_iter_loop = "warn"
filter_map_next = "warn"
flat_map_option = "warn"
fn_params_excessive_bools = "warn"
from_iter_instead_of_collect = "warn"
implicit_clone = "warn"
index_refutable_slice = "warn"
inefficient_to_string = "warn"
invalid_upcast_comparisons = "warn"
iter_filter_is_ok = "warn"
iter_filter_is_some = "warn"
iter_not_returning_iterator = "warn"
large_futures = "warn"
large_stack_arrays = "warn"
large_types_passed_by_value = "warn"
linkedlist = "warn"
macro_use_imports = "warn"
manual_assert = "warn"
manual_instant_elapsed = "warn"
manual_is_power_of_two = "warn"
manual_is_variant_and = "warn"
manual_ok_or = "warn"
manual_string_new = "warn"
many_single_char_names = "warn"
manual_unwrap_or = "warn"
match_on_vec_items = "warn"
match_same_arms = "warn"
match_wildcard_for_single_variants = "warn"
maybe_infinite_iter = "warn"
mismatching_type_param_order = "warn"
missing_fields_in_debug = "warn"
mut_mut = "warn"
needless_bitwise_bool = "warn"
needless_continue = "warn"
needless_for_each = "warn"
needless_pass_by_value = "warn"
needless_raw_string_hashes = "warn"
no_effect_underscore_binding = "warn"
no_mangle_with_rust_abi = "warn"
option_as_ref_cloned = "warn"
option_option = "warn"
ptr_as_ptr = "warn"
ptr_cast_constness = "warn"
range_minus_one = "warn"
ref_as_ptr = "warn"
ref_binding_to_reference = "warn"
ref_option = "warn"
ref_option_ref = "warn"
return_self_not_must_use = "warn"
same_functions_in_if_condition = "warn"
should_panic_without_expect = "warn"
similar_names = "warn"
stable_sort_primitive = "warn"
str_split_at_newline = "warn"
struct_excessive_bools = "warn"
struct_field_names = "warn"
transmute_ptr_to_ptr = "warn"
trivially_copy_pass_by_ref = "warn"
unicode_not_nfc = "warn"
unnecessary_box_returns = "warn"
unnecessary_join = "warn"
unnecessary_literal_bound = "warn"
unnecessary_wraps = "warn"
unnested_or_patterns = "warn"
unused_async = "warn"
unused_self = "warn"
used_underscore_binding = "warn"
used_underscore_items = "warn"
zero_sized_map_values = "warn"
[patch.crates-io]
pathfinder_simd = { git = "https://github.com/itsjunetime/pathfinder.git" }
+80 -67
View File
@@ -1,21 +1,23 @@
GNU AFFERO GENERAL PUBLIC LICENSE
Version 3, 19 November 2007
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The GNU Affero General Public License is a free, copyleft license for
software and other kinds of works, specifically designed to ensure
cooperation with the community in the case of network server software.
The GNU General Public License is a free, copyleft license for
software and other kinds of works.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
our General Public Licenses are intended to guarantee your freedom to
the GNU General Public License is intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users.
software for all its users. We, the Free Software Foundation, use the
GNU General Public License for most of our software; it applies also to
any other work released this way by its authors. You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
@@ -24,34 +26,44 @@ them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
Developers that use our General Public Licenses protect your rights
with two steps: (1) assert copyright on the software, and (2) offer
you this License which gives you legal permission to copy, distribute
and/or modify the software.
To protect your rights, we need to prevent others from denying you
these rights or asking you to surrender the rights. Therefore, you have
certain responsibilities if you distribute copies of the software, or if
you modify it: responsibilities to respect the freedom of others.
A secondary benefit of defending all users' freedom is that
improvements made in alternate versions of the program, if they
receive widespread use, become available for other developers to
incorporate. Many developers of free software are heartened and
encouraged by the resulting cooperation. However, in the case of
software used on network servers, this result may fail to come about.
The GNU General Public License permits making a modified version and
letting the public access it on a server without ever releasing its
source code to the public.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must pass on to the recipients the same
freedoms that you received. You must make sure that they, too, receive
or can get the source code. And you must show them these terms so they
know their rights.
The GNU Affero General Public License is designed specifically to
ensure that, in such cases, the modified source code becomes available
to the community. It requires the operator of a network server to
provide the source code of the modified version running there to the
users of that server. Therefore, public use of a modified version, on
a publicly accessible server, gives the public access to the source
code of the modified version.
Developers that use the GNU GPL protect your rights with two steps:
(1) assert copyright on the software, and (2) offer you this License
giving you legal permission to copy, distribute and/or modify it.
An older license, called the Affero General Public License and
published by Affero, was designed to accomplish similar goals. This is
a different license, not a version of the Affero GPL, but Affero has
released a new version of the Affero GPL which permits relicensing under
this license.
For the developers' and authors' protection, the GPL clearly explains
that there is no warranty for this free software. For both users' and
authors' sake, the GPL requires that modified versions be marked as
changed, so that their problems will not be attributed erroneously to
authors of previous versions.
Some devices are designed to deny users access to install or run
modified versions of the software inside them, although the manufacturer
can do so. This is fundamentally incompatible with the aim of
protecting users' freedom to change the software. The systematic
pattern of such abuse occurs in the area of products for individuals to
use, which is precisely where it is most unacceptable. Therefore, we
have designed this version of the GPL to prohibit the practice for those
products. If such problems arise substantially in other domains, we
stand ready to extend this provision to those domains in future versions
of the GPL, as needed to protect the freedom of users.
Finally, every program is threatened constantly by software patents.
States should not allow patents to restrict development and use of
software on general-purpose computers, but in those that do, we wish to
avoid the special danger that patents applied to a free program could
make it effectively proprietary. To prevent this, the GPL assures that
patents cannot be used to render the program non-free.
The precise terms and conditions for copying, distribution and
modification follow.
@@ -60,7 +72,7 @@ modification follow.
0. Definitions.
"This License" refers to version 3 of the GNU Affero General Public License.
"This License" refers to version 3 of the GNU General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
@@ -537,45 +549,35 @@ to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
13. Remote Network Interaction; Use with the GNU General Public License.
Notwithstanding any other provision of this License, if you modify the
Program, your modified version must prominently offer all users
interacting with it remotely through a computer network (if your version
supports such interaction) an opportunity to receive the Corresponding
Source of your version by providing access to the Corresponding Source
from a network server at no charge, through some standard or customary
means of facilitating copying of software. This Corresponding Source
shall include the Corresponding Source for any work covered by version 3
of the GNU General Public License that is incorporated pursuant to the
following paragraph.
13. Use with the GNU Affero General Public License.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU General Public License into a single
under version 3 of the GNU Affero General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the work with which it is combined will remain governed by version
3 of the GNU General Public License.
but the special requirements of the GNU Affero General Public License,
section 13, concerning interaction through a network will apply to the
combination as such.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU Affero General Public License from time to time. Such new versions
will be similar in spirit to the present version, but may differ in detail to
the GNU General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the
Program specifies that a certain numbered version of the GNU Affero General
Program specifies that a certain numbered version of the GNU General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU Affero General Public License, you may choose any version ever published
GNU General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU Affero General Public License can be used, that proxy's
versions of the GNU General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
@@ -633,29 +635,40 @@ the "copyright" line and a pointer to where the full notice is found.
Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
GNU General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If your software can interact with users remotely through a computer
network, you should also make sure that it provides a way for users to
get its source. For example, if your program is a web application, its
interface could display a "Source" link that leads users to an archive
of the code. There are many ways you could offer source, and different
solutions will be better for different programs; see section 13 for the
specific requirements.
If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:
<program> Copyright (C) <year> <name of author>
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU AGPL, see
<http://www.gnu.org/licenses/>.
For more information on this, and how to apply and follow the GNU GPL, see
<https://www.gnu.org/licenses/>.
The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
<https://www.gnu.org/licenses/why-not-lgpl.html>.
+2 -2
View File
@@ -14,7 +14,7 @@ Designed to be performant, very responsive, and work well with even very large P
- Reactive layout
## To Build
First, you need to install the system dependencies. This will generally only include `libfontconfig`. If you're on linux, these will probably show up in your package manager as something like `libfontconfig1-devel` or `libfontconfig-dev`.
First, you need to install the system dependencies. This includes packages such as (but not limited to) `cairo`, `gtk`, and `poppler`. If you're on linux, these will probably show up in your package manager as something like `libcairo-devel` or `cairo-dev`.
If it turns out that you're missing one of these, it will fail to compile and tell you what library you're missing. Find the development package for that library in your package manager, install it, and try to build again. Now, the important steps:
@@ -30,4 +30,4 @@ I dunno. Just for fun, mostly.
Yeah, sure. Please do.
Please note, though, that all contributions will be treated as licensed under MPL-2.0.
Please note, though, that all contributions will be treated as licensed under MPL-2.0. This is so that we can relicense to MPL-2.0 at some point in the future if we manage to move away from poppler as a backend (since that is the only dependency, at time of writing, which requires the GPLv3 license).
+5 -6
View File
@@ -71,7 +71,7 @@ pub async fn render_first_page(path: impl AsRef<Path>) {
} = start_all_rendering(path);
// we only want to render until the first page is ready to be printed
while pages.iter().all(Option::is_none) {
while pages.iter().all(|p| p.is_none()) {
tokio::select! {
Some(renderer_msg) = from_render_rx.next() => {
handle_renderer_msg(renderer_msg, &mut pages, &mut to_converter_tx);
@@ -94,15 +94,14 @@ async fn render_all_files(path: &'static str) -> Vec<PageInfo> {
while let Some(info) = from_render_rx.next().await {
match info.expect("Renderer ran into an error while rendering") {
RenderInfo::Reloaded => (),
RenderInfo::NumPages(num) => fill_default(&mut pages, num),
RenderInfo::Page(page) => {
let num = page.page_num;
let num = page.page;
pages[num] = Some(page);
}
};
if pages.iter().all(Option::is_some) {
if pages.iter().all(|p| p.is_some()) {
break;
}
}
@@ -137,7 +136,7 @@ async fn convert_all_files(files: Vec<PageInfo>) {
}
}
while converted.iter().any(Option::is_none) {
while converted.iter().any(|p| p.is_none()) {
let page = from_converter_rx
.next()
.await
@@ -158,7 +157,7 @@ impl Profiler for CpuProfiler {
fn start_profiling(&mut self, benchmark_id: &str, _: &std::path::Path) {
let file = format!(
"./{}-{}.profile",
benchmark_id.replace('/', "-"),
benchmark_id.replace("/", "-"),
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
+1 -3
View File
@@ -21,8 +21,6 @@ pub fn handle_renderer_msg(
to_converter_tx.send(ConverterMsg::NumPages(num)).unwrap();
}
Ok(RenderInfo::Page(info)) => to_converter_tx.send(ConverterMsg::AddImg(info)).unwrap(),
// We can ignore the `Reloaded` variant 'cause that's only used to send info to the TUI
Ok(RenderInfo::Reloaded) => (),
Err(e) => panic!("Got error from renderer: {e:?}")
}
}
@@ -63,7 +61,7 @@ pub fn start_rendering_loop(
Sender<RenderNotif>
) {
let pathbuf = path.as_ref().canonicalize().unwrap();
let str_path = pathbuf.into_os_string().to_string_lossy().to_string();
let str_path = format!("file://{}", pathbuf.into_os_string().to_string_lossy());
let (to_render_tx, from_main_rx) = unbounded();
let (to_main_tx, from_render_rx) = unbounded();
+1 -1
Submodule ratatui updated: 1166bebf44...8bf0c1ef77
+13
View File
@@ -0,0 +1,13 @@
#!/usr/bin/env bash
# 1. Pull the git source of poppler
# 2. cd poppler
# 3. git checkout poppler-23.07.0
# 4. mkdir build
# 5. cd build
# 6. cmake .. -DENABLE_GPGME=OFF -DENABLE_QT5=OFF -DENABLE_QT6=OFF -DENABLE_BOOST=OFF -DBUILD_SHARED_LIBS=OFF
# 7. cmake --build . --parallel $(nproc)
env SYSTEM_DEPS_POPPLER_GLIB_LINK=static \
SYSTEM_DEPS_POPPLER_GLIB_NO_PKG_CONFIG=1 \
SYSTEM_DEPS_POPPLER_GLIB_SEARCH_NATIVE=/path/to/poppler/build/glib \
SYSTEM_DEPS_POPPLER_GLIB_LIB=poppler-glib \
cargo perf --bin for_profiling --
+11 -24
View File
@@ -1,9 +1,8 @@
use flume::{Receiver, SendError, Sender, TryRecvError};
use futures_util::stream::StreamExt;
use image::DynamicImage;
use image::ImageFormat;
use itertools::Itertools;
use ratatui_image::{picker::Picker, protocol::Protocol, Resize};
use rayon::iter::ParallelIterator;
use crate::renderer::{fill_default, PageInfo, RenderError};
@@ -55,25 +54,13 @@ 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 img_area = page_info.img_data.area;
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!()
};
let img_area = page_info.img_data.cell_area;
let dyn_img =
image::load_from_memory_with_format(&page_info.img_data.data, ImageFormat::Png)
.map_err(|e| {
RenderError::Render(format!("Couldn't convert Vec<u8> to DynamicImage: {e}"))
})?;
// We don't actually want to Crop this image, but we've already
// verified (with the ImageSurface stuff) that the image is the correct
@@ -82,7 +69,7 @@ pub async fn run_conversion_loop(
let txt_img = picker
.new_protocol(dyn_img, img_area, Resize::None)
.map_err(|e| {
RenderError::Converting(format!(
RenderError::Render(format!(
"Couldn't convert DynamicImage to ratatui image: {e}"
))
})?;
@@ -92,15 +79,15 @@ pub async fn run_conversion_loop(
Ok(Some(ConvertedPage {
page: txt_img,
num: page_info.page_num,
num_results: page_info.result_rects.len()
num: page_info.page,
num_results: page_info.search_results
}))
}
fn handle_notif(msg: ConverterMsg, images: &mut Vec<Option<PageInfo>>, page: &mut usize) {
match msg {
ConverterMsg::AddImg(img) => {
let page_num = img.page_num;
let page_num = img.page;
images[page_num] = Some(img);
}
ConverterMsg::NumPages(n_pages) => {
-3
View File
@@ -1,6 +1,3 @@
#[global_allocator]
static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc;
pub mod converter;
pub mod renderer;
pub mod skip;
+38 -51
View File
@@ -1,10 +1,10 @@
use std::{
ffi::OsString,
io::{stdout, Read, Write},
num::NonZeroUsize,
path::PathBuf
};
use converter::{run_conversion_loop, ConvertedPage, ConverterMsg};
use crossterm::{
execute,
terminal::{
@@ -13,14 +13,17 @@ use crossterm::{
}
};
use futures_util::{stream::StreamExt, FutureExt};
use glib::{LogField, LogLevel, LogWriterOutput};
use notify::{Event, EventKind, RecursiveMode, Watcher};
use ratatui::{backend::CrosstermBackend, Terminal};
use ratatui_image::picker::Picker;
use tdf::{
converter::{run_conversion_loop, ConvertedPage, ConverterMsg},
renderer::{self, RenderError, RenderInfo, RenderNotif},
tui::{BottomMessage, InputAction, MessageSetting, Tui}
};
use renderer::{RenderError, RenderInfo, RenderNotif};
use tui::{InputAction, Tui};
mod converter;
mod renderer;
mod skip;
mod tui;
// Dummy struct for easy errors in main
#[derive(Debug)]
@@ -57,30 +60,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let (render_tx, tui_rx) = flume::unbounded();
let watch_to_tui_tx = render_tx.clone();
let mut watcher = notify::recommended_watcher(on_notify_ev(
watch_to_tui_tx,
watch_to_render_tx,
path.file_name()
.ok_or("Path does not have a last component??")?
.to_owned()
))?;
let mut watcher =
notify::recommended_watcher(on_notify_ev(watch_to_tui_tx, watch_to_render_tx))?;
// So we have to watch the parent directory of the file that we are interested in because the
// `notify` library works on inodes, and if the file is deleted, that inode is gone as well,
// and then the notify library just gives up on trying to watch for the file reappearing. Imo
// they should start watching the parent directory if the file is deleted, and then wait for it
// to reappear and then begin watching it again, but whatever. It seems they've made their
// opinion on this clear
// (https://github.com/notify-rs/notify/issues/113#issuecomment-281836995) so whatever, guess
// we have to do this annoying workaround.
watcher.watch(
path.parent().expect("The root directory is not a PDF"),
RecursiveMode::NonRecursive
)?;
// We're making this nonrecursive 'cause we're just watching a single file, so there's nothing
// to recurse into
watcher.watch(&path, RecursiveMode::NonRecursive)?;
// TODO: Handle non-utf8 file names? Maybe by constructing a CString and passing that in to the
// mupdf stuff instead of a rust string?
let file_path = path.clone().into_os_string().to_string_lossy().to_string();
// poppler stuff instead of a rust string?
let file_path = format!("file://{}", path.clone().into_os_string().to_string_lossy());
let mut window_size = window_size()?;
@@ -154,12 +143,17 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|| "Unknown file".into(),
|n| n.to_string_lossy().to_string()
);
let mut tui = Tui::new(file_name, flags.max_wide, flags.r_to_l.unwrap_or_default());
let mut tui = tui::Tui::new(file_name, flags.max_wide, flags.r_to_l.unwrap_or_default());
let backend = CrosstermBackend::new(std::io::stdout());
let mut term = Terminal::new(backend)?;
term.skip_diff(true);
// poppler has some annoying logging (e.g. if you request a page index out-of-bounds of a
// document's pages, then it will return `None`, but still log to stderr with CRITICAL level),
// so we want to just ignore all logging since this is a tui app.
glib::log_set_writer_func(noop);
execute!(
term.backend_mut(),
EnterAlternateScreen,
@@ -167,7 +161,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
)?;
enable_raw_mode()?;
let mut main_area = Tui::main_layout(&term.get_frame());
let mut main_area = tui::Tui::main_layout(&term.get_frame());
tui_tx.send(RenderNotif::Area(main_area[1]))?;
let mut tui_rx = tui_rx.into_stream();
@@ -196,16 +190,20 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
},
Some(renderer_msg) = tui_rx.next() => {
match renderer_msg {
Ok(render_info) => match render_info {
// if an Ok comes through, we know the error has been resolved ('cause it kinda
// bails whenever we run into an error) so just clear it
Ok(render_info) => {
match render_info {
RenderInfo::NumPages(num) => {
tui.set_n_pages(num);
to_converter.send(ConverterMsg::NumPages(num))?;
},
RenderInfo::Page(info) => {
tui.got_num_results_on_page(info.page_num, info.result_rects.len());
tui.got_num_results_on_page(info.page, info.search_results);
to_converter.send(ConverterMsg::AddImg(info))?;
},
RenderInfo::Reloaded => tui.set_msg(MessageSetting::Some(BottomMessage::Reloaded)),
}
tui.set_bottom_msg(None);
},
Err(e) => tui.show_error(e),
}
@@ -245,8 +243,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
fn on_notify_ev(
to_tui_tx: flume::Sender<Result<RenderInfo, RenderError>>,
to_render_tx: flume::Sender<RenderNotif>,
file_name: OsString
to_render_tx: flume::Sender<RenderNotif>
) -> impl Fn(notify::Result<Event>) {
move |res| match res {
// If we get an error here, and then an error sending, everything's going wrong. Just give
@@ -254,29 +251,19 @@ fn on_notify_ev(
Err(e) => to_tui_tx.send(Err(RenderError::Notify(e))).unwrap(),
// TODO: Should we match EventKind::Rename and propogate that so that the other parts of the
// process know that too? Or should that be
Ok(ev) => {
// We only watch the parent directory (see the comment above `watcher.watch` in `fn
// main`) so we need to filter out events to only ones that pertain to the single file
// we care about
if !ev
.paths
.iter()
.any(|path| path.file_name().is_some_and(|f| f == file_name))
{
return;
}
match ev.kind {
Ok(ev) => match ev.kind {
EventKind::Access(_) => (),
EventKind::Remove(_) => to_tui_tx
.send(Err(RenderError::Converting("File was deleted".into())))
.unwrap(),
EventKind::Remove(_) =>
drop(to_tui_tx.send(Err(RenderError::Render("File was deleted".into())))),
// This shouldn't fail to send unless the receiver gets disconnected. If that's
// happened, then like the main thread has panicked or something, so it doesn't matter
// we don't handle the error here.
EventKind::Other | EventKind::Any | EventKind::Create(_) | EventKind::Modify(_) =>
to_render_tx.send(RenderNotif::Reload).unwrap(),
drop(to_render_tx.send(renderer::RenderNotif::Reload)),
}
}
}
fn noop(_: LogLevel, _: &[LogField<'_>]) -> LogWriterOutput {
LogWriterOutput::Handled
}
+144 -128
View File
@@ -1,9 +1,10 @@
use std::{thread::sleep, time::Duration};
use std::thread;
use cairo::{Antialias, Context, Format, Surface};
use crossterm::terminal::WindowSize;
use flume::{Receiver, SendError, Sender, TryRecvError};
use itertools::Itertools;
use mupdf::{Colorspace, Document, Matrix, Page, Pixmap};
use poppler::{Color, Document, FindFlags, Page, Rectangle, SelectionStyle};
use ratatui::layout::Rect;
pub enum RenderNotif {
@@ -16,27 +17,28 @@ pub enum RenderNotif {
#[derive(Debug)]
pub enum RenderError {
Notify(notify::Error),
Doc(mupdf::error::Error),
Converting(String)
Doc(glib::Error),
// Don't like storing an error as a string but it needs to be Send to send to the main thread,
// and it's just going to be shown to the user, so whatever
Render(String)
}
pub enum RenderInfo {
NumPages(usize),
Page(PageInfo),
Reloaded
Page(PageInfo)
}
#[derive(Clone)]
pub struct PageInfo {
pub img_data: ImageData,
pub page_num: usize,
pub result_rects: Vec<HighlightRect>
pub page: usize,
pub search_results: usize
}
#[derive(Clone)]
pub struct ImageData {
pub pixels: Vec<u8>,
pub cell_area: Rect
pub data: Vec<u8>,
pub area: Rect
}
#[derive(Default)]
@@ -53,7 +55,7 @@ pub fn fill_default<T: Default>(vec: &mut Vec<T>, size: usize) {
}
}
// this function has to be sync (non-async) because the mupdf::Document needs to be held during
// this function has to be sync (non-async) because the poppler::Document needs to be held during
// most of it, but that's basically just a wrapper around `*c_void` cause it's just a binding to C
// code, so it's !Send and thus can't be held across await points. So we can't call any of the
// async `send` or `recv` methods in this function body, since those create await points. Which
@@ -68,17 +70,19 @@ pub fn fill_default<T: Default>(vec: &mut Vec<T>, size: usize) {
#[allow(clippy::needless_pass_by_value)]
pub fn start_rendering(
path: &str,
sender: Sender<Result<RenderInfo, RenderError>>,
mut sender: Sender<Result<RenderInfo, RenderError>>,
receiver: Receiver<RenderNotif>,
size: WindowSize
) -> Result<(), SendError<Result<RenderInfo, RenderError>>> {
// first, wait 'til we get told what the current starting area is so that we can set it to
// know what to render to
let mut area = loop {
let mut area;
loop {
if let RenderNotif::Area(r) = receiver.recv().unwrap() {
break r;
area = r;
break;
}
}
};
// We want this outside of 'reload so that if the doc reloads, the search term that somebody
// set will still get highlighted in the reloaded doc
@@ -89,17 +93,11 @@ pub fn start_rendering(
let col_w = size.width / size.columns;
let col_h = size.height / size.rows;
let mut stored_doc = None;
'reload: loop {
let doc = match Document::open(path) {
let doc = match Document::from_file(path, None) {
Err(e) => {
// if there's an error, tell the main loop
sender.send(Err(RenderError::Doc(e)))?;
match stored_doc {
Some(ref d) => d,
None => {
// then wait for a reload notif (since what probably happened is that the file was
// temporarily removed to facilitate a save or something like that)
while let Ok(msg) = receiver.recv() {
@@ -112,26 +110,10 @@ pub fn start_rendering(
// done, so we're fine to just return
return Ok(());
}
}
}
Ok(d) => {
if stored_doc.is_some() {
sender.send(Ok(RenderInfo::Reloaded))?;
}
&*stored_doc.insert(d)
}
};
let n_pages = match doc.page_count() {
Ok(n) => n as usize,
Err(e) => {
sender.send(Err(RenderError::Doc(e)))?;
// just basic backoff i think
sleep(Duration::from_secs(1));
continue 'reload;
}
Ok(d) => d
};
let n_pages = doc.n_pages() as usize;
sender.send(Ok(RenderInfo::NumPages(n_pages)))?;
// We're using this vec of bools to indicate which page numbers have already been rendered,
@@ -211,8 +193,8 @@ pub fn start_rendering(
.map(|(idx, p)| (start_point - (idx + 1), p))
);
let area_w = f32::from(area.width) * f32::from(col_w);
let area_h = f32::from(area.height) * f32::from(col_h);
let area_w = f64::from(area.width) * f64::from(col_w);
let area_h = f64::from(area.height) * f64::from(col_h);
// we go through each page
for (num, rendered) in page_iter {
@@ -236,12 +218,12 @@ pub fn start_rendering(
// We know this is in range 'cause we're iterating over it but we still just want
// to be safe
let page = match doc.load_page(num as i32) {
Err(e) => {
sender.send(Err(RenderError::Doc(e)))?;
let Some(page) = doc.page(num as i32) else {
sender.send(Err(RenderError::Render(format!(
"Couldn't get page {num} ({}) of doc?",
num as i32
))))?;
continue;
}
Ok(p) => p
};
let rendered_with_no_results =
@@ -263,34 +245,26 @@ pub fn start_rendering(
// we make a potentially incorrect assumption here that writing the context
// to a png won't fail, and mark that it all rendered correctly here before
// spawning off the thread to do so and send it.
rendered.contained_term = Some(ctx.result_rects.is_empty());
rendered.contained_term = Some(ctx.num_results > 0);
rendered.successful = true;
let cap = (ctx.pixmap.width()
* ctx.pixmap.height() * u32::from(ctx.pixmap.n()))
as usize;
let mut pixels = Vec::with_capacity(cap);
if let Err(e) = ctx.pixmap.write_to(&mut pixels, mupdf::ImageFormat::PNM) {
sender.send(Err(RenderError::Doc(e)))?;
continue;
};
sender.send(Ok(RenderInfo::Page(PageInfo {
img_data: ImageData {
pixels,
cell_area: Rect {
x: 0,
y: 0,
width: (ctx.surface_w / f32::from(col_w)) as u16,
height: (ctx.surface_h / f32::from(col_h)) as u16
// if this is the page that the user is currently trying to look at, don't
// bother spawning off a thread to render it to a png - it'll only slow
// down the time til the user can see it (due to the overhead of creating a
// thread), but we still want to spawn threads to render the other pages
// since the effects of parallelizing that will be noticeable if the user
// tries to move through pages more quickly
if num == start_point {
render_ctx_to_png(&ctx, &mut sender, (col_w, col_h), num)?;
} else {
let mut sender = sender.clone();
thread::spawn(move || {
render_ctx_to_png(&ctx, &mut sender, (col_w, col_h), num)
});
}
},
page_num: num,
result_rects: ctx.result_rects
})))?;
}
// And if we got an error, then obviously we need to propagate that
Err(e) => sender.send(Err(RenderError::Doc(e)))?
Err(e) => sender.send(Err(RenderError::Render(e)))?
}
}
@@ -309,38 +283,35 @@ pub fn start_rendering(
}
struct RenderedContext {
pixmap: Pixmap,
surface_w: f32,
surface_h: f32,
result_rects: Vec<HighlightRect>
surface: Surface,
num_results: usize,
surface_width: f64,
surface_height: f64
}
/// SAFETY: I think this is safe because, although the backing struct for `Surface` does contain
/// pointers to like the cairo_backend_t struct that all the cairo stuff is using, that struct is
/// basically just a vtable, so accessing it from multiple threads *should* be safe since we're
/// just calling the same functions with different data. The only other thing it holds reference to
/// is a `cairo_device_t`, but that seems to be thread-safe because it's managed through ref counts
/// and a mutex. Also, as far as I can tell from reading the source code, write_to_png_stream (the
/// only function we call on this struct) doesn't access the device at all, so we should be fine
/// there.
/// We want this to be Send so that we can delegate the png writing to a separate thread (since
/// that's the thing that takes the most time, by far, in this app).
unsafe impl Send for RenderedContext {}
fn render_single_page_to_ctx(
page: &Page,
search_term: Option<&str>,
already_rendered_no_results: bool,
(area_w, area_h): (f32, f32)
) -> Result<Option<RenderedContext>, mupdf::error::Error> {
let mut max_hits = 10;
let result_rects = loop {
let rects = search_term
(area_w, area_h): (f64, f64)
) -> Result<Option<RenderedContext>, String> {
let mut result_rects = search_term
.as_ref()
// mupdf allocates a buffer of the size we give it to try to fill it with results. If we
// pass in u32::MAX, it allocates too much memory to function. If we pass too small of a
// number in, we may miss out on some of the results. Ideally, we'd like to make a better
// interface than this, but we're stuck with this kinda ugly looping until we make sure
// that we've found every instance of it on this page.
.map(|term| page.search(term, max_hits))
.transpose()?
.map(|term| page.find_text_with_options(term, FindFlags::DEFAULT | FindFlags::MULTILINE))
.unwrap_or_default();
if rects.len() < (max_hits as usize) {
break rects;
}
max_hits *= 2;
};
// If there are no search terms on this page, and we've already rendered it with no search
// terms, then just return none to avoid this computation
if result_rects.is_empty() && already_rendered_no_results {
@@ -348,8 +319,7 @@ fn render_single_page_to_ctx(
}
// then, get the size of the page
let bounds = page.bounds()?;
let (p_width, p_height) = (bounds.x1 - bounds.x0, bounds.y1 - bounds.y0);
let (p_width, p_height) = page.size();
// and get its aspect ratio
let p_aspect_ratio = p_width / p_height;
@@ -371,47 +341,93 @@ fn render_single_page_to_ctx(
area_h / p_height
};
let surface_w = p_width * scale_factor;
let surface_h = p_height * scale_factor;
let surface_width = p_width * scale_factor;
let surface_height = p_height * scale_factor;
let colorspace = Colorspace::device_rgb();
let matrix = Matrix::new_scale(scale_factor, scale_factor);
let surface = cairo::ImageSurface::create(
Format::Rgb16_565,
// No matter how big you make these arguments, the image will be drawn at the same
// size. So if you make them really big, the image will be drawn on a quarter of it. If
// you make them really small, the image will cover more than all of the surface.
//
// However, that only stands as long as you don't scale the context that you place this
// surface into. If you scale the dimensions of this image by n, then scale the context
// by that same amount, then it'll still fit perfectly into the context, but be
// rendered at higher quality.
surface_width as i32,
surface_height as i32
)
.map_err(|e| format!("Couldn't create ImageSurface: {e}"))?;
surface.set_device_scale(scale_factor, scale_factor);
let mut pixmap = page.to_pixmap(&matrix, &colorspace, 0.0, false)?;
let ctx = Context::new(surface).map_err(|e| format!("Couldn't create Context: {e}"))?;
let (x_res, y_res) = pixmap.resolution();
let new_x = (x_res as f32 * scale_factor) as i32;
let new_y = (y_res as f32 * scale_factor) as i32;
pixmap.set_resolution(new_x, new_y);
// The default background color of PDFs (at least, I think) is white, so we need to set
// that as the background color, then paint, then render.
ctx.set_source_rgba(1.0, 1.0, 1.0, 1.0);
let result_rects = result_rects
.into_iter()
.map(|quad| {
let ul_x = (quad.ul.x * scale_factor) as u32;
let ul_y = (quad.ul.y * scale_factor) as u32;
let lr_x = (quad.lr.x * scale_factor) as u32;
let lr_y = (quad.lr.y * scale_factor) as u32;
HighlightRect {
ul_x,
ul_y,
lr_x,
lr_y
ctx.set_antialias(Antialias::None);
ctx.paint()
.map_err(|e| format!("Couldn't paint Context: {e}"))?;
page.render(&ctx);
let num_results = result_rects.len();
if !result_rects.is_empty() {
let mut highlight_color = Color::new();
highlight_color.set_red((u16::MAX / 5) * 4);
highlight_color.set_green((u16::MAX / 5) * 4);
let mut old_rect = Rectangle::new();
for rect in &mut result_rects {
// According to https://gitlab.freedesktop.org/poppler/poppler/-/issues/763, these rects
// need to be corrected since they use different references as the y-coordinate base
rect.set_y1(p_height - rect.y1());
rect.set_y2(p_height - rect.y2());
page.render_selection(
&ctx,
rect,
&mut old_rect,
SelectionStyle::Glyph,
&mut Color::new(),
&mut highlight_color
);
}
}
})
.collect::<Vec<_>>();
Ok(Some(RenderedContext {
pixmap,
surface_w,
surface_h,
result_rects
surface: ctx.target(),
num_results,
surface_width,
surface_height
}))
}
#[derive(Clone)]
pub struct HighlightRect {
pub ul_x: u32,
pub ul_y: u32,
pub lr_x: u32,
pub lr_y: u32
fn render_ctx_to_png(
ctx: &RenderedContext,
sender: &mut Sender<Result<RenderInfo, RenderError>>,
(col_w, col_h): (u16, u16),
page: usize
) -> Result<(), SendError<Result<RenderInfo, RenderError>>> {
let mut img_data = Vec::with_capacity((ctx.surface_height * ctx.surface_width) as usize);
match ctx.surface.write_to_png(&mut img_data) {
Err(e) => sender.send(Err(RenderError::Render(format!(
"Couldn't write surface to png: {e}"
)))),
Ok(()) => sender.send(Ok(RenderInfo::Page(PageInfo {
img_data: ImageData {
data: img_data,
area: Rect {
width: ctx.surface_width as u16 / col_w,
height: ctx.surface_height as u16 / col_h,
x: 0,
y: 0
}
},
page,
search_results: ctx.num_results
})))
}
}
+51 -111
View File
@@ -1,16 +1,9 @@
use std::{borrow::Cow, io::stdout, num::NonZeroUsize, rc::Rc};
use std::{io::stdout, num::NonZeroUsize, rc::Rc};
use crossterm::{
event::{Event, KeyCode, KeyModifiers, MouseEventKind},
event::{Event, KeyCode, MouseEventKind},
execute,
terminal::{
disable_raw_mode, enable_raw_mode, BeginSynchronizedUpdate, EnterAlternateScreen,
LeaveAlternateScreen
}
};
use nix::{
sys::signal::{kill, Signal::SIGSTOP},
unistd::Pid
terminal::BeginSynchronizedUpdate
};
use ratatui::{
layout::{Constraint, Flex, Layout, Rect},
@@ -50,8 +43,7 @@ pub enum BottomMessage {
Help,
SearchResults(String),
Error(String),
Input(InputCommand),
Reloaded
Input(InputCommand)
}
pub enum InputCommand {
@@ -160,18 +152,18 @@ impl Tui {
let rendered_span = Span::styled(&rendered_str, Style::new().fg(Color::Cyan));
frame.render_widget(rendered_span, bottom_layout[1]);
let (msg_str, color): (Cow<'_, str>, _) = match self.bottom_msg {
let (msg_str, color) = match self.bottom_msg {
BottomMessage::Help => (
"/: Search, g: Go To Page, n: Next Search Result, N: Previous Search Result".into(),
"/: Search, g: Go To Page, n: Next Search Result, N: Previous Search Result"
.to_string(),
Color::Blue
),
BottomMessage::Error(ref e) => (e.as_str().into(), Color::Red),
BottomMessage::Error(ref e) => (format!("Couldn't render a page: {e}"), Color::Red),
BottomMessage::Input(ref input_state) => (
match input_state {
InputCommand::GoToPage(page) => format!("Go to: {page}"),
InputCommand::Search(s) => format!("Search: {s}")
}
.into(),
},
Color::Blue
),
BottomMessage::SearchResults(ref term) => {
@@ -189,12 +181,10 @@ impl Tui {
format!(
"Results for '{term}': {num_found} (searched: {}%)",
num_searched / self.rendered.len()
)
.into(),
),
Color::Blue
)
}
BottomMessage::Reloaded => ("Document was reloaded!".into(), Color::Blue)
};
let span = Span::styled(msg_str, Style::new().fg(color));
@@ -279,7 +269,7 @@ impl Tui {
fn render_single_page(&mut self, frame: &mut Frame<'_>, page_idx: usize, img_area: Rect) {
match self.rendered[page_idx].img {
Some(ref mut page_img) => frame.render_widget(Image::new(page_img), img_area),
Some(ref page_img) => frame.render_widget(Image::new(page_img), img_area),
None => Self::render_loading_in(frame, img_area)
};
}
@@ -380,16 +370,12 @@ impl Tui {
match key.code {
KeyCode::Char(c) => {
// TODO: refactor back to `if let` arm guards when those are stabilized
if let BottomMessage::Input(InputCommand::Search(ref mut term)) =
self.bottom_msg
{
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 let BottomMessage::Input(InputCommand::GoToPage(ref mut page)) = self.bottom_msg {
return c.to_digit(10).map(|input_num| {
*page = (*page * 10) + input_num as usize;
InputAction::Redraw
@@ -403,15 +389,13 @@ impl Tui {
'k' => self.change_page(PageChange::Prev, ChangeAmount::WholeScreen),
'q' => Some(InputAction::QuitApp),
'g' => {
self.set_msg(MessageSetting::Some(BottomMessage::Input(
InputCommand::GoToPage(0)
)));
self.set_bottom_msg(Some(BottomMessage::Input(InputCommand::GoToPage(0))));
Some(InputAction::Redraw)
}
'/' => {
self.set_msg(MessageSetting::Some(BottomMessage::Input(
InputCommand::Search(String::new())
)));
self.set_bottom_msg(Some(BottomMessage::Input(InputCommand::Search(
String::new()
))));
Some(InputAction::Redraw)
}
'n' if self.page < self.rendered.len() - 1 => {
@@ -440,85 +424,49 @@ impl Tui {
});
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
)
.unwrap();
disable_raw_mode().unwrap();
// 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.
kill(Pid::this(), SIGSTOP).unwrap();
enable_raw_mode().unwrap();
execute!(
&mut backend,
EnterAlternateScreen,
crossterm::cursor::Hide
)
.unwrap();
self.last_render.rect = Rect::default();
Some(InputAction::Redraw)
}
},
_ => None
}
}
},
KeyCode::Backspace => {
if let BottomMessage::Input(InputCommand::Search(ref mut term)) =
self.bottom_msg
{
if let BottomMessage::Input(InputCommand::Search(ref mut term)) = self.bottom_msg {
term.pop();
return Some(InputAction::Redraw);
}
None
}
},
KeyCode::Right => self.change_page(PageChange::Next, ChangeAmount::Single),
KeyCode::Down => self.change_page(PageChange::Next, ChangeAmount::WholeScreen),
KeyCode::Left => self.change_page(PageChange::Prev, ChangeAmount::Single),
KeyCode::Up => self.change_page(PageChange::Prev, ChangeAmount::WholeScreen),
KeyCode::Esc => match self.bottom_msg {
BottomMessage::Help => Some(InputAction::QuitApp),
_ => {
// When we hit escape, we just want to pop off the current message and
// show the underlying one.
self.set_msg(MessageSetting::Pop);
BottomMessage::Input(_) => {
self.set_bottom_msg(None);
Some(InputAction::Redraw)
}
_ => Some(InputAction::QuitApp)
},
KeyCode::Enter => {
let mut default = BottomMessage::default();
std::mem::swap(&mut self.bottom_msg, &mut default);
let BottomMessage::Input(ref cmd) = default else {
std::mem::swap(&mut self.bottom_msg, &mut default);
let BottomMessage::Input(_) = self.bottom_msg else {
return None;
};
self.set_bottom_msg(None);
let Some(BottomMessage::Input(ref cmd)) = self.prev_msg else {
// We need to verify it's an input msg currently, and only then take it
// and replace it by a default Help message. Don't exactly know how to
// do this otherwise.
unreachable!();
};
match cmd {
// Only forward the command if it's within range
InputCommand::GoToPage(page) => {
// We need to subtract 1 b/c they're tracked internally as
// 0-indexed but input and displayed as 1-indexed
let zero_page = page.saturating_sub(1);
let rendered_len = self.rendered.len();
if zero_page < rendered_len {
self.set_page(zero_page);
Some(InputAction::JumpingToPage(zero_page))
} else {
self.set_msg(MessageSetting::Some(BottomMessage::Error(
format!("Cannot jump to page {page}; there are only {rendered_len} pages in the document")
)));
Some(InputAction::Redraw)
}
let page = *page;
(page < self.rendered.len()).then(|| {
self.set_page(page);
InputAction::JumpingToPage(page)
})
}
InputCommand::Search(term) => {
let term = term.clone();
@@ -526,14 +474,14 @@ impl Tui {
// We only want to show search results if there would actually be
// data to show
if !term.is_empty() {
self.set_msg(MessageSetting::Some(
BottomMessage::SearchResults(term.clone())
));
self.set_bottom_msg(Some(BottomMessage::SearchResults(
term.clone()
)));
} else {
// else, if it's not empty, we just want to reset the bottom
// area to show the default data; we don't want it to like show
// the data from a previous search
self.set_msg(MessageSetting::Reset);
self.set_bottom_msg(Some(BottomMessage::Help));
}
// Reset all the search results
@@ -567,10 +515,10 @@ impl Tui {
}
pub fn show_error(&mut self, err: RenderError) {
self.set_msg(MessageSetting::Some(BottomMessage::Error(match err {
self.set_bottom_msg(Some(BottomMessage::Error(match err {
RenderError::Notify(e) => format!("Auto-reload failed: {e}"),
RenderError::Doc(e) => format!("Couldn't process document: {e}"),
RenderError::Converting(e) => format!("Couldn't convert page after rendering: {e}")
RenderError::Doc(e) => format!("Couldn't open document: {e}"),
RenderError::Render(e) => format!("Couldn't render page: {e}")
})));
}
@@ -584,18 +532,17 @@ impl Tui {
// We have `msg` as optional so that if they reset it to none, it'll replace it with
// `prev_msg`, but if they reset it to something else, it'll put the current thing in prev_msg
pub fn set_msg(&mut self, msg: MessageSetting) {
pub fn set_bottom_msg(&mut self, msg: Option<BottomMessage>) {
match msg {
MessageSetting::Some(mut msg) => {
Some(mut msg) => {
std::mem::swap(&mut self.bottom_msg, &mut msg);
self.prev_msg = Some(msg);
}
MessageSetting::Default => self.set_msg(MessageSetting::Some(BottomMessage::default())),
MessageSetting::Reset => {
self.prev_msg = None;
self.bottom_msg = BottomMessage::default();
None => {
let mut new_bottom = self.prev_msg.take().unwrap_or_default();
std::mem::swap(&mut self.bottom_msg, &mut new_bottom);
self.prev_msg = Some(new_bottom);
}
MessageSetting::Pop => self.bottom_msg = self.prev_msg.take().unwrap_or_default()
}
}
}
@@ -618,10 +565,3 @@ enum ChangeAmount {
WholeScreen,
Single
}
pub enum MessageSetting {
Some(BottomMessage),
Default,
Reset,
Pop
}