pdfq 0.1.1

Fast, parallel PDF search with a pdfgrep-compatible CLI
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
//! Output: prefixes, match highlighting, context lines (from pdfgrep).

use crate::cli::{OutConf, PagenumType};

/// A byte span of a match inside the page text.
#[derive(Debug, Clone, Copy)]
pub struct MatchSpan {
    pub start: usize,
    pub end: usize,
}

#[derive(Clone, Copy)]
pub struct PrefixCtx<'a> {
    pub filename: &'a str,
    pub pagenum: u32,
    pub pagelabel: &'a str,
}

pub struct Writer {
    buf: String,
    out: OutConf,
}

impl Writer {
    pub fn new(out: OutConf) -> Self {
        Self {
            buf: String::new(),
            out,
        }
    }

    pub fn take(&mut self) -> String {
        std::mem::take(&mut self.buf)
    }

    fn color_on(&self, code: &str) -> String {
        if self.out.color && !code.is_empty() {
            code.to_string()
        } else {
            String::new()
        }
    }

    const COLOR_OFF: &'static str = "\x1b[m\x1b[K";

    fn color_off(&self) -> &'static str {
        if self.out.color {
            Self::COLOR_OFF
        } else {
            ""
        }
    }

    /// `filename<sep>[pagenum<sep>]`; sep is `:`, `-` (context), or NUL (-Z).
    pub fn prefix(&mut self, ctx: PrefixCtx<'_>, in_context: bool) {
        let sep_owned;
        let sep: &str = if in_context {
            "-"
        } else {
            sep_owned = self.out.prefix_sep.clone();
            &sep_owned
        };
        let color = self.out.color;
        let fn_c = if color {
            self.out.colors.filename.clone()
        } else {
            String::new()
        };
        let pn_c = if color {
            self.out.colors.pagenum.clone()
        } else {
            String::new()
        };
        let se_c = if color {
            self.out.colors.separator.clone()
        } else {
            String::new()
        };
        let off = if color { Self::COLOR_OFF } else { "" };

        if self.out.filename {
            if !fn_c.is_empty() {
                self.buf.push_str(&fn_c);
            }
            self.buf.push_str(ctx.filename);
            self.buf.push_str(off);

            if self.out.null_byte_sep {
                self.buf.push('\0');
            } else {
                if !se_c.is_empty() {
                    self.buf.push_str(&se_c);
                }
                self.buf.push_str(sep);
                self.buf.push_str(off);
            }
        }

        if self.out.pagenum {
            if !pn_c.is_empty() {
                self.buf.push_str(&pn_c);
            }
            match self.out.pagenum_type {
                PagenumType::Index => {
                    self.buf.push_str(&ctx.pagenum.to_string());
                }
                PagenumType::Label => {
                    self.buf.push_str(ctx.pagelabel);
                }
            }
            self.buf.push_str(off);

            if !se_c.is_empty() {
                self.buf.push_str(&se_c);
            }
            self.buf.push_str(sep);
            self.buf.push_str(off);
        }
    }

    /// Print the line containing `matches` (non-empty, sorted, same line).
    pub fn line_matches(&mut self, ctx: PrefixCtx<'_>, text: &str, matches: &[MatchSpan]) {
        let first = matches[0];
        let last = matches[matches.len() - 1];

        // Line start: byte after the previous newline (or 0).
        let a = text[..first.start]
            .rfind('\n')
            .map(|i| i + 1)
            .unwrap_or(0);
        // Line end: the newline terminating the last match's line.
        let b = text[last.end..]
            .find('\n')
            .map(|i| last.end + i)
            .unwrap_or(text.len());

        let hl = self.color_on(&self.out.colors.highlight.clone());
        let off = self.color_off();

        self.prefix(ctx, false);

        let mut prev = a;
        for m in matches {
            if prev <= m.start {
                self.buf.push_str(&text[prev..m.start]);
            }
            if !hl.is_empty() {
                self.buf.push_str(&hl);
            }
            self.buf.push_str(&text[m.start..m.end]);
            self.buf.push_str(off);
            prev = m.end;
        }
        self.buf.push_str(&text[prev..b]);
        self.buf.push('\n');
    }

    /// `-o`: only the matched bytes.
    pub fn only_match(&mut self, ctx: PrefixCtx<'_>, text: &str, m: MatchSpan) {
        let hl = self.color_on(&self.out.colors.highlight.clone());
        let off = self.color_off();
        self.prefix(ctx, false);
        if !hl.is_empty() {
            self.buf.push_str(&hl);
        }
        self.buf.push_str(&text[m.start..m.end]);
        self.buf.push_str(off);
        self.buf.push('\n');
    }

    /// `-l`/`-L`: filename plus NUL or newline, never suppressed by -h.
    pub fn only_filename(&mut self, filename: &str) {
        let fn_c = self.color_on(&self.out.colors.filename.clone());
        let off = self.color_off();
        if !fn_c.is_empty() {
            self.buf.push_str(&fn_c);
        }
        self.buf.push_str(filename);
        self.buf.push_str(off);
        if self.out.null_byte_sep {
            self.buf.push('\0');
        } else {
            self.buf.push('\n');
        }
    }

    pub fn context_before(&mut self, ctx: PrefixCtx<'_>, m: MatchSpan, text: &str) {
        if !self.out.context_mode {
            return;
        }
        self.ctx_before_lines(ctx, m, text, self.out.context_before);
    }

    pub fn context_after(&mut self, ctx: PrefixCtx<'_>, m: MatchSpan, text: &str) {
        if !self.out.context_mode {
            return;
        }
        self.ctx_after_lines(ctx, m, text, self.out.context_after);
    }

    fn ctx_before_lines(
        &mut self,
        ctx: PrefixCtx<'_>,
        m: MatchSpan,
        text: &str,
        mut lines: i32,
    ) {
        let Some(line_begin) = text[..m.start].rfind('\n') else {
            return; // match is on the first line of the page
        };

        let mut collected: Vec<&str> = Vec::new();
        let mut pos = line_begin;
        while lines > 0 {
            lines -= 1;
            if pos == 0 {
                collected.push("");
                break;
            }
            let newpos = text[..pos].rfind('\n');
            let start_pos = newpos.map(|i| i + 1).unwrap_or(0);
            collected.push(&text[start_pos..pos]);
            match newpos {
                Some(np) => pos = np,
                None => break,
            }
        }

        for l in collected.iter().rev() {
            self.prefix(ctx, true);
            self.buf.push_str(l);
            self.buf.push('\n');
        }
    }

    fn ctx_after_lines(&mut self, ctx: PrefixCtx<'_>, m: MatchSpan, text: &str, mut lines: i32) {
        let Some(line_end_rel) = text[m.end..].find('\n') else {
            return; // match is on the last line of the page
        };
        let mut pos = m.end + line_end_rel;

        while lines > 0 {
            lines -= 1;
            if pos == text.len() - 1 {
                break;
            }
            let newpos = text[pos + 1..].find('\n').map(|i| pos + 1 + i);
            let end_pos = newpos.unwrap_or(text.len());
            self.prefix(ctx, true);
            self.buf.push_str(&text[pos + 1..end_pos]);
            self.buf.push('\n');
            match newpos {
                Some(np) => pos = np,
                None => break,
            }
        }
    }

    /// Between groups: after of m1, `--` only if lines remain, then before of m2.
    pub fn context_between(
        &mut self,
        ctx: PrefixCtx<'_>,
        m1: MatchSpan,
        m2: MatchSpan,
        text: &str,
    ) {
        if !self.out.context_mode {
            return;
        }

        let mut pos_right: Option<usize> = text[m1.end..].find('\n').map(|i| m1.end + i);
        let mut pos_left: Option<usize> = text[..m2.start].rfind('\n');

        // Count lines after m1 that fall strictly before m2's line.
        let mut lines_right = 0i32;
        while let Some(pr) = pos_right {
            if lines_right >= self.out.context_after {
                break;
            }
            match pos_left {
                Some(pl) if pr < pl => {}
                _ => break,
            }
            lines_right += 1;
            pos_right = text[pr + 1..].find('\n').map(|i| pr + 1 + i);
        }

        // Count lines before m2 that fall strictly after m1's line.
        let mut lines_left = 0i32;
        while let Some(pl) = pos_left {
            if lines_left >= self.out.context_before {
                break;
            }
            match pos_right {
                Some(pr) if pl > pr => {}
                _ => break,
            }
            lines_left += 1;
            if pl == 0 {
                pos_left = None;
                break;
            }
            pos_left = text[..pl].rfind('\n');
        }

        self.ctx_after_lines(ctx, m1, text, lines_right);
        // Mutated positions; None on the left still separates (npos).
        let sep = match (pos_left, pos_right) {
            (Some(pl), Some(pr)) => pl > pr,
            (None, Some(_)) => true,
            _ => false,
        };
        if sep {
            self.context_separator();
        }
        self.ctx_before_lines(ctx, m2, text, lines_left);
    }

    pub fn context_separator(&mut self) {
        if self.out.context_mode {
            self.buf.push_str("--\n");
        }
    }

    /// Raw push used for count/page-count lines.
    pub fn raw_line(&mut self, s: &str) {
        self.buf.push_str(s);
        self.buf.push('\n');
    }
}

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

    fn conf() -> OutConf {
        OutConf {
            filename: false,
            pagenum: false,
            pagenum_type: PagenumType::Index,
            color: false,
            only_matching: false,
            null_byte_sep: false,
            prefix_sep: ":".into(),
            context_mode: false,
            context_before: 0,
            context_after: 0,
            colors: ColorScheme::default(),
        }
    }

    fn ctx<'a>() -> PrefixCtx<'a> {
        PrefixCtx {
            filename: "f.pdf",
            pagenum: 1,
            pagelabel: "i",
        }
    }

    #[test]
    fn plain_line_with_match() {
        let mut w = Writer::new(conf());
        let text = "hello world";
        w.line_matches(ctx(), text, &[MatchSpan { start: 6, end: 11 }]);
        // The whole line is printed, only the match is highlighted.
        assert_eq!(w.take(), "hello world\n");
    }

    #[test]
    fn page_number_prefix() {
        let mut c = conf();
        c.pagenum = true;
        let mut w = Writer::new(c);
        w.line_matches(ctx(), "abc", &[MatchSpan { start: 0, end: 3 }]);
        assert_eq!(w.take(), "1:abc\n");
    }

    #[test]
    fn filename_and_page_prefix() {
        let mut c = conf();
        c.filename = true;
        c.pagenum = true;
        let mut w = Writer::new(c);
        w.line_matches(ctx(), "abc", &[MatchSpan { start: 0, end: 3 }]);
        assert_eq!(w.take(), "f.pdf:1:abc\n");
    }

    #[test]
    fn null_separator_after_filename_only() {
        let mut c = conf();
        c.filename = true;
        c.pagenum = true;
        c.null_byte_sep = true;
        let mut w = Writer::new(c);
        w.line_matches(ctx(), "abc", &[MatchSpan { start: 0, end: 3 }]);
        assert_eq!(w.take(), "f.pdf\u{0}1:abc\n");
    }

    #[test]
    fn context_uses_dash_separator() {
        let mut c = conf();
        c.filename = true;
        c.pagenum = true;
        c.context_mode = true;
        c.context_before = 1;
        c.context_after = 1;
        let mut w = Writer::new(c);
        let text = "one\ntwo\nthree";
        // match on "two"
        w.prefix(ctx(), true);
        w.raw_line("one");
        w.line_matches(ctx(), text, &[MatchSpan { start: 4, end: 7 }]);
        w.prefix(ctx(), true);
        w.raw_line("three");
        // Context lines join every part with "-"; only match lines use ":".
        assert_eq!(w.take(), "f.pdf-1-one\nf.pdf:1:two\nf.pdf-1-three\n");
    }

    #[test]
    fn multiple_matches_same_line() {
        let mut w = Writer::new(conf());
        let text = "foofoo";
        w.line_matches(
            ctx(),
            text,
            &[MatchSpan { start: 0, end: 3 }, MatchSpan { start: 3, end: 6 }],
        );
        assert_eq!(w.take(), "foofoo\n");
    }
}