bigtext 0.7.2

Fast text editor for huge files (1-50+ GB): opens instantly, never freezes, streaming search/replace and atomic save
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
//! Filter view: the lines that match the search query (or, inverted, the lines that do not) in
//! a virtualized list under the text, with export to a file and copy to the clipboard.

use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

use egui::text::{LayoutJob, TextFormat};
use egui::FontId;

use bigtext::filter::{self, FilterSpec, LineKind};
use bigtext::jobs::{self, human_bytes, CancelToken, Progress, Throttle};
use bigtext::jsonq::FieldQuery;
use bigtext::loglevel::{self, Level, Levels};
use bigtext::search::{Matcher, Query, SearchEnd};
use bigtext::text::TextBuf;

use super::follow_id::FollowSummary;
use super::time_range::{After, Window};
use super::i18n::{tr, trf};
use super::tasks::{self, TaskOutcome};
use super::{same_path, BigTextApp, Msg};

/// Lines kept in the list; an export always writes all of them.
pub const FILTER_CAP: usize = 5_000_000;
/// "Copy lines" refuses to put more than this on the clipboard.
pub const COPY_MAX: u64 = 100 * 1024 * 1024;
/// Bytes of a line shown in the list.
const SHOW_MAX: u64 = 4096;

/// A listed line is context, not a match ([`FilterData::kinds`]).
const KIND_CONTEXT: u8 = 1;
/// Lines were left out before a listed line.
const KIND_GAP: u8 = 2;

#[derive(Default)]
pub struct FilterData {
    /// Start offsets of the kept lines.
    pub lines: Vec<u64>,
    /// With context: KIND_* bits of each line (empty without context).
    pub kinds: Vec<u8>,
    /// Lines with a match (without the context lines).
    pub hits: usize,
    /// When following an id: its first and last time and errors.
    pub summary: FollowSummary,
    pub capped: bool,
    pub done: bool,
    pub end: Option<SearchEnd>,
    pub elapsed: Option<Duration>,
}

impl FilterData {
    /// Whether listed line `i` is context around a match.
    pub fn is_context(&self, i: usize) -> bool {
        self.kinds.get(i).is_some_and(|k| k & KIND_CONTEXT != 0)
    }
}

/// A condition added on top of a filter (the Narrow row): lines with (or, `negated`, without) a
/// match of `query`.
#[derive(Clone)]
pub struct FilterTerm {
    pub query: Query,
    pub negated: bool,
    pub matcher: Arc<Matcher>,
}

pub(super) fn term_refs(terms: &[FilterTerm]) -> Vec<filter::Term<'_>> {
    terms.iter().map(|t| filter::Term { m: &t.matcher, negated: t.negated }).collect()
}

/// Whether a line's bytes (UTF-8) meet every added condition.
pub(super) fn line_meets(terms: &[filter::Term], line: &[u8]) -> bool {
    terms.iter().all(|t| {
        let mut found = false;
        t.m.find_in(line, 0, 0, line.len(), line.len(), &mut None, &mut |_, _| {
            found = true;
            false
        });
        found != t.negated
    })
}

pub struct FilterResults {
    pub query: Query,
    /// Set when the lines were picked by log level (the level bar), not by the search query.
    pub levels: Option<Levels>,
    /// Set when following an id (Search → Follow id at caret).
    pub follow: Option<String>,
    pub spec: FilterSpec,
    pub matcher: Arc<Matcher>,
    /// Conditions added with the Narrow row, all of which a kept line meets.
    pub terms: Vec<FilterTerm>,
    /// Set for a field query over JSON Lines (the search bar's {} switch).
    pub json: Option<Arc<FieldQuery>>,
    pub version: u64,
    pub data: Arc<Mutex<FilterData>>,
    pub cancel: CancelToken,
    pub progress: Progress,
}

impl FilterResults {
    pub fn is_running(&self) -> bool {
        !self.data.lock().unwrap().done
    }
}

/// What to filter by.
pub(crate) struct FilterRequest {
    pub query: Query,
    pub m: Arc<Matcher>,
    pub spec: FilterSpec,
    pub levels: Option<Levels>,
    pub follow: Option<String>,
    pub terms: Vec<FilterTerm>,
    /// A field query over JSON Lines instead of a match of `m` (`m` only highlights).
    pub json: Option<Arc<FieldQuery>>,
}

impl BigTextApp {
    /// Starts filtering the active document by the search query.
    pub(crate) fn start_filter(&mut self) {
        let Some(d) = self.docs.get(self.active) else { return };
        if !d.is_ready() {
            return;
        }
        if self.search.query.pattern.is_empty() {
            self.open_search(false);
            return;
        }
        let pattern = self.search.query.pattern.clone();
        if self.search.fields || (d.effective_lang() == crate::view::highlight::Lang::Json && bigtext::jsonq::looks_like_query(&pattern)) {
            self.start_field_filter(&pattern);
            return;
        }
        let m = match self.search.matcher(d.buf.codec) {
            Ok(m) => m,
            Err(e) => {
                self.search.status = e;
                return;
            }
        };
        let column = self.search.filter_column.and_then(|c| Some((d.table_state.dialect?, c))).filter(|_| d.table);
        let range = match self.time_window(After::Filter) {
            Window::Wait => return,
            Window::Bytes(a, b) => Some((a, b)),
            Window::All => None,
        };
        let spec = self.with_context(FilterSpec { invert: self.search.invert, column, range, ..Default::default() });
        self.search.remember();
        self.run_filter(self.search.query.clone(), m, spec, None);
    }

    /// Filters JSON Lines by a field query (`.level == "error" and .duration > 500`).
    pub(crate) fn start_field_filter(&mut self, text: &str) {
        let Some(d) = self.docs.get(self.active) else { return };
        let q = match FieldQuery::parse(text) {
            Ok(q) => Arc::new(q),
            Err(e) => {
                self.search.status = e;
                self.open_search(false);
                return;
            }
        };
        if d.buf.codec.unit() != 1 {
            self.search.status = tr("Field queries need a UTF-8 file").into();
            return;
        }
        let hl = Query { pattern: q.highlight(), regex: false, case_sensitive: true, whole_word: false, dot_all: false };
        let Ok(m) = Matcher::new(&hl, d.buf.codec) else { return };
        let range = match self.time_window(After::Filter) {
            Window::Wait => return,
            Window::Bytes(a, b) => Some((a, b)),
            Window::All => None,
        };
        let spec = self.with_context(FilterSpec { invert: self.search.invert, range, ..Default::default() });
        self.search.remember();
        let query = Query { pattern: text.to_string(), ..self.search.query.clone() };
        self.run_filter_req(FilterRequest { query, m: Arc::new(m), spec, levels: None, follow: None, terms: Vec::new(), json: Some(q) }, None);
    }

    /// `spec` with the context lines and whole entries chosen in the filter panel.
    pub(crate) fn with_context(&self, spec: FilterSpec) -> FilterSpec {
        let n = self.settings.filter_context.min(filter::CONTEXT_MAX);
        FilterSpec { before: n, after: n, entries: self.settings.filter_entries, ..spec }
    }

    /// Runs the active document's filter again (after an edit or a change of its options), with
    /// its added conditions.
    pub(crate) fn rerun_filter(&mut self) {
        self.refilter(None);
    }

    /// Runs the active document's filter again with `terms` (or its own) as added conditions.
    fn refilter(&mut self, terms: Option<Vec<FilterTerm>>) {
        if self.docs.get(self.active).is_none_or(|d| d.filter.is_none()) {
            return;
        }
        let range = match self.time_window(After::Refilter) {
            Window::Wait => return,
            Window::Bytes(a, b) => Some((a, b)),
            Window::All => None,
        };
        let Some(d) = self.docs.get(self.active) else { return };
        let Some(r) = d.filter.as_ref() else { return };
        let codec = d.buf.codec;
        let spec = match r.follow {
            Some(_) => FilterSpec { entries: true, ..Default::default() },
            None => self.with_context(FilterSpec { invert: r.spec.invert, column: r.spec.column, range, ..Default::default() }),
        };
        // A field query's matcher only highlights.
        let q = match &r.json {
            Some(j) => Query { pattern: j.highlight(), regex: false, case_sensitive: true, whole_word: false, dot_all: false },
            None => r.query.clone(),
        };
        let m = match Matcher::new(&q, codec) {
            Ok(m) => Arc::new(m),
            Err(e) => {
                self.error = Some(e);
                return;
            }
        };
        let terms = terms.unwrap_or_else(|| r.terms.clone());
        let terms = terms.into_iter().filter_map(|t| Matcher::new(&t.query, codec).ok().map(|m| FilterTerm { matcher: Arc::new(m), ..t })).collect();
        let req = FilterRequest { query: r.query.clone(), m, spec, levels: r.levels, follow: r.follow.clone(), terms, json: r.json.clone() };
        self.run_filter_req(req, None);
    }

    /// Adds a condition to the active document's filter: lines with `pattern` (with the search
    /// bar's options), or without it. When the filter is complete, only its lines are read.
    pub(crate) fn narrow_filter(&mut self, pattern: &str, negated: bool) {
        let Some(d) = self.docs.get(self.active) else { return };
        let Some(r) = d.filter.as_ref() else { return };
        if pattern.is_empty() {
            return;
        }
        let query = Query { pattern: pattern.to_string(), dot_all: false, ..self.search.query.clone() };
        let matcher = match Matcher::new(&query, d.buf.codec) {
            Ok(m) => Arc::new(m),
            Err(e) => {
                self.notify(e, None);
                return;
            }
        };
        let mut terms = r.terms.clone();
        terms.push(FilterTerm { query, negated, matcher });
        // The lines of a complete, current filter (its matches, not their context).
        let within = {
            let g = r.data.lock().unwrap();
            (g.done && !g.capped && g.end == Some(SearchEnd::Completed) && r.version == d.version)
                .then(|| g.lines.iter().enumerate().filter(|&(i, _)| !g.is_context(i)).map(|(_, &s)| s).collect::<Vec<u64>>())
        };
        let req = FilterRequest { query: r.query.clone(), m: r.matcher.clone(), spec: r.spec, levels: r.levels, follow: r.follow.clone(), terms, json: r.json.clone() };
        self.run_filter_req(req, within);
    }

    /// Drops the added conditions from the `from`-th on and runs the filter again.
    pub(crate) fn unnarrow_filter(&mut self, from: usize) {
        let Some(r) = self.docs.get(self.active).and_then(|d| d.filter.as_ref()) else { return };
        let terms = r.terms[..from.min(r.terms.len())].to_vec();
        self.refilter(Some(terms));
    }

    /// Drops the `i`-th added condition and runs the filter again.
    pub(crate) fn remove_filter_term(&mut self, i: usize) {
        let Some(r) = self.docs.get(self.active).and_then(|d| d.filter.as_ref()) else { return };
        let mut terms = r.terms.clone();
        if i < terms.len() {
            terms.remove(i);
        }
        self.refilter(Some(terms));
    }

    /// Search → Narrow the filter: adds the search box's pattern (with the ! switch) to the active
    /// filter, or starts a filter when there is none.
    pub(crate) fn filter_within(&mut self) {
        let has = self.docs.get(self.active).is_some_and(|d| d.filter.is_some());
        if !has || self.search.query.pattern.is_empty() {
            self.open_search(false);
            self.start_filter();
            return;
        }
        let (p, invert) = (self.search.query.pattern.clone(), self.search.invert);
        self.search.remember();
        self.narrow_filter(&p, invert);
    }

    /// Filters the active document with `m` in the background, replacing its previous filter.
    pub(crate) fn run_filter(&mut self, query: Query, m: Arc<Matcher>, spec: FilterSpec, levels: Option<Levels>) {
        self.run_filter_req(FilterRequest { query, m, spec, levels, follow: None, terms: Vec::new(), json: None }, None);
    }

    /// Filters the active document in the background, replacing its previous filter. With
    /// `follow`, the lines are an id's, and the job also notes their first and last time and how
    /// many are errors. With `within`, only those lines (an earlier result) are read.
    pub(crate) fn run_filter_req(&mut self, req: FilterRequest, within: Option<Vec<u64>>) {
        let Some(d) = self.docs.get_mut(self.active) else { return };
        if let Some(r) = d.filter.take() {
            r.cancel.cancel();
        }
        let FilterRequest { query, m, spec, levels, follow, terms, json } = req;
        let data = Arc::new(Mutex::new(FilterData::default()));
        let cancel = CancelToken::new();
        let progress = Progress::new(d.buf.len());
        let errors = follow.as_ref().and_then(|_| loglevel::query(Levels::only(Level::Error))).and_then(|q| Matcher::new(&q, d.buf.codec).ok());
        d.filter = Some(FilterResults {
            query,
            levels,
            follow,
            spec,
            matcher: m.clone(),
            terms: terms.clone(),
            json: json.clone(),
            version: d.version,
            data: data.clone(),
            cancel: cancel.clone(),
            progress: progress.clone(),
        });
        self.search.show_filter = true;
        let snap = d.buf.snapshot();
        let waker = self.waker.clone();
        jobs::spawn("filter", move || {
            let started = Instant::now();
            let mut batch: Vec<u64> = Vec::with_capacity(4096);
            let mut kinds: Vec<u8> = Vec::new();
            let with_kinds = spec.has_context();
            let mut prev_end = None;
            let (mut count, mut hits) = (0usize, 0usize);
            let mut summary = FollowSummary::default();
            let mut capped = false;
            let mut throttle = Throttle::new(Duration::from_millis(50));
            let refs = term_refs(&terms);
            let on_line = |ls: u64, le: u64, kind: LineKind| {
                if count >= FILTER_CAP {
                    capped = true;
                    return false;
                }
                if let Some(em) = &errors {
                    if let Some(st) = bigtext::timestamp::stamp_at(&snap, ls) {
                        summary.first.get_or_insert(st);
                        summary.last = Some(st);
                    }
                    if !bigtext::search::matches_in_view(&snap, em, ls..le.min(ls + 64 * 1024), 1).is_empty() {
                        summary.errors += 1;
                    }
                }
                batch.push(ls);
                if with_kinds {
                    let gap = if prev_end.is_some_and(|p| ls > p) { KIND_GAP } else { 0 };
                    kinds.push(gap | if kind == LineKind::Context { KIND_CONTEXT } else { 0 });
                    prev_end = Some(le);
                }
                count += 1;
                hits += usize::from(kind == LineKind::Hit);
                if batch.len() >= 4096 || throttle.ready() {
                    let mut g = data.lock().unwrap();
                    g.lines.append(&mut batch);
                    g.kinds.append(&mut kinds);
                    g.hits = hits;
                    g.summary = summary;
                    drop(g);
                    waker();
                }
                true
            };
            let end = match (&within, &json) {
                (Some(starts), _) => filter::filter_given(&snap, starts, spec, &refs, &cancel, Some(&progress), on_line),
                (None, Some(q)) => filter::filter_predicate(&snap, spec, &|l| q.matches(l) && line_meets(&refs, l), &cancel, Some(&progress), on_line),
                (None, None) => filter::filter_lines_with(&snap, &m, spec, &refs, &cancel, Some(&progress), on_line),
            };
            let mut g = data.lock().unwrap();
            g.lines.append(&mut batch);
            g.kinds.append(&mut kinds);
            g.hits = hits;
            g.summary = summary;
            g.capped = capped;
            g.done = true;
            g.end = Some(end);
            g.elapsed = Some(started.elapsed());
            drop(g);
            waker();
        });
    }

    /// Writes every kept line (not only the listed ones) to a file the user picks.
    pub(crate) fn export_filter(&mut self) {
        let Some(d) = self.docs.get(self.active) else { return };
        let Some(r) = &d.filter else { return };
        if r.version != d.version {
            self.error = Some(tr("The document changed since the filter ran. Run the filter again first.").into());
            return;
        }
        let (snap, m, spec, terms, name) = (d.buf.snapshot(), r.matcher.clone(), r.spec, r.terms.clone(), d.title.clone());
        if let Some(q) = r.json.clone() {
            let Some(target) = self.pick_output_file(tr("Save lines"), &format!("{name}.lines.txt")) else { return };
            self.spawn_task(tr("Saving lines").into(), move |progress, cancel| {
                progress.set_total(snap.len());
                tasks::write_via_temp(&target, |w| {
                    Ok(filter::write_pred_lines_to(&snap, spec, &|l| q.matches(l) && line_meets(&term_refs(&terms), l), w, cancel, Some(progress))?
                        .map(|n| trf("Saved {lines} lines ({size}) to {file}.", &[("lines", &fmt_count(n.lines)), ("size", &human_bytes(n.bytes)), ("file", &file_name(&target))])))
                })
            });
            return;
        }
        self.save_lines_dialog(snap, m, spec, terms, &name);
    }

    /// Asks for a file and writes the lines that `m` and `spec` keep to it.
    pub(crate) fn save_lines_dialog(&mut self, snap: TextBuf, m: Arc<Matcher>, spec: FilterSpec, terms: Vec<FilterTerm>, doc_title: &str) {
        let Some(target) = self.pick_output_file(tr("Save lines"), &format!("{doc_title}.lines.txt")) else { return };
        self.spawn_task(tr("Saving lines").into(), move |progress, cancel| export_lines(&snap, &m, spec, &terms, &target, progress, cancel));
    }

    /// Save dialog for a new output file next to the active document; refuses files open in
    /// BigText (they are memory-mapped and cannot be replaced).
    pub(crate) fn pick_output_file(&mut self, title: &str, name: &str) -> Option<std::path::PathBuf> {
        let mut dlg = rfd::FileDialog::new().set_title(title).set_file_name(name);
        if let Some(dir) = self.docs.get(self.active).and_then(|d| d.path.as_ref()).and_then(|p| p.parent()) {
            dlg = dlg.set_directory(bigtext::file::display_path(dir));
        }
        let target = dlg.save_file()?;
        if self.docs.iter().any(|d| d.path.as_deref().is_some_and(|p| same_path(p, &target))) {
            self.error = Some(tr("That file is open in BigText. Choose another file.").into());
            return None;
        }
        Some(target)
    }

    /// Copies the kept lines to the clipboard (up to [`COPY_MAX`]).
    pub(crate) fn copy_filter(&mut self) {
        let Some(d) = self.docs.get(self.active) else { return };
        let Some(r) = &d.filter else { return };
        let (snap, m, spec, terms, id) = (d.buf.snapshot(), r.matcher.clone(), r.spec, r.terms.clone(), d.id);
        if let Some(q) = r.json.clone() {
            let (tx, waker) = (self.tx.clone(), self.waker.clone());
            jobs::spawn("copy-lines", move || {
                let mut out = Vec::new();
                let r = filter::write_pred_lines_to(&snap, spec, &|l| q.matches(l) && line_meets(&term_refs(&terms), l), &mut CappedVec(&mut out, COPY_MAX), &CancelToken::new(), None);
                let result = match r {
                    Ok(_) => Ok(snap.codec.decode_lossy(&out)),
                    Err(e) => Err(e.to_string()),
                };
                let _ = tx.send(Msg::Copied { doc: id, result, cut: false });
                waker();
            });
            return;
        }
        self.copy_lines(snap, m, spec, terms, id);
    }

    /// Copies the lines that `m` and `spec` keep to the clipboard (up to [`COPY_MAX`]).
    pub(crate) fn copy_lines(&mut self, snap: TextBuf, m: Arc<Matcher>, spec: FilterSpec, terms: Vec<FilterTerm>, id: u64) {
        let tx = self.tx.clone();
        let waker = self.waker.clone();
        jobs::spawn("copy-lines", move || {
            let mut out = Vec::new();
            let r = filter::write_lines_to(&snap, &m, spec, &term_refs(&terms), &mut CappedVec(&mut out, COPY_MAX), &CancelToken::new(), None);
            let result = match r {
                Ok(_) => Ok(snap.codec.decode_lossy(&out)),
                Err(e) if e.kind() == std::io::ErrorKind::OutOfMemory => {
                    Err(trf("The lines are larger than {size}. Use “Save lines…” instead.", &[("size", &human_bytes(COPY_MAX))]))
                }
                Err(e) => Err(e.to_string()),
            };
            let _ = tx.send(Msg::Copied { doc: id, result, cut: false });
            waker();
        });
    }

    pub(super) fn filter_panel(&mut self, ui: &mut egui::Ui) {
        let font = FontId::monospace(self.settings.font_size.min(14.0));
        let mut close = false;
        let mut export = false;
        let mut copy = false;
        let mut rerun = false;
        let mut jump = None;
        let (mut context, mut entries) = (self.settings.filter_context, self.settings.filter_entries);
        let mut mark = None;
        let (mut narrow_text, mut narrow_exclude) = (std::mem::take(&mut self.search.narrow), self.search.narrow_exclude);
        let mut narrow = false;
        let range_label = self.range_label();
        let edit_block = self.lines_edit_blocked();
        let mut remove_lines: Option<bool> = None;
        // Some(None): drop all added conditions; Some(Some(i)): drop the i-th.
        let mut drop_terms: Option<Option<usize>> = None;
        {
            let Some(d) = self.docs.get_mut(self.active) else { return };
            let Some(r) = &d.filter else { return };
            let (n, hits, capped, done, elapsed, end, summary) = {
                let g = r.data.lock().unwrap();
                (g.lines.len(), g.hits, g.capped, g.done, g.elapsed, g.end, g.summary)
            };
            let stale = r.version != d.version;
            ui.horizontal(|ui| {
                match (&r.follow, r.levels) {
                    (Some(id), _) => ui.strong(trf("{n} lines with id “{id}”", &[("n", &fmt_count(hits as u64)), ("id", id)])),
                    (None, Some(lv)) => ui.strong(trf("{n} lines of level {levels}", &[("n", &fmt_count(hits as u64)), ("levels", &lv.names())])),
                    (None, None) if r.json.is_some() => {
                        let what = if r.spec.invert { tr("lines not where") } else { tr("lines where") };
                        ui.strong(format!("{} {what} {}", fmt_count(hits as u64), r.query.pattern))
                    }
                    (None, None) => {
                        let what = if r.spec.invert { tr("lines without") } else { tr("lines with") };
                        ui.strong(format!("{} {what} “{}”", fmt_count(hits as u64), r.query.pattern))
                    }
                };
                if n > hits {
                    ui.label(trf("(+{n} around them)", &[("n", &fmt_count((n - hits) as u64))]));
                }
                if let Some((_, c)) = r.spec.column {
                    ui.label(trf("in column {n}", &[("n", &(c + 1).to_string())]));
                }
                if let (Some(_), Some(l)) = (r.spec.range, &range_label) {
                    ui.label(l);
                }
                if r.follow.is_some() {
                    ui.label(summary.describe());
                    if ui.small_button(tr("Highlight")).on_hover_text(tr("Highlight this id everywhere (View → Highlights)")).clicked() {
                        mark = Some(r.query.clone());
                    }
                } else if !r.spec.invert {
                    ui.separator();
                    ui.add(egui::DragValue::new(&mut context).range(0..=filter::CONTEXT_MAX).prefix("± "))
                        .on_hover_text(tr("Lines shown before and after each line with a match"));
                    ui.checkbox(&mut entries, tr("Whole entry")).on_hover_text(tr("Keep each log entry together: the stack trace under a line, or the entry's first line above it"));
                }
                if capped {
                    ui.label(trf("(list capped at {n}; Save lines writes all)", &[("n", &fmt_count(FILTER_CAP as u64))]));
                }
                if !done {
                    let f = r.progress.fraction();
                    ui.add(egui::ProgressBar::new(f).desired_width(160.0).text(format!("{} {:.0}%", tr("filtering"), f * 100.0)));
                    ui.ctx().request_repaint_after(Duration::from_millis(50));
                    if ui.small_button(tr("Cancel")).clicked() {
                        r.cancel.cancel();
                    }
                } else if let Some(t) = elapsed {
                    let note = if end == Some(SearchEnd::Cancelled) { tr(" (cancelled)") } else { "" };
                    ui.label(format!("{} {:.2}s{note}", tr("in"), t.as_secs_f64()));
                }
                if stale {
                    ui.colored_label(super::theme::warn_text(ui), tr("stale (document changed)"));
                    if ui.small_button(tr("Run again")).clicked() {
                        rerun = true;
                    }
                }
                ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
                    if ui.small_button("×").clicked() {
                        close = true;
                    }
                    if ui.add_enabled(done && !stale, egui::Button::new(tr("Save lines…")).small()).clicked() {
                        export = true;
                    }
                    if ui.add_enabled(done && !stale && n > 0, egui::Button::new(tr("Copy lines")).small()).clicked() {
                        copy = true;
                    }
                    let can = done && edit_block.is_none();
                    let hint = edit_block.unwrap_or("");
                    if ui.add_enabled(can && n > 0, egui::Button::new(tr("Keep only these")).small()).on_hover_text(tr("Delete every other line from the document (one undo step up to 50,000 runs of lines)")).on_disabled_hover_text(hint).clicked() {
                        remove_lines = Some(true);
                    }
                    if ui.add_enabled(can && n > 0, egui::Button::new(tr("Delete these")).small()).on_hover_text(tr("Delete these lines from the document (one undo step up to 50,000 runs of lines)")).on_disabled_hover_text(hint).clicked() {
                        remove_lines = Some(false);
                    }
                });
            });
            // The added conditions as chips, and the Narrow row.
            ui.horizontal(|ui| {
                if !r.terms.is_empty() {
                    let base = match (&r.follow, r.levels) {
                        (Some(id), _) => format!("id {id}"),
                        (None, Some(lv)) => lv.names(),
                        (None, None) => format!("{}“{}”", if r.spec.invert { "¬ " } else { "" }, r.query.pattern),
                    };
                    if ui.small_button(base).on_hover_text(tr("Back to this filter alone")).clicked() {
                        drop_terms = Some(None);
                    }
                    for (i, t) in r.terms.iter().enumerate() {
                        ui.label("›");
                        let text = format!("{}“{}” ×", if t.negated { "¬ " } else { "" }, t.query.pattern);
                        if ui.small_button(text).on_hover_text(tr("Remove this condition")).clicked() {
                            drop_terms = Some(Some(i));
                        }
                    }
                    ui.separator();
                }
                ui.label(tr("Narrow:"));
                let resp = ui.add(egui::TextEdit::singleline(&mut narrow_text).desired_width(180.0).hint_text(tr("and also…")));
                ui.checkbox(&mut narrow_exclude, tr("without")).on_hover_text(tr("Keep the lines that do not have it"));
                let enter = resp.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter));
                if (ui.add_enabled(!narrow_text.is_empty(), egui::Button::new(tr("Add")).small()).clicked() || enter) && !narrow_text.is_empty() {
                    narrow = true;
                }
            });
            let row_h = ui.ctx().fonts_mut(|f| f.row_height(&font)) + 2.0;
            let data = r.data.clone();
            let m = r.matcher.clone();
            let buf = &d.buf;
            let caret_line = d.line_bounds(d.view.caret, 64 * 1024).map(|b| b.0);
            egui::ScrollArea::both().auto_shrink([false, false]).show_rows(ui, row_h, n, |ui, range| {
                ui.style_mut().wrap_mode = Some(egui::TextWrapMode::Extend);
                let (lines, kinds): (Vec<u64>, Vec<u8>) = {
                    let g = data.lock().unwrap();
                    let (a, b) = (range.start.min(g.lines.len()), range.end.min(g.lines.len()));
                    (g.lines[a..b].to_vec(), (a..b).map(|i| g.kinds.get(i).copied().unwrap_or(0)).collect())
                };
                for (i, (ls, k)) in lines.into_iter().zip(kinds).enumerate() {
                    let job = line_job(buf, ls, Some(&m), &font, ui.visuals(), stale, k & KIND_CONTEXT != 0);
                    let resp = ui.selectable_label(caret_line == Some(ls) && !stale, job);
                    if k & KIND_GAP != 0 && range.start + i > 0 {
                        let y = resp.rect.top() - 1.0;
                        ui.painter().hline(resp.rect.x_range(), y, egui::Stroke::new(1.0, ui.visuals().weak_text_color()));
                    }
                    if resp.clicked() {
                        jump = Some(ls);
                    }
                }
            });
            if let (Some(ls), false) = (jump, stale) {
                d.set_caret(ls.max(d.min_caret()), false);
                d.view.center();
            }
            if close {
                if let Some(r) = d.filter.take() {
                    r.cancel.cancel();
                }
            }
        }
        if close {
            self.search.show_filter = false;
        }
        if (context, entries) != (self.settings.filter_context, self.settings.filter_entries) {
            self.settings.filter_context = context;
            self.settings.filter_entries = entries;
            self.persist_state();
            rerun = true;
        }
        if rerun {
            self.rerun_filter();
        }
        if let Some(keep) = remove_lines {
            self.remove_filtered_lines(keep);
        }
        if let Some(q) = mark {
            self.add_mark(q);
        }
        self.search.narrow_exclude = narrow_exclude;
        if narrow {
            self.narrow_filter(&narrow_text, narrow_exclude);
        } else {
            self.search.narrow = narrow_text;
        }
        match drop_terms {
            Some(None) => self.unnarrow_filter(0),
            Some(Some(i)) => self.remove_filter_term(i),
            None => {}
        }
        if export {
            self.export_filter();
        }
        if copy {
            self.copy_filter();
        }
    }
}

pub(super) fn export_lines(snap: &TextBuf, m: &Matcher, spec: FilterSpec, terms: &[FilterTerm], target: &std::path::Path, progress: &Progress, cancel: &CancelToken) -> TaskOutcome {
    progress.set_total(snap.len());
    tasks::write_via_temp(target, |w| {
        Ok(filter::write_lines_to(snap, m, spec, &term_refs(terms), w, cancel, Some(progress))?
            .map(|n| trf("Saved {lines} lines ({size}) to {file}.", &[("lines", &fmt_count(n.lines)), ("size", &human_bytes(n.bytes)), ("file", &file_name(target))])))
    })
}

pub(super) fn file_name(p: &std::path::Path) -> String {
    p.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_else(|| bigtext::file::display_path(p))
}

/// 1234567 → "1,234,567".
pub fn fmt_count(n: u64) -> String {
    let s = n.to_string();
    let mut out = String::with_capacity(s.len() + s.len() / 3);
    for (i, c) in s.chars().enumerate() {
        if i > 0 && (s.len() - i).is_multiple_of(3) {
            out.push(',');
        }
        out.push(c);
    }
    out
}

/// A writer that fails with `OutOfMemory` once more than the limit was written.
struct CappedVec<'a>(&'a mut Vec<u8>, u64);

impl std::io::Write for CappedVec<'_> {
    fn write(&mut self, b: &[u8]) -> std::io::Result<usize> {
        if self.0.len() as u64 + b.len() as u64 > self.1 {
            return Err(std::io::Error::from(std::io::ErrorKind::OutOfMemory));
        }
        self.0.extend_from_slice(b);
        Ok(b.len())
    }
    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}

/// One list row: line number and the line's text with its matches highlighted (context lines
/// dimmed, without highlights). When the first match is far to the right, the text starts a
/// little before it.
/// A listed line: its number and text, with `m`'s matches highlighted (context lines dim).
pub(crate) fn line_job(buf: &TextBuf, ls: u64, m: Option<&Matcher>, font: &FontId, v: &egui::Visuals, stale: bool, context: bool) -> LayoutJob {
    let mut job = LayoutJob::default();
    let weak = TextFormat { font_id: font.clone(), color: v.weak_text_color(), ..Default::default() };
    let normal = TextFormat { font_id: font.clone(), color: if stale || context { v.weak_text_color() } else { v.text_color() }, ..Default::default() };
    let hl = TextFormat { font_id: font.clone(), color: v.strong_text_color(), background: v.selection.bg_fill, ..Default::default() };
    let (line, exact) = buf.line_of(ls.min(buf.len()));
    job.append(&format!("{}{:>8}  ", if exact { " " } else { "~" }, line + 1), 0.0, weak.clone());
    let codec = buf.codec;
    let start = ls.max(codec.bom_len as u64).min(buf.len());
    let Some(raw) = buf.read(start..(start + SHOW_MAX).min(buf.len())) else {
        job.append("…", 0.0, normal);
        return job;
    };
    let cut = codec.find_nl(&raw, start).unwrap_or(raw.len());
    let mut raw = &raw[..cut];
    // Drop the CR of a CRLF line break.
    let cr = codec.cr_unit();
    if raw.ends_with(cr) && codec.nl == bigtext::encoding::NlByte::Lf {
        raw = &raw[..raw.len() - cr.len()];
    }
    let end = start + raw.len() as u64;
    let hits = match m {
        Some(m) if !context => bigtext::search::matches_in_view(buf, m, start..end, 64),
        _ => Vec::new(),
    };
    let mut from = start;
    if let Some(&(s, _)) = hits.first() {
        if s > start + 160 {
            let skip = buf.snap_char(s - 60).max(start);
            job.append("…", 0.0, weak);
            from = skip;
        }
    }
    let clean = |b: &[u8]| -> String { codec.decode_lossy(b).chars().map(|c| if c == '\t' { ' ' } else if c.is_control() { '·' } else { c }).collect() };
    let slice = |a: u64, b: u64| &raw[(a - start) as usize..(b - start) as usize];
    for &(s, e) in &hits {
        let (s, e) = (s.clamp(from, end), e.clamp(from, end));
        if e <= from || s >= end {
            continue;
        }
        if s > from {
            job.append(&clean(slice(from, s)), 0.0, normal.clone());
        }
        job.append(&clean(slice(s, e)), 0.0, hl.clone());
        from = e;
    }
    if from < end {
        job.append(&clean(slice(from, end)), 0.0, normal);
    }
    job
}