jsonl-tui 0.1.1

Terminal explorer for JSONL files: search, filter, sort, group and export from your keyboard or mouse.
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
//! Filter expression parsing/evaluation and search matching.
//!
//! Filter syntax: space-separated clauses, AND-combined. Each clause is
//! `field OP value` with OP one of `=`, `!=`, `>`, `<`, `>=`, `<=`, `~`
//! (regex). Comparisons attempt numeric coercion (f64) first, falling back
//! to string comparison.

use std::cmp::Ordering;

use anyhow::{bail, Result};
use regex::{Regex, RegexBuilder};

use crate::data::{CompactValue, FlatRecord};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Op {
    Eq,
    Ne,
    Gt,
    Lt,
    Ge,
    Le,
    Regex,
}

#[derive(Debug, Clone)]
pub struct Clause {
    pub field: String,
    pub op: Op,
    pub value: String,
    regex: Option<Regex>,
}

/// Parse a whole filter expression into AND-combined clauses.
pub fn parse_filter(input: &str) -> Result<Vec<Clause>> {
    input.split_whitespace().map(parse_clause).collect()
}

fn parse_clause(token: &str) -> Result<Clause> {
    const OPS: [(&str, Op); 7] = [
        ("!=", Op::Ne),
        (">=", Op::Ge),
        ("<=", Op::Le),
        ("=", Op::Eq),
        (">", Op::Gt),
        ("<", Op::Lt),
        ("~", Op::Regex),
    ];
    // Pick the operator that appears earliest; on ties prefer the longer
    // symbol (so `>=` wins over `>` and `=`).
    let mut best: Option<(usize, &str, Op)> = None;
    for (sym, op) in OPS {
        if let Some(idx) = token.find(sym) {
            let better = match best {
                None => true,
                Some((bidx, bsym, _)) => idx < bidx || (idx == bidx && sym.len() > bsym.len()),
            };
            if better {
                best = Some((idx, sym, op));
            }
        }
    }
    let Some((idx, sym, op)) = best else {
        bail!("clause '{token}' has no operator (expected =, !=, >, <, >=, <=, ~)");
    };
    let field = token[..idx].trim();
    let value = token[idx + sym.len()..].trim();
    if field.is_empty() {
        bail!("clause '{token}' is missing a field name");
    }
    if value.is_empty() {
        bail!("clause '{token}' is missing a value");
    }
    let regex = if op == Op::Regex {
        Some(Regex::new(value).map_err(|e| anyhow::anyhow!("bad regex '{value}': {e}"))?)
    } else {
        None
    };
    Ok(Clause {
        field: field.to_string(),
        op,
        value: value.to_string(),
        regex,
    })
}

fn is_grouping_char(c: char) -> bool {
    // regular / no-break / narrow no-break space, apostrophe, underscore
    matches!(c, ' ' | '\u{a0}' | '\u{202f}' | '\'' | '_')
}

/// Extract a numeric value from a string that may contain currency symbols,
/// unit suffixes and grouping separators:
///   "1 000 $" -> 1000, "239129 EURO" -> 239129, "$1,234.56" -> 1234.56,
///   "1.234,56 \u{20ac}" -> 1234.56, "1'000'000" -> 1e6, "-$5,000" -> -5000.
/// Returns `None` when the string contains no digits.
pub fn fuzzy_number(s: &str) -> Option<f64> {
    let t = s.trim();
    if t.is_empty() {
        return None;
    }
    // Fast path: plain numbers (also covers scientific notation).
    if let Ok(n) = t.parse::<f64>() {
        return Some(n);
    }

    let chars: Vec<char> = t.chars().collect();
    let first = chars.iter().position(|c| c.is_ascii_digit())?;
    let negative = chars[..first].contains(&'-');

    // Collect the numeric token: digits plus separators.
    let mut end = first;
    while end < chars.len() {
        let c = chars[end];
        if c.is_ascii_digit() || c == '.' || c == ',' || is_grouping_char(c) {
            end += 1;
        } else {
            break;
        }
    }
    let token: String = chars[first..end].iter().collect();
    let token = token.trim_end_matches(|c: char| !c.is_ascii_digit());
    let cleaned: String = token.chars().filter(|c| !is_grouping_char(*c)).collect();

    // Disambiguate '.' vs ',' as decimal/grouping separators.
    let n_dots = cleaned.matches('.').count();
    let n_commas = cleaned.matches(',').count();
    let normalized: String = match (n_dots, n_commas) {
        (0, 0) => cleaned,
        // Only dots: one dot = decimal point; several = grouping ("1.234.567").
        (1, 0) => cleaned,
        (_, 0) => cleaned.chars().filter(|c| *c != '.').collect(),
        // Only commas: "1,234" (3 trailing digits) = grouping, "1,5" = decimal.
        (0, 1) => {
            let after = cleaned.split(',').nth(1).unwrap_or("");
            if after.len() == 3 {
                cleaned.replace(',', "")
            } else {
                cleaned.replace(',', ".")
            }
        }
        (0, _) => cleaned.replace(',', ""),
        // Both: whichever comes last is the decimal separator.
        (_, _) => {
            let dec = if cleaned.rfind(',') > cleaned.rfind('.') {
                ','
            } else {
                '.'
            };
            let grp = if dec == ',' { '.' } else { ',' };
            let no_grp: String = cleaned.chars().filter(|c| *c != grp).collect();
            // Keep only the last `dec` as the decimal point.
            match no_grp.rfind(dec) {
                Some(pos) => {
                    let (int, frac) = no_grp.split_at(pos);
                    format!("{}.{}", int.replace(dec, ""), &frac[dec.len_utf8()..])
                }
                None => no_grp,
            }
        }
    };
    let n: f64 = normalized.parse().ok()?;
    Some(if negative { -n } else { n })
}

/// A comparable sort key: numbers (including numbers embedded in noisy
/// strings) sort numerically and before plain strings.
#[derive(Debug, Clone, PartialEq)]
pub enum SortKey {
    Num(f64),
    Str(String),
}

impl SortKey {
    /// Direction-aware comparison. The type grouping is fixed regardless of
    /// direction — numbers always sort before plain strings (and null/missing
    /// always last, handled by the caller); `desc` only reverses the order
    /// *within* each group.
    pub fn compare(&self, other: &SortKey, desc: bool) -> Ordering {
        let dir = |ord: Ordering| if desc { ord.reverse() } else { ord };
        match (self, other) {
            (SortKey::Num(a), SortKey::Num(b)) => {
                dir(a.partial_cmp(b).unwrap_or(Ordering::Equal))
            }
            (SortKey::Str(a), SortKey::Str(b)) => dir(a.cmp(b)),
            (SortKey::Num(_), SortKey::Str(_)) => Ordering::Less,
            (SortKey::Str(_), SortKey::Num(_)) => Ordering::Greater,
        }
    }
}

/// Sort key for a flattened value:
/// - JSON `null` returns `None`, so it sorts last together with missing fields
/// - numbers, and strings that contain a number ("1 000 $", "239129 EURO"),
///   sort numerically
/// - everything else sorts by its string form
pub fn sort_key(v: &CompactValue) -> Option<SortKey> {
    match v {
        CompactValue::Null => None,
        CompactValue::Int(_) | CompactValue::UInt(_) | CompactValue::Float(_) => {
            Some(SortKey::Num(v.as_number().unwrap_or(0.0)))
        }
        CompactValue::Str(s) => Some(match fuzzy_number(s) {
            Some(n) => SortKey::Num(n),
            None => SortKey::Str(s.to_string()),
        }),
        other => Some(SortKey::Str(other.display())),
    }
}

/// Numeric-first comparison: if both sides contain a number (possibly inside
/// a noisy string like "1 000 $"), compare numerically, otherwise compare as
/// strings.
pub fn coerce_compare(a: &str, b: &str) -> Ordering {
    match (fuzzy_number(a), fuzzy_number(b)) {
        (Some(x), Some(y)) => x.partial_cmp(&y).unwrap_or(Ordering::Equal),
        _ => a.cmp(b),
    }
}

impl Clause {
    /// Evaluate this clause against a flattened value (`None` = missing).
    /// A missing field fails every clause except `!=`, which succeeds
    /// (absent implies "not equal to the given value"). JSON `null` matches
    /// `field=null` / `field!=x` / regex, but never range comparisons.
    pub fn matches(&self, v: Option<&CompactValue>) -> bool {
        let Some(v) = v else {
            return self.op == Op::Ne;
        };
        if v.is_null() {
            return match self.op {
                Op::Eq => self.value == "null",
                Op::Ne => self.value != "null",
                Op::Regex => self
                    .regex
                    .as_ref()
                    .map(|re| re.is_match("null"))
                    .unwrap_or(false),
                _ => false, // null never satisfies >, <, >=, <=
            };
        }
        if self.op == Op::Regex {
            return self
                .regex
                .as_ref()
                .map(|re| re.is_match(&v.display()))
                .unwrap_or(false);
        }
        // Fast path: native JSON numbers vs a numeric filter value avoids
        // the string round-trip.
        let ord = match (v.as_number(), fuzzy_number(&self.value)) {
            (Some(a), Some(b)) => a.partial_cmp(&b).unwrap_or(Ordering::Equal),
            _ => coerce_compare(&v.display(), &self.value),
        };
        match self.op {
            Op::Regex => unreachable!("handled above"),
            Op::Eq => ord == Ordering::Equal,
            Op::Ne => ord != Ordering::Equal,
            Op::Gt => ord == Ordering::Greater,
            Op::Lt => ord == Ordering::Less,
            Op::Ge => ord != Ordering::Less,
            Op::Le => ord != Ordering::Greater,
        }
    }
}

/// All clauses must match (AND). `ids[i]` is the interned path id of
/// `clauses[i].field` (`None` when the field doesn't exist in the file).
pub fn matches_all(clauses: &[Clause], ids: &[Option<u32>], flat: &FlatRecord) -> bool {
    clauses
        .iter()
        .zip(ids)
        .all(|(c, id)| c.matches(id.and_then(|i| flat.get(i))))
}

/// Compiled search query.
pub enum SearchMode {
    /// Lowercased needle for case-insensitive substring search.
    Substring(String),
    /// Case-insensitive regex (input prefixed with `re:`).
    Regex(Regex),
}

/// Compile a search query. Empty/whitespace input means "no search".
pub fn build_search(query: &str) -> Result<Option<SearchMode>> {
    let q = query.trim();
    if q.is_empty() {
        return Ok(None);
    }
    if let Some(pat) = q.strip_prefix("re:") {
        if pat.is_empty() {
            return Ok(None);
        }
        let re = RegexBuilder::new(pat)
            .case_insensitive(true)
            .build()
            .map_err(|e| anyhow::anyhow!("bad search regex: {e}"))?;
        Ok(Some(SearchMode::Regex(re)))
    } else {
        Ok(Some(SearchMode::Substring(q.to_lowercase())))
    }
}

/// True if any of the given columns (interned path ids) matches the search.
pub fn record_matches_search(flat: &FlatRecord, columns: &[u32], mode: &SearchMode) -> bool {
    columns.iter().filter_map(|&id| flat.get(id)).any(|v| {
        let s = v.display();
        match mode {
            SearchMode::Substring(q) => s.to_lowercase().contains(q),
            SearchMode::Regex(re) => re.is_match(&s),
        }
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::data::{flatten_compact, PathInterner};
    use serde_json::json;

    struct Ctx {
        interner: PathInterner,
        flat: FlatRecord,
    }

    fn flat(v: serde_json::Value) -> Ctx {
        let mut interner = PathInterner::default();
        let flat = flatten_compact(&v, &mut interner);
        Ctx { interner, flat }
    }

    fn check_all(clauses: &[Clause], ctx: &Ctx) -> bool {
        let ids: Vec<Option<u32>> = clauses.iter().map(|c| ctx.interner.id(&c.field)).collect();
        matches_all(clauses, &ids, &ctx.flat)
    }

    fn check_search(ctx: &Ctx, columns: &[&str], mode: &SearchMode) -> bool {
        let ids: Vec<u32> = columns.iter().filter_map(|c| ctx.interner.id(c)).collect();
        record_matches_search(&ctx.flat, &ids, mode)
    }

    #[test]
    fn parse_all_operators() {
        let cases = [
            ("a=1", Op::Eq),
            ("a!=1", Op::Ne),
            ("a>1", Op::Gt),
            ("a<1", Op::Lt),
            ("a>=1", Op::Ge),
            ("a<=1", Op::Le),
            ("a~^x", Op::Regex),
        ];
        for (input, op) in cases {
            let c = &parse_filter(input).unwrap()[0];
            assert_eq!(c.op, op, "input {input}");
            assert_eq!(c.field, "a");
        }
    }

    #[test]
    fn parse_multi_clause_and_values() {
        let cs = parse_filter("status=error user.score>=3").unwrap();
        assert_eq!(cs.len(), 2);
        assert_eq!(cs[0].field, "status");
        assert_eq!(cs[0].value, "error");
        assert_eq!(cs[1].field, "user.score");
        assert_eq!(cs[1].op, Op::Ge);
        assert_eq!(cs[1].value, "3");
    }

    #[test]
    fn parse_errors() {
        assert!(parse_filter("noop").is_err());
        assert!(parse_filter("=value").is_err());
        assert!(parse_filter("field=").is_err());
        assert!(parse_filter("a~[bad").is_err());
    }

    #[test]
    fn numeric_coercion_beats_string_compare() {
        let r = flat(json!({"score": 10}));
        // string compare would say "10" < "3"; numeric must win
        assert!(check_all(&parse_filter("score>3").unwrap(), &r));
        assert!(!check_all(&parse_filter("score<3").unwrap(), &r));
        assert!(check_all(&parse_filter("score=10.0").unwrap(), &r));
    }

    #[test]
    fn string_fallback_compare() {
        let r = flat(json!({"name": "banana"}));
        assert!(check_all(&parse_filter("name>apple").unwrap(), &r));
        assert!(check_all(&parse_filter("name!=apple").unwrap(), &r));
    }

    #[test]
    fn regex_clause() {
        let r = flat(json!({"msg": "connection timeout after 30s"}));
        assert!(check_all(&parse_filter("msg~time.ut").unwrap(), &r));
        assert!(!check_all(&parse_filter("msg~^timeout").unwrap(), &r));
    }

    #[test]
    fn nested_paths_and_missing_fields() {
        let r = flat(json!({"user": {"id": 7}}));
        assert!(check_all(&parse_filter("user.id=7").unwrap(), &r));
        // missing field: fails everything except !=
        assert!(!check_all(&parse_filter("ghost=1").unwrap(), &r));
        assert!(check_all(&parse_filter("ghost!=1").unwrap(), &r));
    }

    #[test]
    fn and_combination() {
        let r = flat(json!({"a": 1, "b": "x"}));
        assert!(check_all(&parse_filter("a=1 b=x").unwrap(), &r));
        assert!(!check_all(&parse_filter("a=1 b=y").unwrap(), &r));
    }

    #[test]
    fn search_substring_case_insensitive() {
        let r = flat(json!({"msg": "Hello World", "other": 5}));
        let m = build_search("wORLd").unwrap().unwrap();
        assert!(check_search(&r, &["msg"], &m));
        let m = build_search("mars").unwrap().unwrap();
        assert!(!check_search(&r, &["msg"], &m));
        // only visible columns are searched
        let m = build_search("5").unwrap().unwrap();
        assert!(!check_search(&r, &["msg"], &m));
    }

    #[test]
    fn search_regex_mode() {
        let r = flat(json!({"msg": "Error 404"}));
        let m = build_search("re:error \\d+").unwrap().unwrap();
        assert!(check_search(&r, &["msg"], &m));
        assert!(build_search("re:[bad").is_err());
        assert!(build_search("   ").unwrap().is_none());
    }

    #[test]
    fn fuzzy_number_parsing() {
        assert_eq!(fuzzy_number("1 000 $"), Some(1000.0));
        assert_eq!(fuzzy_number("239129 EURO"), Some(239129.0));
        assert_eq!(fuzzy_number("$1,234.56"), Some(1234.56));
        assert_eq!(fuzzy_number("1.234,56 \u{20ac}"), Some(1234.56));
        assert_eq!(fuzzy_number("1'000'000"), Some(1_000_000.0));
        assert_eq!(fuzzy_number("-$5,000"), Some(-5000.0));
        assert_eq!(fuzzy_number("1\u{a0}000"), Some(1000.0)); // no-break space
        assert_eq!(fuzzy_number("2.5"), Some(2.5));
        assert_eq!(fuzzy_number("-2e3"), Some(-2000.0));
        assert_eq!(fuzzy_number("1,5"), Some(1.5)); // decimal comma
        assert_eq!(fuzzy_number("1,234"), Some(1234.0)); // grouping comma
        assert_eq!(fuzzy_number("1.234.567"), Some(1_234_567.0)); // grouping dots
        assert_eq!(fuzzy_number("12%"), Some(12.0));
        assert_eq!(fuzzy_number("abc"), None);
        assert_eq!(fuzzy_number(""), None);
        assert_eq!(fuzzy_number("N/A"), None);
    }

    #[test]
    fn filter_compares_noisy_numeric_strings() {
        let r = flat(json!({"price": "1 500 $"}));
        assert!(check_all(&parse_filter("price>1000").unwrap(), &r));
        assert!(!check_all(&parse_filter("price>2000").unwrap(), &r));
        assert!(check_all(&parse_filter("price=1500").unwrap(), &r));
        let r = flat(json!({"price": "239129 EURO"}));
        assert!(check_all(&parse_filter("price>=239129").unwrap(), &r));
        assert!(check_all(&parse_filter("price<240000").unwrap(), &r));
    }

    #[test]
    fn null_values_in_filters() {
        let r = flat(json!({"price": null}));
        assert!(check_all(&parse_filter("price=null").unwrap(), &r));
        assert!(!check_all(&parse_filter("price!=null").unwrap(), &r));
        // null never satisfies numeric range clauses
        assert!(!check_all(&parse_filter("price>0").unwrap(), &r));
    }

    fn cv(v: serde_json::Value) -> CompactValue {
        CompactValue::from_value(&v)
    }

    #[test]
    fn sort_key_classification() {
        assert_eq!(sort_key(&cv(json!(null))), None, "null sorts like missing");
        assert_eq!(sort_key(&cv(json!(5))), Some(SortKey::Num(5.0)));
        assert_eq!(sort_key(&cv(json!("1 000 $"))), Some(SortKey::Num(1000.0)));
        assert_eq!(
            sort_key(&cv(json!("hello"))),
            Some(SortKey::Str("hello".into()))
        );
        // numbers sort before plain strings, in both directions
        assert_eq!(
            SortKey::Num(2.0).compare(&SortKey::Str("a".into()), false),
            Ordering::Less
        );
        assert_eq!(
            SortKey::Num(2.0).compare(&SortKey::Str("a".into()), true),
            Ordering::Less
        );
        assert_eq!(
            SortKey::Num(500.0).compare(&SortKey::Num(1000.0), false),
            Ordering::Less
        );
        assert_eq!(
            SortKey::Num(500.0).compare(&SortKey::Num(1000.0), true),
            Ordering::Greater
        );
    }
}