nu-explore 0.113.0

Nushell table pager
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
//! Quick reference data for the regex explorer.
//!
//! This module provides categorized regex patterns compatible with the
//! fancy-regex crate, similar to regex101.com's quick reference panel.

/// A single quick reference item
#[derive(Clone)]
pub struct QuickRefItem {
    /// The pattern syntax to display (e.g., "\\d")
    pub syntax: &'static str,
    /// Description of what the pattern matches
    pub description: &'static str,
    /// The actual pattern to insert (may differ from syntax for display purposes)
    pub insert: &'static str,
}

/// A category of quick reference items
pub struct QuickRefCategory {
    pub name: &'static str,
    pub items: &'static [QuickRefItem],
}

/// All quick reference categories - patterns are compatible with fancy-regex
pub static QUICK_REF_CATEGORIES: &[QuickRefCategory] = &[
    QuickRefCategory {
        name: "Anchors",
        items: &[
            QuickRefItem {
                syntax: "^",
                description: "Start of string/line",
                insert: "^",
            },
            QuickRefItem {
                syntax: "$",
                description: "End of string/line",
                insert: "$",
            },
            QuickRefItem {
                syntax: "\\b",
                description: "Word boundary",
                insert: "\\b",
            },
            QuickRefItem {
                syntax: "\\B",
                description: "Not a word boundary",
                insert: "\\B",
            },
            QuickRefItem {
                syntax: "\\A",
                description: "Start of string only",
                insert: "\\A",
            },
            QuickRefItem {
                syntax: "\\z",
                description: "End of string only",
                insert: "\\z",
            },
            QuickRefItem {
                syntax: "\\Z",
                description: "End before trailing newlines",
                insert: "\\Z",
            },
            QuickRefItem {
                syntax: "\\G",
                description: "Where previous match ended",
                insert: "\\G",
            },
        ],
    },
    QuickRefCategory {
        name: "Character Classes",
        items: &[
            QuickRefItem {
                syntax: ".",
                description: "Any character except newline",
                insert: ".",
            },
            QuickRefItem {
                syntax: "\\O",
                description: "Any character including newline",
                insert: "\\O",
            },
            QuickRefItem {
                syntax: "\\d",
                description: "Digit [0-9]",
                insert: "\\d",
            },
            QuickRefItem {
                syntax: "\\D",
                description: "Not a digit [^0-9]",
                insert: "\\D",
            },
            QuickRefItem {
                syntax: "\\w",
                description: "Word character [a-zA-Z0-9_]",
                insert: "\\w",
            },
            QuickRefItem {
                syntax: "\\W",
                description: "Not a word character",
                insert: "\\W",
            },
            QuickRefItem {
                syntax: "\\s",
                description: "Whitespace character",
                insert: "\\s",
            },
            QuickRefItem {
                syntax: "\\S",
                description: "Not a whitespace character",
                insert: "\\S",
            },
            QuickRefItem {
                syntax: "\\h",
                description: "Hex digit [0-9A-Fa-f]",
                insert: "\\h",
            },
            QuickRefItem {
                syntax: "\\H",
                description: "Not a hex digit",
                insert: "\\H",
            },
            QuickRefItem {
                syntax: "[abc]",
                description: "Any of a, b, or c",
                insert: "[abc]",
            },
            QuickRefItem {
                syntax: "[^abc]",
                description: "Not a, b, or c",
                insert: "[^abc]",
            },
            QuickRefItem {
                syntax: "[a-z]",
                description: "Character range a-z",
                insert: "[a-z]",
            },
            QuickRefItem {
                syntax: "[A-Z]",
                description: "Character range A-Z",
                insert: "[A-Z]",
            },
            QuickRefItem {
                syntax: "[0-9]",
                description: "Character range 0-9",
                insert: "[0-9]",
            },
        ],
    },
    QuickRefCategory {
        name: "Quantifiers",
        items: &[
            QuickRefItem {
                syntax: "*",
                description: "0 or more (greedy)",
                insert: "*",
            },
            QuickRefItem {
                syntax: "+",
                description: "1 or more (greedy)",
                insert: "+",
            },
            QuickRefItem {
                syntax: "?",
                description: "0 or 1 (greedy)",
                insert: "?",
            },
            QuickRefItem {
                syntax: "{n}",
                description: "Exactly n times",
                insert: "{1}",
            },
            QuickRefItem {
                syntax: "{n,}",
                description: "n or more times",
                insert: "{1,}",
            },
            QuickRefItem {
                syntax: "{n,m}",
                description: "Between n and m times",
                insert: "{1,3}",
            },
            QuickRefItem {
                syntax: "*?",
                description: "0 or more (lazy)",
                insert: "*?",
            },
            QuickRefItem {
                syntax: "+?",
                description: "1 or more (lazy)",
                insert: "+?",
            },
            QuickRefItem {
                syntax: "??",
                description: "0 or 1 (lazy)",
                insert: "??",
            },
            QuickRefItem {
                syntax: "{n,m}?",
                description: "Between n and m (lazy)",
                insert: "{1,3}?",
            },
        ],
    },
    QuickRefCategory {
        name: "Groups & Capturing",
        items: &[
            QuickRefItem {
                syntax: "(...)",
                description: "Capturing group",
                insert: "()",
            },
            QuickRefItem {
                syntax: "(?:...)",
                description: "Non-capturing group",
                insert: "(?:)",
            },
            QuickRefItem {
                syntax: "(?<name>...)",
                description: "Named capturing group",
                insert: "(?<name>)",
            },
            QuickRefItem {
                syntax: "(?P<name>...)",
                description: "Named group (Python style)",
                insert: "(?P<name>)",
            },
            QuickRefItem {
                syntax: "(?>...)",
                description: "Atomic group (no backtrack)",
                insert: "(?>)",
            },
            QuickRefItem {
                syntax: "\\1",
                description: "Backreference to group 1",
                insert: "\\1",
            },
            QuickRefItem {
                syntax: "\\k<name>",
                description: "Backreference to named group",
                insert: "\\k<name>",
            },
            QuickRefItem {
                syntax: "(?P=name)",
                description: "Named backref (Python style)",
                insert: "(?P=name)",
            },
            QuickRefItem {
                syntax: "a|b",
                description: "Match a or b (alternation)",
                insert: "|",
            },
        ],
    },
    QuickRefCategory {
        name: "Lookaround",
        items: &[
            QuickRefItem {
                syntax: "(?=...)",
                description: "Positive lookahead",
                insert: "(?=)",
            },
            QuickRefItem {
                syntax: "(?!...)",
                description: "Negative lookahead",
                insert: "(?!)",
            },
            QuickRefItem {
                syntax: "(?<=...)",
                description: "Positive lookbehind",
                insert: "(?<=)",
            },
            QuickRefItem {
                syntax: "(?<!...)",
                description: "Negative lookbehind",
                insert: "(?<!)",
            },
        ],
    },
    QuickRefCategory {
        name: "Conditionals",
        items: &[
            QuickRefItem {
                syntax: "(?(1)yes|no)",
                description: "If group 1 matched",
                insert: "(?(1)|)",
            },
            QuickRefItem {
                syntax: "(?(<n>)yes|no)",
                description: "If named group matched",
                insert: "(?(<name>)|)",
            },
        ],
    },
    QuickRefCategory {
        name: "Special",
        items: &[
            QuickRefItem {
                syntax: "\\K",
                description: "Reset match start",
                insert: "\\K",
            },
            QuickRefItem {
                syntax: "\\e",
                description: "Escape character (\\x1B)",
                insert: "\\e",
            },
        ],
    },
    QuickRefCategory {
        name: "Escape Sequences",
        items: &[
            QuickRefItem {
                syntax: "\\n",
                description: "Newline",
                insert: "\\n",
            },
            QuickRefItem {
                syntax: "\\r",
                description: "Carriage return",
                insert: "\\r",
            },
            QuickRefItem {
                syntax: "\\t",
                description: "Tab",
                insert: "\\t",
            },
            QuickRefItem {
                syntax: "\\xHH",
                description: "Hex character code",
                insert: "\\x00",
            },
            QuickRefItem {
                syntax: "\\u{HHHH}",
                description: "Unicode code point",
                insert: "\\u{0000}",
            },
            QuickRefItem {
                syntax: "\\\\",
                description: "Literal backslash",
                insert: "\\\\",
            },
            QuickRefItem {
                syntax: "\\.",
                description: "Literal dot",
                insert: "\\.",
            },
            QuickRefItem {
                syntax: "\\*",
                description: "Literal asterisk",
                insert: "\\*",
            },
            QuickRefItem {
                syntax: "\\+",
                description: "Literal plus",
                insert: "\\+",
            },
            QuickRefItem {
                syntax: "\\?",
                description: "Literal question mark",
                insert: "\\?",
            },
            QuickRefItem {
                syntax: "\\^",
                description: "Literal caret",
                insert: "\\^",
            },
            QuickRefItem {
                syntax: "\\$",
                description: "Literal dollar",
                insert: "\\$",
            },
            QuickRefItem {
                syntax: "\\[",
                description: "Literal bracket",
                insert: "\\[",
            },
            QuickRefItem {
                syntax: "\\(",
                description: "Literal parenthesis",
                insert: "\\(",
            },
            QuickRefItem {
                syntax: "\\{",
                description: "Literal brace",
                insert: "\\{",
            },
            QuickRefItem {
                syntax: "\\|",
                description: "Literal pipe",
                insert: "\\|",
            },
        ],
    },
    QuickRefCategory {
        name: "Flags/Modifiers",
        items: &[
            QuickRefItem {
                syntax: "(?i)",
                description: "Case insensitive",
                insert: "(?i)",
            },
            QuickRefItem {
                syntax: "(?m)",
                description: "Multiline (^ $ match lines)",
                insert: "(?m)",
            },
            QuickRefItem {
                syntax: "(?s)",
                description: "Dotall (. matches newlines)",
                insert: "(?s)",
            },
            QuickRefItem {
                syntax: "(?x)",
                description: "Extended (ignore whitespace)",
                insert: "(?x)",
            },
            QuickRefItem {
                syntax: "(?-i)",
                description: "Disable case insensitive",
                insert: "(?-i)",
            },
            QuickRefItem {
                syntax: "(?im)",
                description: "Multiple flags",
                insert: "(?im)",
            },
            QuickRefItem {
                syntax: "(?i:...)",
                description: "Flags for group only",
                insert: "(?i:)",
            },
        ],
    },
    QuickRefCategory {
        name: "Common Patterns",
        items: &[
            QuickRefItem {
                syntax: "\\d+",
                description: "One or more digits",
                insert: "\\d+",
            },
            QuickRefItem {
                syntax: "\\w+",
                description: "One or more word chars",
                insert: "\\w+",
            },
            QuickRefItem {
                syntax: "\\S+",
                description: "One or more non-whitespace",
                insert: "\\S+",
            },
            QuickRefItem {
                syntax: ".*",
                description: "Any characters (greedy)",
                insert: ".*",
            },
            QuickRefItem {
                syntax: ".*?",
                description: "Any characters (lazy)",
                insert: ".*?",
            },
            QuickRefItem {
                syntax: "^.*$",
                description: "Entire line",
                insert: "^.*$",
            },
            QuickRefItem {
                syntax: "(\\w+) \\1",
                description: "Repeated word",
                insert: "(\\w+) \\1",
            },
            QuickRefItem {
                syntax: "(?<!\\S)",
                description: "Start of word (lookbehind)",
                insert: "(?<!\\S)",
            },
            QuickRefItem {
                syntax: "(?!\\S)",
                description: "End of word (lookahead)",
                insert: "(?!\\S)",
            },
        ],
    },
];

/// Represents a flattened entry (either a category header or an item)
#[derive(Clone)]
pub enum QuickRefEntry {
    Category(&'static str),
    Item(QuickRefItem),
}

/// Get a flattened list of all entries (headers and items)
pub fn get_flattened_entries() -> Vec<QuickRefEntry> {
    let mut entries = Vec::new();
    for category in QUICK_REF_CATEGORIES {
        entries.push(QuickRefEntry::Category(category.name));
        for item in category.items {
            entries.push(QuickRefEntry::Item(item.clone()));
        }
    }
    entries
}

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

    #[test]
    fn test_all_quick_ref_patterns_are_valid() {
        // Patterns that are meant to be combined with other patterns
        // (they are partial/templates and won't compile on their own)
        let partial_patterns = [
            "*",            // quantifier, needs something before it
            "+",            // quantifier, needs something before it
            "?",            // quantifier, needs something before it
            "{1}",          // quantifier, needs something before it
            "{1,}",         // quantifier, needs something before it
            "{1,3}",        // quantifier, needs something before it
            "*?",           // lazy quantifier, needs something before it
            "+?",           // lazy quantifier, needs something before it
            "??",           // lazy quantifier, needs something before it
            "{1,3}?",       // lazy quantifier, needs something before it
            "|",            // alternation, needs something around it
            "\\1",          // backreference, needs a capturing group
            "\\k<name>",    // named backreference, needs a named group
            "(?P=name)",    // named backreference (Python), needs a named group
            "(?(1)|)",      // conditional, needs a capturing group
            "(?(<name>)|)", // conditional, needs a named group
        ];

        for category in QUICK_REF_CATEGORIES {
            for item in category.items {
                // Skip partial patterns that are meant to be combined
                if partial_patterns.contains(&item.insert) {
                    continue;
                }

                let result = Regex::new(item.insert);
                assert!(
                    result.is_ok(),
                    "Pattern '{}' (insert: '{}') in category '{}' failed to compile: {:?}",
                    item.syntax,
                    item.insert,
                    category.name,
                    result.err()
                );
            }
        }
    }

    #[test]
    fn test_partial_patterns_work_when_combined() {
        // Test that quantifier patterns work when applied to something
        let quantifiers = [
            "*", "+", "?", "{1}", "{1,}", "{1,3}", "*?", "+?", "??", "{1,3}?",
        ];
        for q in quantifiers {
            let pattern = format!("a{}", q);
            let result = Regex::new(&pattern);
            assert!(
                result.is_ok(),
                "Quantifier '{}' failed when combined: {:?}",
                q,
                result.err()
            );
        }

        // Test alternation
        let result = Regex::new("a|b");
        assert!(result.is_ok(), "Alternation pattern failed");
    }

    #[test]
    fn test_context_dependent_patterns() {
        // Test backreferences work with proper context
        let result = Regex::new(r"(\w+)\s+\1");
        assert!(result.is_ok(), "Backreference \\1 should work with group");

        // Test named backreference with named group
        let result = Regex::new(r"(?<word>\w+)\s+\k<word>");
        assert!(
            result.is_ok(),
            "Named backreference \\k<word> should work with named group"
        );

        // Test Python-style named backreference
        let result = Regex::new(r"(?P<word>\w+)\s+(?P=word)");
        assert!(
            result.is_ok(),
            "Python-style named backreference should work"
        );

        // Test conditional with group
        let result = Regex::new("(a)?(?(1)b|c)");
        assert!(result.is_ok(), "Conditional (?(1)...) should work");

        // Test conditional with named group
        let result = Regex::new("(?<test>a)?(?(<test>)b|c)");
        assert!(result.is_ok(), "Conditional with named group should work");
    }

    #[test]
    fn test_flattened_entries_not_empty() {
        let entries = get_flattened_entries();
        assert!(!entries.is_empty(), "Flattened entries should not be empty");

        // Check we have at least some categories and items
        let category_count = entries
            .iter()
            .filter(|e| matches!(e, QuickRefEntry::Category(_)))
            .count();
        let item_count = entries
            .iter()
            .filter(|e| matches!(e, QuickRefEntry::Item(_)))
            .count();

        assert!(category_count > 0, "Should have at least one category");
        assert!(item_count > 0, "Should have at least one item");
    }
}