bigtext 0.14.0

Free editor for huge text and log files (1-50+ GB): opens them without loading them into memory, 4 GB/s search, 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
//! Named sessions (File → Sessions): the open files with their positions, bookmarks and pins,
//! each tab's filter, and the highlights, saved under a name such as "Incident 2026-10-28" and
//! opened again later. And Edit → Copy file:line, a `path:line` reference that
//! `bigtext path:line` opens at that line.

#[cfg(test)]
use std::path::Path;
use std::path::PathBuf;

use bigtext::file;
use bigtext::loglevel::Levels;
use bigtext::search::Query;

use super::document::Restore;
use super::extras::{self, TabState};
use super::i18n::{tr, trf};
use super::marks::Mark;
use super::BigTextApp;

/// A tab's filter, to run again when the session opens.
#[derive(Clone, Debug, PartialEq)]
pub struct SavedFilter {
    pub query: Query,
    pub invert: bool,
    pub levels: Option<Levels>,
    /// Conditions added with the Narrow row: (query, lines must lack it).
    pub terms: Vec<(Query, bool)>,
}

#[derive(Clone, Debug, Default)]
pub struct Session {
    pub tabs: Vec<(TabState, bool, Option<SavedFilter>)>,
    /// What was chosen for the files (encoding, view, table layout).
    pub opts: Vec<extras::FileOpts>,
    pub active: usize,
    pub marks: Vec<Mark>,
}

#[derive(Default)]
pub struct SessionsUi {
    pub name: String,
}

/// The folder sessions are kept in (next to the state file).
pub fn sessions_dir() -> Option<PathBuf> {
    Some(extras::state_file()?.parent()?.join("sessions"))
}

/// A file name for a session name (letters, digits, spaces, `-`, `_` and `.` kept).
fn file_for(name: &str) -> Option<PathBuf> {
    let safe: String = name
        .trim()
        .chars()
        .map(|c| {
            if c.is_alphanumeric() || " -_.".contains(c) {
                c
            } else {
                '_'
            }
        })
        .collect();
    let safe = safe.trim_matches(['.', ' ']).to_string();
    (!safe.is_empty())
        .then(|| sessions_dir().map(|d| d.join(format!("{safe}.session"))))
        .flatten()
}

/// Saved sessions by name, newest first.
pub fn list() -> Vec<String> {
    let Some(dir) = sessions_dir() else {
        return Vec::new();
    };
    let Ok(entries) = std::fs::read_dir(dir) else {
        return Vec::new();
    };
    let mut v: Vec<(std::time::SystemTime, String)> = entries
        .flatten()
        .filter_map(|e| {
            let p = e.path();
            let name = p.file_stem()?.to_string_lossy().into_owned();
            (p.extension()? == "session").then(|| {
                (
                    e.metadata()
                        .and_then(|m| m.modified())
                        .unwrap_or(std::time::UNIX_EPOCH),
                    name,
                )
            })
        })
        .collect();
    v.sort_by_key(|x| std::cmp::Reverse(x.0));
    v.into_iter().map(|x| x.1).collect()
}

pub fn serialize(s: &Session) -> String {
    let mut out = String::from("# BigText session\n");
    for (i, (t, pinned, filter)) in s.tabs.iter().enumerate() {
        out.push_str(&format!("tab={}\n", extras::tab_line(t)));
        if *pinned {
            out.push_str(&format!("pinned={i}\n"));
        }
        if let Some(f) = filter
            .as_ref()
            .filter(|f| !f.query.pattern.contains(['\n', '\r']))
        {
            let levels = f.levels.map(|l| l.bits().to_string()).unwrap_or_default();
            out.push_str(&format!(
                "filter={i}|{levels}|{}|{}|{}\n",
                u8::from(f.invert),
                extras::query_flags(&f.query),
                f.query.pattern
            ));
            for (q, negated) in f
                .terms
                .iter()
                .filter(|t| !t.0.pattern.contains(['\n', '\r']))
            {
                out.push_str(&format!(
                    "filterterm={i}|{}|{}|{}\n",
                    u8::from(*negated),
                    extras::query_flags(q),
                    q.pattern
                ));
            }
        }
    }
    for o in &s.opts {
        out.push_str(&format!("fileopt={}\n", extras::file_opts_line(o)));
    }
    for m in &s.marks {
        out.push_str(&format!("mark={}\n", extras::mark_line(m)));
    }
    out.push_str(&format!("active={}\n", s.active));
    out
}

pub fn parse(text: &str) -> Session {
    let mut s = Session::default();
    for line in text.lines() {
        let Some((k, v)) = line.split_once('=') else {
            continue;
        };
        match k {
            "tab" => s
                .tabs
                .extend(extras::parse_tab(v).map(|t| (t, false, None))),
            "pinned" => {
                if let Some(t) = v.parse::<usize>().ok().and_then(|i| s.tabs.get_mut(i)) {
                    t.1 = true;
                }
            }
            "filter" => {
                let mut it = v.splitn(5, '|');
                let (Some(i), Some(levels), Some(invert), Some(flags), Some(pattern)) =
                    (it.next(), it.next(), it.next(), it.next(), it.next())
                else {
                    continue;
                };
                let f = SavedFilter {
                    query: extras::query_from(pattern.to_string(), flags),
                    invert: invert == "1",
                    levels: levels.parse().ok().map(Levels::from_bits),
                    terms: Vec::new(),
                };
                if let Some(t) = i.parse::<usize>().ok().and_then(|i| s.tabs.get_mut(i)) {
                    t.2 = Some(f);
                }
            }
            "filterterm" => {
                let mut it = v.splitn(4, '|');
                let (Some(i), Some(negated), Some(flags), Some(pattern)) =
                    (it.next(), it.next(), it.next(), it.next())
                else {
                    continue;
                };
                if let Some(f) = i
                    .parse::<usize>()
                    .ok()
                    .and_then(|i| s.tabs.get_mut(i))
                    .and_then(|t| t.2.as_mut())
                {
                    f.terms.push((
                        extras::query_from(pattern.to_string(), flags),
                        negated == "1",
                    ));
                }
            }
            "mark" => s.marks.extend(extras::parse_mark(v)),
            "fileopt" => s.opts.extend(extras::parse_file_opts(v)),
            "active" => s.active = v.parse().unwrap_or(0),
            _ => {}
        }
    }
    s
}

impl BigTextApp {
    /// The open files (not temporary ones), their state, and the highlights.
    pub(crate) fn current_session(&self) -> Session {
        let mut s = Session {
            marks: self.settings.marks.clone(),
            ..Default::default()
        };
        for (i, d) in self.docs.iter().enumerate() {
            if d.temporary && d.unpacked_from.is_none() {
                continue;
            }
            let Some(path) = d.unpacked_from.as_deref().or(d.path.as_deref()) else {
                continue;
            };
            if i == self.active {
                s.active = s.tabs.len();
            }
            let tab = TabState {
                path: PathBuf::from(file::display_path(path)),
                member: d.member.clone(),
                caret: d.view.caret,
                top: d.view.top,
                wrap: d.view.wrap,
                bookmarks: d.bookmarks.clone(),
            };
            let filter = d.filter.as_ref().map(|f| SavedFilter {
                query: f.query.clone(),
                invert: f.spec.invert,
                levels: f.levels,
                terms: f
                    .terms
                    .iter()
                    .map(|t| (t.query.clone(), t.negated))
                    .collect(),
            });
            s.tabs.push((tab, d.pinned, filter));
            s.opts
                .extend(extras::doc_opts(d).filter(|o| o.worth_keeping()));
        }
        s
    }

    /// Saves the current session under `name` (replacing one of that name).
    pub(crate) fn save_session(&mut self, name: &str) -> Result<PathBuf, String> {
        let path = file_for(name).ok_or_else(|| tr("Type a name for the session.").to_string())?;
        if let Some(dir) = path.parent() {
            std::fs::create_dir_all(dir).map_err(|e| e.to_string())?;
        }
        super::extras::write_atomic(&path, serialize(&self.current_session()).as_bytes())
            .map_err(|e| super::friendly_io(&e, &path))?;
        Ok(path)
    }

    /// Opens the files of session `name` (already open ones keep their tabs) at their
    /// positions, with their filters, and adds its highlights.
    pub(crate) fn open_session(&mut self, name: &str) {
        let Some(path) = file_for(name) else { return };
        let text = match std::fs::read_to_string(&path) {
            Ok(t) => t,
            Err(e) => {
                self.error = Some(super::friendly_io(&e, &path));
                return;
            }
        };
        self.apply_session(parse(&text));
        self.notify(trf("Opened the session “{name}”.", &[("name", name)]), None);
    }

    pub(crate) fn apply_session(&mut self, s: Session) {
        for o in s.opts.into_iter().rev() {
            extras::merge_opts(&mut self.file_opts, o);
        }
        for m in s.marks {
            if !self.settings.marks.iter().any(|x| x.query == m.query) {
                self.settings.marks.push(m);
            }
        }
        let mut active = None;
        for (i, (t, pinned, filter)) in s.tabs.into_iter().enumerate() {
            self.open_path_member(t.path.clone(), t.member.clone());
            let Some(d) = self.docs.get_mut(self.active) else {
                continue;
            };
            d.pinned |= pinned;
            if i == s.active {
                active = Some(d.id);
            }
            if d.is_ready() {
                let len = d.buf.len();
                d.set_caret(t.caret.min(len), false);
                d.view.top = t.top.min(len);
                d.view.wrap = t.wrap;
                d.bookmarks = t.bookmarks.into_iter().filter(|&b| b <= len).collect();
                d.pending_filter = filter;
            } else {
                d.restore = Some(Restore {
                    caret: t.caret,
                    top: t.top,
                    wrap: t.wrap,
                    bookmarks: t.bookmarks,
                    filter,
                    ..Default::default()
                });
            }
        }
        let current = self.docs.get(self.active).map(|d| d.id);
        self.docs.sort_by_key(|d| !d.pinned);
        if let Some(i) = active
            .or(current)
            .and_then(|id| self.docs.iter().position(|d| d.id == id))
        {
            self.active = i;
        }
        self.persist_state();
    }

    /// Runs the filters of tabs restored from a session once they have opened.
    pub(super) fn run_pending_filters(&mut self) {
        let Some(i) = self
            .docs
            .iter()
            .position(|d| d.pending_filter.is_some() && d.is_ready())
        else {
            return;
        };
        let Some(f) = self.docs[i].pending_filter.take() else {
            return;
        };
        let active = self.active;
        self.active = i;
        let codec = self.docs[i].buf.codec;
        let terms: Vec<_> = f
            .terms
            .iter()
            .filter_map(|(q, negated)| {
                bigtext::search::Matcher::new(q, codec).ok().map(|m| {
                    super::filter_panel::FilterTerm {
                        query: q.clone(),
                        negated: *negated,
                        matcher: std::sync::Arc::new(m),
                    }
                })
            })
            .collect();
        let query = match f.levels {
            Some(lv) => bigtext::loglevel::query(lv),
            None => Some(f.query),
        };
        if let Some(query) = query {
            if let Ok(m) = bigtext::search::Matcher::new(&query, codec) {
                let spec = self.with_context(bigtext::filter::FilterSpec {
                    invert: f.invert && f.levels.is_none(),
                    ..Default::default()
                });
                self.run_filter_req(
                    super::filter_panel::FilterRequest {
                        query,
                        m: std::sync::Arc::new(m),
                        spec,
                        levels: f.levels,
                        follow: None,
                        terms,
                        json: None,
                    },
                    None,
                );
            }
        }
        self.active = active;
    }

    pub(crate) fn delete_session(&mut self, name: &str) {
        if let Some(p) = file_for(name) {
            let _ = std::fs::remove_file(p);
        }
    }

    /// File → Sessions: save the current one, open or delete saved ones.
    pub(super) fn sessions_menu(&mut self, ui: &mut egui::Ui) {
        ui.set_min_width(280.0);
        ui.horizontal(|ui| {
            ui.add(egui::TextEdit::singleline(&mut self.sessions.name).desired_width(170.0).hint_text(tr("Name")));
            if ui.add_enabled(!self.sessions.name.trim().is_empty(), egui::Button::new(tr("Save session"))).on_hover_text(tr("Saves the open files with their positions, bookmarks, pins and filters, and the highlights")).clicked() {
                let name = std::mem::take(&mut self.sessions.name);
                match self.save_session(&name) {
                    Ok(_) => self.notify(trf("Saved the session “{name}”.", &[("name", &name)]), None),
                    Err(e) => self.error = Some(e),
                }
            }
        });
        let names = list();
        if !names.is_empty() {
            ui.separator();
        }
        let (mut open, mut delete) = (None, None);
        for n in names {
            ui.horizontal(|ui| {
                if ui.button(&n).clicked() {
                    open = Some(n.clone());
                }
                if ui.small_button("×").on_hover_text(tr("Delete")).clicked() {
                    delete = Some(n.clone());
                }
            });
        }
        if let Some(n) = open {
            ui.close();
            self.open_session(&n);
        }
        if let Some(n) = delete {
            self.delete_session(&n);
        }
    }

    /// Edit → Copy file:line: "C:\logs\app.log:1234" for the caret's line.
    pub(crate) fn copy_file_line(&mut self, ctx: &egui::Context) {
        let Some(d) = self.docs.get(self.active) else {
            return;
        };
        let Some(p) = d
            .unpacked_from
            .as_deref()
            .or(d.path.as_deref())
            .filter(|_| !d.temporary || d.unpacked_from.is_some())
        else {
            self.notify(tr("Save the document first: it has no file.").into(), None);
            return;
        };
        let (line, exact) = d.buf.line_of(d.view.caret);
        let text = format!("{}:{}", file::display_path(p), line + 1);
        ctx.copy_text(text.clone());
        let note = if exact {
            trf("Copied {text}", &[("text", &text)])
        } else {
            trf(
                "Copied {text} (the line number is an estimate until indexing ends)",
                &[("text", &text)],
            )
        };
        self.notify(note, None);
    }
}

/// Whether `p` is inside the sessions folder.
#[cfg(test)]
fn in_sessions_dir(p: &Path) -> bool {
    sessions_dir().is_some_and(|d| p.starts_with(d))
}

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

    #[test]
    fn round_trip() {
        let s = Session {
            tabs: vec![
                (
                    TabState {
                        path: PathBuf::from(r"C:\logs\a|b.log"),
                        member: None,
                        caret: 10,
                        top: 5,
                        wrap: true,
                        bookmarks: vec![1, 2],
                    },
                    true,
                    Some(SavedFilter {
                        query: Query {
                            pattern: "ERROR|WARN".into(),
                            regex: true,
                            case_sensitive: true,
                            whole_word: false,
                            dot_all: false,
                        },
                        invert: true,
                        levels: None,
                        terms: vec![
                            (
                                Query {
                                    pattern: "db|x".into(),
                                    regex: false,
                                    case_sensitive: true,
                                    whole_word: true,
                                    dot_all: false,
                                },
                                true,
                            ),
                            (
                                Query {
                                    pattern: "user=1".into(),
                                    ..Query::default()
                                },
                                false,
                            ),
                        ],
                    }),
                ),
                (
                    TabState {
                        path: PathBuf::from("/var/log/x.log"),
                        member: Some("x/y.log".into()),
                        caret: 0,
                        top: 0,
                        wrap: false,
                        bookmarks: vec![],
                    },
                    false,
                    Some(SavedFilter {
                        query: Query::default(),
                        invert: false,
                        levels: Some(Levels::only(Level::Error).toggled(Level::Warn)),
                        terms: Vec::new(),
                    }),
                ),
            ],
            active: 1,
            opts: vec![extras::FileOpts {
                path: PathBuf::from(r"C:\logs\a|b.log"),
                enc: Some(bigtext::encoding::Encoding::Utf16Be),
                wrap: true,
                ..Default::default()
            }],
            marks: vec![Mark {
                query: Query {
                    pattern: "id=\\d+".into(),
                    regex: true,
                    ..Default::default()
                },
                color: 2,
                enabled: true,
            }],
        };
        let back = parse(&serialize(&s));
        assert_eq!(back.tabs.len(), 2);
        assert_eq!(back.tabs[0].0.path, s.tabs[0].0.path);
        assert_eq!((back.tabs[0].1, back.tabs[1].1), (true, false));
        assert_eq!(back.tabs[0].2, s.tabs[0].2);
        assert_eq!(
            back.tabs[1].2.as_ref().and_then(|f| f.levels),
            Some(Levels::only(Level::Error).toggled(Level::Warn))
        );
        assert_eq!((back.active, back.marks.clone()), (1, s.marks.clone()));
        assert_eq!(back.opts, s.opts);
    }

    #[test]
    fn names_become_safe_file_names() {
        let Some(p) = file_for("Incident: 2026/10/28") else {
            return;
        };
        assert_eq!(
            p.file_name().unwrap().to_string_lossy(),
            "Incident_ 2026_10_28.session"
        );
        assert!(file_for(" .. ").is_none());
        assert!(in_sessions_dir(&p));
    }
}