pdfboss-tui 0.8.0

Terminal explorer for PDF internals: element tree, object inspector, hex view and page preview
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//! Interactive terminal explorer for PDF internals, implemented from
//! ISO 32000 on top of `pdfboss-aio`'s async document model.
//!
//! State machine (`app`), pane models (`tree`, `inspector`, `hexview`,
//! `preview`, `search`), key mapping (`input`) and rendering (`ui`) are
//! pure and unit-testable; only [`run`] touches the real terminal. The
//! event loop `tokio::select!`s over the crossterm event stream, a
//! background-task message channel and a 100 ms tick, so long operations
//! (element streaming, hex fetches, search, preview rasterization) never
//! block input.

pub mod app;
pub mod hexview;
pub mod input;
pub mod inspector;
pub mod preview;
pub mod search;
pub mod tree;
pub mod ui;

use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::Duration;

use crossterm::event::{Event, EventStream};
use crossterm::execute;
use crossterm::terminal::{
    disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen,
};
use futures_util::StreamExt;
use pdfboss_aio::AsyncDocument;
use pdfboss_core::elements::{Element, ElementOpts, Span};
use pdfboss_core::ObjRef;
use ratatui::backend::CrosstermBackend;
use ratatui::Terminal;
use tokio::sync::mpsc::{self, UnboundedSender};

use crate::app::{App, Cmd, Msg};
use crate::hexview::{HexSource, WINDOW_BYTES};
use crate::inspector::InspectorPayload;
use crate::preview::{fit_scale, PreviewFrame};
use crate::search::{object_matches, SearchHit};
use crate::tree::TreeReq;

/// Elements per tree batch message.
const TREE_BATCH: usize = 64;

/// Turns crossterm's raw terminal-attach failure into an actionable
/// message when there is no real terminal to attach to, leaving any other
/// I/O error untouched.
///
/// `enable_raw_mode` opens `/dev/tty` whenever stdin isn't itself a tty (a
/// piped-stdio run under a test harness, a script, or a CI job); with no
/// controlling terminal at all that open fails `ENXIO` ("Device not
/// configured" -- raw OS error 6 on macOS, confirmed empirically: opening
/// `/dev/tty` with no controlling terminal at all reports 6, not 25), and
/// against a real non-tty device it fails `ENOTTY` ("Inappropriate ioctl
/// for device", 25 on both macOS and Linux). Neither maps to a stable,
/// matchable `io::ErrorKind` on stable Rust (macOS reports the
/// nightly-only `ErrorKind::Uncategorized` here), so the raw OS error
/// number is the only portable signal available.
fn friendly_no_tty_error(err: std::io::Error) -> std::io::Error {
    match err.raw_os_error() {
        Some(6) | Some(25) => {
            std::io::Error::new(err.kind(), "pdfboss tui requires an interactive terminal")
        }
        _ => err,
    }
}

/// Restores the terminal on drop, so panics and early returns never leave
/// the shell in raw mode.
struct TerminalGuard;

impl Drop for TerminalGuard {
    fn drop(&mut self) {
        disable_raw_mode().ok();
        execute!(std::io::stdout(), LeaveAlternateScreen).ok();
    }
}

/// Runs the explorer until the user quits. `doc` supplies all data (file-
/// or HTTP-backed); `title` labels the status bar. Document-level errors
/// become status-bar toasts; only terminal I/O errors are returned.
pub async fn run(doc: AsyncDocument, title: String) -> std::io::Result<()> {
    enable_raw_mode().map_err(friendly_no_tty_error)?;
    // The guard is constructed before `EnterAlternateScreen` (not after) so
    // that an early return from *that* fallible call still restores raw
    // mode: a local already constructed at the point of an early `?` return
    // is dropped, so there is no window where raw mode is enabled but
    // unguarded.
    let guard = TerminalGuard;
    execute!(std::io::stdout(), EnterAlternateScreen)?;
    let mut terminal = Terminal::new(CrosstermBackend::new(std::io::stdout()))?;
    let size = terminal.size()?;
    let mut app = App::new(
        title,
        doc.version(),
        doc.page_count(),
        (size.width, size.height),
    );
    let (tx, mut rx) = mpsc::unbounded_channel::<Msg>();
    let search_epoch = Arc::new(AtomicU64::new(0));
    let mut events = EventStream::new();
    let mut tick = tokio::time::interval(Duration::from_millis(100));
    loop {
        terminal.draw(|frame| ui::draw(&app, frame))?;
        let msg = tokio::select! {
            maybe_event = events.next() => match maybe_event {
                Some(Ok(Event::Key(key))) => Msg::Key(key),
                Some(Ok(Event::Resize(width, height))) => Msg::Resize(width, height),
                Some(Ok(..)) => continue,
                Some(Err(..)) | None => break,
            },
            Some(msg) = rx.recv() => msg,
            _ = tick.tick() => Msg::Tick,
        };
        for cmd in app.update(msg) {
            execute_cmd(&doc, &tx, &search_epoch, cmd);
        }
        if app.should_quit {
            break;
        }
    }
    drop(guard);
    Ok(())
}

/// Spawns the background task a [`Cmd`] describes; completions come back
/// to the loop as [`Msg`]s on the channel.
fn execute_cmd(
    doc: &AsyncDocument,
    tx: &UnboundedSender<Msg>,
    search_epoch: &Arc<AtomicU64>,
    cmd: Cmd,
) {
    let doc = doc.clone();
    let tx = tx.clone();
    match cmd {
        Cmd::LoadTree(req) => {
            tokio::spawn(load_tree(doc, tx, req));
        }
        Cmd::LoadContents { page, r } => {
            tokio::spawn(load_contents(doc, tx, page, r));
        }
        Cmd::LoadObject { generation, r } => {
            tokio::spawn(async move {
                let message = match doc.get_object(r).await {
                    Ok(object) => Msg::InspectorLoaded {
                        generation,
                        payload: InspectorPayload::Object { r, object },
                    },
                    Err(error) => Msg::InspectorFailed {
                        generation,
                        error: error.to_string(),
                    },
                };
                tx.send(message).ok();
            });
        }
        Cmd::DecodeStream { generation, r } => {
            tokio::spawn(async move {
                let message = match decoded_stream_data(&doc, r).await {
                    Ok(data) => Msg::InspectorLoaded {
                        generation,
                        payload: InspectorPayload::Decoded { r, data },
                    },
                    Err(error) => Msg::InspectorFailed { generation, error },
                };
                tx.send(message).ok();
            });
        }
        Cmd::LoadHex {
            generation,
            source,
            window_start,
        } => {
            tokio::spawn(load_hex(doc, tx, generation, source, window_start));
        }
        Cmd::StartSearch { generation, query } => {
            let epoch = Arc::clone(search_epoch);
            epoch.store(generation, Ordering::SeqCst);
            tokio::spawn(run_search(doc, tx, epoch, generation, query));
        }
        Cmd::CancelSearch { generation } => {
            search_epoch.store(generation, Ordering::SeqCst);
        }
        Cmd::RenderPreview {
            generation,
            page,
            max_w,
            max_h,
            file_bytes,
        } => {
            tokio::spawn(render_preview(
                doc, tx, generation, page, max_w, max_h, file_bytes,
            ));
        }
    }
}

/// Whether a completed tree-population pass should be reported as an
/// outright failure (`Msg::TreeFailed`) instead of a normal `done` batch:
/// the pass delivered *zero* elements in total and recorded at least one
/// parse error along the way (total salvage failure — nothing usable came
/// out of it). A pass that delivered any real elements keeps the existing
/// partial-salvage behavior, even when it also recorded errors.
fn pass_failed(total_elements: usize, total_errors: usize) -> bool {
    total_elements == 0 && total_errors > 0
}

/// Streams a tree section's elements in batches. Per-element parse errors
/// are counted, never fatal (salvage semantics: a document with an
/// unusable logical layer still explores physically).
async fn load_tree(doc: AsyncDocument, tx: UnboundedSender<Msg>, req: TreeReq) {
    let opts = match req {
        TreeReq::Physical => ElementOpts {
            physical: true,
            logical: false,
            pages: None,
            content_ops: false,
        },
        TreeReq::Logical => ElementOpts {
            physical: false,
            logical: true,
            pages: None,
            content_ops: false,
        },
        // Contents folders load through `load_contents`.
        TreeReq::Contents { .. } => return,
    };
    let mut stream = doc.elements(opts);
    let mut batch: Vec<Element> = Vec::new();
    let mut errors = 0usize;
    // Totals persist across mid-stream flushes (which reset `batch` and
    // `errors` below) so the end-of-pass decision sees the whole pass,
    // not just the last unflushed chunk.
    let mut total_elements = 0usize;
    let mut total_errors = 0usize;
    while let Some(item) = stream.next().await {
        match item {
            Ok(element) => {
                batch.push(element);
                total_elements += 1;
            }
            Err(..) => {
                errors += 1;
                total_errors += 1;
            }
        }
        if batch.len() >= TREE_BATCH {
            let elements = std::mem::take(&mut batch);
            let batch_errors = std::mem::take(&mut errors);
            let sent = tx.send(Msg::TreeBatch {
                req,
                elements,
                errors: batch_errors,
                done: false,
            });
            if sent.is_err() {
                return;
            }
        }
    }
    if pass_failed(total_elements, total_errors) {
        // Total salvage failure: nothing usable ever came out of this
        // pass. Emitting `TreeFailed` (instead of a `done: true` batch
        // with zero elements) marks the section Failed so a re-expand
        // retries the load, rather than looking permanently empty.
        let error = format!("{total_errors} element(s) failed to parse, nothing salvaged");
        tx.send(Msg::TreeFailed { req, error }).ok();
    } else {
        tx.send(Msg::TreeBatch {
            req,
            elements: batch,
            errors,
            done: true,
        })
        .ok();
    }
}

/// Fetches a page dict and reports its `/Contents` refs (a single ref or
/// an array of refs).
async fn load_contents(doc: AsyncDocument, tx: UnboundedSender<Msg>, page: usize, r: ObjRef) {
    let message = match page_contents(&doc, r).await {
        Ok(refs) => Msg::ContentsLoaded { page, refs },
        Err(error) => Msg::ContentsFailed { page, error },
    };
    tx.send(message).ok();
}

async fn page_contents(doc: &AsyncDocument, r: ObjRef) -> Result<Vec<ObjRef>, String> {
    let object = doc.get_object(r).await.map_err(|error| error.to_string())?;
    let Some(dict) = object.as_dict() else {
        return Err(format!("object {} {} is not a page dict", r.num, r.gen));
    };
    let mut refs = Vec::new();
    match dict.get("Contents") {
        Some(pdfboss_core::Object::Ref(content_ref)) => refs.push(*content_ref),
        Some(pdfboss_core::Object::Array(items)) => {
            for item in items {
                if let pdfboss_core::Object::Ref(content_ref) = item {
                    refs.push(*content_ref);
                }
            }
        }
        Some(..) | None => {}
    }
    Ok(refs)
}

/// Decoded data of stream object `r`.
async fn decoded_stream_data(doc: &AsyncDocument, r: ObjRef) -> Result<Vec<u8>, String> {
    let object = doc.get_object(r).await.map_err(|error| error.to_string())?;
    let Some(stream) = object.as_stream() else {
        return Err(format!("object {} {} is not a stream", r.num, r.gen));
    };
    doc.decode_stream(stream)
        .await
        .map_err(|error| error.to_string())
}

/// Loads one hex window: a `read_span` window of a file span, or the whole
/// decoded object-stream container (decoded buffers are small).
async fn load_hex(
    doc: AsyncDocument,
    tx: UnboundedSender<Msg>,
    generation: u64,
    source: HexSource,
    window_start: u64,
) {
    let outcome: Result<(u64, u64, Vec<u8>), String> = match source {
        HexSource::File { span } => {
            let total_len = span.end.saturating_sub(span.start);
            let start = span.start + window_start;
            let end = (start + WINDOW_BYTES as u64).min(span.end);
            match doc.read_span(Span { start, end }).await {
                Ok(bytes) => Ok((window_start, total_len, bytes)),
                Err(error) => Err(error.to_string()),
            }
        }
        HexSource::DecodedObjStm { container } => {
            match decoded_stream_data(&doc, container).await {
                Ok(bytes) => Ok((0, bytes.len() as u64, bytes)),
                Err(error) => Err(error),
            }
        }
    };
    let message = match outcome {
        Ok((start, total_len, bytes)) => Msg::HexLoaded {
            generation,
            window_start: start,
            total_len,
            bytes,
        },
        Err(error) => Msg::HexFailed { generation, error },
    };
    tx.send(message).ok();
}

/// Visits physical objects lazily, streaming one message per match. A
/// newer search generation (shared epoch) terminates this task early.
async fn run_search(
    doc: AsyncDocument,
    tx: UnboundedSender<Msg>,
    epoch: Arc<AtomicU64>,
    generation: u64,
    query: String,
) {
    let opts = ElementOpts {
        physical: true,
        logical: false,
        pages: None,
        content_ops: false,
    };
    let mut stream = doc.elements(opts);
    while let Some(item) = stream.next().await {
        if epoch.load(Ordering::SeqCst) != generation {
            return;
        }
        let Ok(Element::IndirectObject { r, object, .. }) = item else {
            continue;
        };
        if object_matches(&query, r.num, &object) {
            let hit = SearchHit { r };
            if tx.send(Msg::SearchResult { generation, hit }).is_err() {
                return;
            }
        }
    }
    tx.send(Msg::SearchDone { generation }).ok();
}

/// Renders a page preview. The whole file is fetched once (and cached by
/// the app for later renders); rasterization runs in `spawn_blocking`, and
/// the sync `Document` is created and dropped entirely inside the closure
/// (it is not `Send`).
async fn render_preview(
    doc: AsyncDocument,
    tx: UnboundedSender<Msg>,
    generation: u64,
    page: usize,
    max_w: u32,
    max_h: u32,
    file_bytes: Option<Arc<Vec<u8>>>,
) {
    let bytes = match file_bytes {
        Some(bytes) => Ok(bytes),
        None => fetch_whole_file(&doc).await.map(Arc::new),
    };
    let bytes = match bytes {
        Ok(bytes) => bytes,
        Err(error) => {
            tx.send(Msg::PreviewReady {
                generation,
                result: Err(error),
            })
            .ok();
            return;
        }
    };
    let render_input = Arc::clone(&bytes);
    // The render is lenient: content pdfboss cannot read is skipped, so the
    // preview can come out blank with no error at all. The report's summary
    // rides along and becomes a status-bar toast.
    let rendered = tokio::task::spawn_blocking(
        move || -> Result<(pdfboss_render::Pixmap, Option<String>), String> {
            let document = pdfboss_core::Document::load(render_input.as_ref().clone())
                .map_err(|error| error.to_string())?;
            let page_object = document.page(page).map_err(|error| error.to_string())?;
            let (page_w, page_h) = page_object.size();
            let scale = fit_scale(page_w, page_h, max_w, max_h);
            let options = pdfboss_render::RenderOptions::default();
            let (pixmap, report) =
                pdfboss_render::render_page_reporting(&document, &page_object, scale, &options)
                    .map_err(|error| error.to_string())?;
            Ok((pixmap, report.summary()))
        },
    )
    .await;
    let result = match rendered {
        Ok(Ok((pixmap, notice))) => Ok(PreviewFrame {
            file_bytes: bytes,
            pixmap,
            notice,
        }),
        Ok(Err(error)) => Err(error),
        Err(join_error) => Err(join_error.to_string()),
    };
    tx.send(Msg::PreviewReady { generation, result }).ok();
}

/// Fetches the entire file via one `read_span` over
/// `0..doc.file_len()` (the aio crate reports the length synchronously).
async fn fetch_whole_file(doc: &AsyncDocument) -> Result<Vec<u8>, String> {
    let end = doc.file_len();
    doc.read_span(Span { start: 0, end })
        .await
        .map_err(|error| error.to_string())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn pass_failed_true_only_when_zero_elements_and_some_errors() {
        assert!(
            pass_failed(0, 1),
            "zero elements with at least one error is total failure"
        );
        assert!(
            pass_failed(0, 5),
            "any positive error count still fails when no elements arrived"
        );
        assert!(!pass_failed(0, 0), "empty-but-clean pass is not a failure");
        assert!(
            !pass_failed(3, 2),
            "partial salvage: any real element wins over errors"
        );
        assert!(!pass_failed(3, 0), "clean pass with elements");
    }

    #[tokio::test]
    async fn fetch_whole_file_reads_exactly_file_len_bytes() {
        let data = pdfboss_testkit::simple_doc("Hello");
        let doc = AsyncDocument::from_bytes(data.clone())
            .await
            .expect("fixture opens");
        assert_eq!(doc.file_len(), data.len() as u64, "reported length");
        let fetched = fetch_whole_file(&doc).await.expect("whole-file fetch");
        assert_eq!(fetched, data, "fetch covers the entire file");
    }
}