rgx-cli 0.12.0

A terminal regex tester with real-time matching, multi-engine support, and plain-English explanations
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
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
use std::collections::VecDeque;
use std::time::{Duration, Instant};

use crate::ansi::{GREEN_BOLD, RED_BOLD, RESET};
use crate::engine::{self, CompiledRegex, EngineFlags, EngineKind, RegexEngine};
use crate::explain::{self, ExplainNode};
use crate::input::editor::Editor;
use crate::input::Action;

const MAX_PATTERN_HISTORY: usize = 100;
const STATUS_DISPLAY_TICKS: u32 = 40; // ~2 seconds at 50ms tick rate

#[derive(Debug, Clone)]
pub struct BenchmarkResult {
    pub engine: EngineKind,
    pub compile_time: Duration,
    pub match_time: Duration,
    pub match_count: usize,
    pub error: Option<String>,
}

fn truncate(s: &str, max_chars: usize) -> String {
    let char_count = s.chars().count();
    if char_count <= max_chars {
        s.to_string()
    } else {
        let end = s
            .char_indices()
            .nth(max_chars)
            .map(|(i, _)| i)
            .unwrap_or(s.len());
        format!("{}...", &s[..end])
    }
}

#[derive(Default)]
pub struct OverlayState {
    pub help: bool,
    pub help_page: usize,
    pub recipes: bool,
    pub recipe_index: usize,
    pub benchmark: bool,
    pub codegen: bool,
    pub codegen_language_index: usize,
    pub grex: Option<crate::ui::grex_overlay::GrexOverlayState>,
}

#[derive(Default)]
pub struct ScrollState {
    pub match_scroll: u16,
    pub replace_scroll: u16,
    pub explain_scroll: u16,
}

#[derive(Default)]
pub struct PatternHistory {
    pub entries: VecDeque<String>,
    pub index: Option<usize>,
    pub temp: Option<String>,
}

#[derive(Default)]
pub struct MatchSelection {
    pub match_index: usize,
    pub capture_index: Option<usize>,
}

#[derive(Default)]
pub struct StatusMessage {
    pub text: Option<String>,
    ticks: u32,
}

impl StatusMessage {
    pub fn set(&mut self, message: String) {
        self.text = Some(message);
        self.ticks = STATUS_DISPLAY_TICKS;
    }

    pub fn tick(&mut self) -> bool {
        if self.text.is_some() {
            if self.ticks > 0 {
                self.ticks -= 1;
            } else {
                self.text = None;
                return true;
            }
        }
        false
    }
}

pub struct App {
    pub regex_editor: Editor,
    pub test_editor: Editor,
    pub replace_editor: Editor,
    pub focused_panel: u8,
    pub engine_kind: EngineKind,
    pub flags: EngineFlags,
    pub matches: Vec<engine::Match>,
    pub replace_result: Option<engine::ReplaceResult>,
    pub explanation: Vec<ExplainNode>,
    pub error: Option<String>,
    pub overlay: OverlayState,
    pub should_quit: bool,
    pub scroll: ScrollState,
    pub history: PatternHistory,
    pub selection: MatchSelection,
    pub status: StatusMessage,
    pub show_whitespace: bool,
    pub rounded_borders: bool,
    pub vim_mode: bool,
    pub vim_state: crate::input::vim::VimState,
    pub compile_time: Option<Duration>,
    pub match_time: Option<Duration>,
    pub error_offset: Option<usize>,
    pub output_on_quit: bool,
    pub workspace_path: Option<String>,
    pub benchmark_results: Vec<BenchmarkResult>,
    pub syntax_tokens: Vec<crate::ui::syntax_highlight::SyntaxToken>,
    #[cfg(feature = "pcre2-engine")]
    pub debug_session: Option<crate::engine::pcre2_debug::DebugSession>,
    #[cfg(feature = "pcre2-engine")]
    debug_cache: Option<crate::engine::pcre2_debug::DebugSession>,
    pub grex_result_tx: tokio::sync::mpsc::UnboundedSender<(u64, String)>,
    grex_result_rx: tokio::sync::mpsc::UnboundedReceiver<(u64, String)>,
    engine: Box<dyn RegexEngine>,
    compiled: Option<Box<dyn CompiledRegex>>,
}

impl App {
    pub const PANEL_REGEX: u8 = 0;
    pub const PANEL_TEST: u8 = 1;
    pub const PANEL_REPLACE: u8 = 2;
    pub const PANEL_MATCHES: u8 = 3;
    pub const PANEL_EXPLAIN: u8 = 4;
    pub const PANEL_COUNT: u8 = 5;
}

impl App {
    pub fn new(engine_kind: EngineKind, flags: EngineFlags) -> Self {
        let engine = engine::create_engine(engine_kind);
        let (grex_result_tx, grex_result_rx) = tokio::sync::mpsc::unbounded_channel();
        Self {
            regex_editor: Editor::new(),
            test_editor: Editor::new(),
            replace_editor: Editor::new(),
            focused_panel: 0,
            engine_kind,
            flags,
            matches: Vec::new(),
            replace_result: None,
            explanation: Vec::new(),
            error: None,
            overlay: OverlayState::default(),
            should_quit: false,
            scroll: ScrollState::default(),
            history: PatternHistory::default(),
            selection: MatchSelection::default(),
            status: StatusMessage::default(),
            show_whitespace: false,
            rounded_borders: false,
            vim_mode: false,
            vim_state: crate::input::vim::VimState::new(),
            compile_time: None,
            match_time: None,
            error_offset: None,
            output_on_quit: false,
            workspace_path: None,
            benchmark_results: Vec::new(),
            syntax_tokens: Vec::new(),
            #[cfg(feature = "pcre2-engine")]
            debug_session: None,
            #[cfg(feature = "pcre2-engine")]
            debug_cache: None,
            grex_result_tx,
            grex_result_rx,
            engine,
            compiled: None,
        }
    }

    pub fn set_replacement(&mut self, text: &str) {
        self.replace_editor = Editor::with_content(text.to_string());
        self.rereplace();
    }

    pub fn scroll_replace_up(&mut self) {
        self.scroll.replace_scroll = self.scroll.replace_scroll.saturating_sub(1);
    }

    pub fn scroll_replace_down(&mut self) {
        self.scroll.replace_scroll = self.scroll.replace_scroll.saturating_add(1);
    }

    pub fn rereplace(&mut self) {
        let template = self.replace_editor.content().to_string();
        if template.is_empty() || self.matches.is_empty() {
            self.replace_result = None;
            return;
        }
        let text = self.test_editor.content().to_string();
        self.replace_result = Some(engine::replace_all(&text, &self.matches, &template));
    }

    pub fn set_pattern(&mut self, pattern: &str) {
        self.regex_editor = Editor::with_content(pattern.to_string());
        self.recompute();
    }

    pub fn set_test_string(&mut self, text: &str) {
        self.test_editor = Editor::with_content(text.to_string());
        self.rematch();
    }

    pub fn switch_engine(&mut self) {
        self.engine_kind = self.engine_kind.next();
        self.engine = engine::create_engine(self.engine_kind);
        self.recompute();
    }

    /// Low-level engine setter. Does NOT call `recompute()` — the caller
    /// must trigger recompilation separately if needed.
    pub fn switch_engine_to(&mut self, kind: EngineKind) {
        self.engine_kind = kind;
        self.engine = engine::create_engine(kind);
    }

    pub fn scroll_match_up(&mut self) {
        self.scroll.match_scroll = self.scroll.match_scroll.saturating_sub(1);
    }

    pub fn scroll_match_down(&mut self) {
        self.scroll.match_scroll = self.scroll.match_scroll.saturating_add(1);
    }

    pub fn scroll_explain_up(&mut self) {
        self.scroll.explain_scroll = self.scroll.explain_scroll.saturating_sub(1);
    }

    pub fn scroll_explain_down(&mut self) {
        self.scroll.explain_scroll = self.scroll.explain_scroll.saturating_add(1);
    }

    pub fn recompute(&mut self) {
        let pattern = self.regex_editor.content().to_string();
        self.scroll.match_scroll = 0;
        self.scroll.explain_scroll = 0;
        self.error_offset = None;

        if pattern.is_empty() {
            self.compiled = None;
            self.matches.clear();
            self.explanation.clear();
            self.error = None;
            self.compile_time = None;
            self.match_time = None;
            self.syntax_tokens.clear();
            return;
        }

        // Auto-select engine: upgrade (never downgrade) if the pattern
        // requires a more powerful engine than the currently active one.
        let suggested = engine::detect_minimum_engine(&pattern);
        if engine::is_engine_upgrade(self.engine_kind, suggested) {
            let prev = self.engine_kind;
            self.engine_kind = suggested;
            self.engine = engine::create_engine(suggested);
            self.status.set(format!(
                "Auto-switched {} \u{2192} {} for this pattern",
                prev, suggested,
            ));
        }

        // Compile
        let compile_start = Instant::now();
        match self.engine.compile(&pattern, &self.flags) {
            Ok(compiled) => {
                self.compile_time = Some(compile_start.elapsed());
                self.compiled = Some(compiled);
                self.error = None;
            }
            Err(e) => {
                self.compile_time = Some(compile_start.elapsed());
                self.compiled = None;
                self.matches.clear();
                self.error = Some(e.to_string());
            }
        }

        // Rebuild syntax highlight tokens (pattern changed)
        self.syntax_tokens = crate::ui::syntax_highlight::highlight(&pattern);

        // Explain (uses regex-syntax, independent of engine)
        match explain::explain(&pattern) {
            Ok(nodes) => self.explanation = nodes,
            Err((msg, offset)) => {
                self.explanation.clear();
                if self.error_offset.is_none() {
                    self.error_offset = offset;
                }
                if self.error.is_none() {
                    self.error = Some(msg);
                }
            }
        }

        // Match
        self.rematch();
    }

    pub fn rematch(&mut self) {
        self.scroll.match_scroll = 0;
        self.selection.match_index = 0;
        self.selection.capture_index = None;
        if let Some(compiled) = &self.compiled {
            let text = self.test_editor.content().to_string();
            if text.is_empty() {
                self.matches.clear();
                self.replace_result = None;
                self.match_time = None;
                return;
            }
            let match_start = Instant::now();
            match compiled.find_matches(&text) {
                Ok(m) => {
                    self.match_time = Some(match_start.elapsed());
                    self.matches = m;
                }
                Err(e) => {
                    self.match_time = Some(match_start.elapsed());
                    self.matches.clear();
                    self.error = Some(e.to_string());
                }
            }
        } else {
            self.matches.clear();
            self.match_time = None;
        }
        self.rereplace();
    }

    // --- Pattern history ---

    pub fn commit_pattern_to_history(&mut self) {
        let pattern = self.regex_editor.content().to_string();
        if pattern.is_empty() {
            return;
        }
        if self.history.entries.back().map(|s| s.as_str()) == Some(&pattern) {
            return;
        }
        self.history.entries.push_back(pattern);
        if self.history.entries.len() > MAX_PATTERN_HISTORY {
            self.history.entries.pop_front();
        }
        self.history.index = None;
        self.history.temp = None;
    }

    pub fn history_prev(&mut self) {
        if self.history.entries.is_empty() {
            return;
        }
        let new_index = match self.history.index {
            Some(0) => return,
            Some(idx) => idx - 1,
            None => {
                self.history.temp = Some(self.regex_editor.content().to_string());
                self.history.entries.len() - 1
            }
        };
        self.history.index = Some(new_index);
        let pattern = self.history.entries[new_index].clone();
        self.regex_editor = Editor::with_content(pattern);
        self.recompute();
    }

    pub fn history_next(&mut self) {
        let idx = match self.history.index {
            Some(idx) => idx,
            None => return,
        };
        if idx + 1 < self.history.entries.len() {
            let new_index = idx + 1;
            self.history.index = Some(new_index);
            let pattern = self.history.entries[new_index].clone();
            self.regex_editor = Editor::with_content(pattern);
            self.recompute();
        } else {
            // Past end — restore temp
            self.history.index = None;
            let content = self.history.temp.take().unwrap_or_default();
            self.regex_editor = Editor::with_content(content);
            self.recompute();
        }
    }

    // --- Match selection + clipboard ---

    pub fn select_match_next(&mut self) {
        if self.matches.is_empty() {
            return;
        }
        match self.selection.capture_index {
            None => {
                let m = &self.matches[self.selection.match_index];
                if !m.captures.is_empty() {
                    self.selection.capture_index = Some(0);
                } else if self.selection.match_index + 1 < self.matches.len() {
                    self.selection.match_index += 1;
                }
            }
            Some(ci) => {
                let m = &self.matches[self.selection.match_index];
                if ci + 1 < m.captures.len() {
                    self.selection.capture_index = Some(ci + 1);
                } else if self.selection.match_index + 1 < self.matches.len() {
                    self.selection.match_index += 1;
                    self.selection.capture_index = None;
                }
            }
        }
        self.scroll_to_selected();
    }

    pub fn select_match_prev(&mut self) {
        if self.matches.is_empty() {
            return;
        }
        match self.selection.capture_index {
            Some(0) => {
                self.selection.capture_index = None;
            }
            Some(ci) => {
                self.selection.capture_index = Some(ci - 1);
            }
            None => {
                if self.selection.match_index > 0 {
                    self.selection.match_index -= 1;
                    let m = &self.matches[self.selection.match_index];
                    if !m.captures.is_empty() {
                        self.selection.capture_index = Some(m.captures.len() - 1);
                    }
                }
            }
        }
        self.scroll_to_selected();
    }

    fn scroll_to_selected(&mut self) {
        if self.matches.is_empty() || self.selection.match_index >= self.matches.len() {
            return;
        }
        let mut line = 0usize;
        for i in 0..self.selection.match_index {
            line += 1 + self.matches[i].captures.len();
        }
        if let Some(ci) = self.selection.capture_index {
            line += 1 + ci;
        }
        self.scroll.match_scroll = u16::try_from(line).unwrap_or(u16::MAX);
    }

    pub fn copy_selected_match(&mut self) {
        let text = self.selected_text();
        let Some(text) = text else { return };
        let msg = format!("Copied: \"{}\"", truncate(&text, 40));
        self.copy_to_clipboard(&text, &msg);
    }

    fn copy_to_clipboard(&mut self, text: &str, success_msg: &str) {
        match arboard::Clipboard::new() {
            Ok(mut cb) => match cb.set_text(text) {
                Ok(()) => self.status.set(success_msg.to_string()),
                Err(e) => self.status.set(format!("Clipboard error: {e}")),
            },
            Err(e) => self.status.set(format!("Clipboard error: {e}")),
        }
    }

    /// Print match results or replacement output to stdout.
    pub fn print_output(&self, group: Option<&str>, count: bool, color: bool) {
        if count {
            println!("{}", self.matches.len());
            return;
        }
        if let Some(ref result) = self.replace_result {
            if color {
                print_colored_replace(&result.output, &result.segments);
            } else {
                print!("{}", result.output);
            }
        } else if let Some(group_spec) = group {
            for m in &self.matches {
                if let Some(text) = engine::lookup_capture(m, group_spec) {
                    if color {
                        println!("{RED_BOLD}{text}{RESET}");
                    } else {
                        println!("{text}");
                    }
                } else {
                    eprintln!("rgx: group '{group_spec}' not found in match");
                }
            }
        } else if color {
            let text = self.test_editor.content();
            print_colored_matches(text, &self.matches);
        } else {
            for m in &self.matches {
                println!("{}", m.text);
            }
        }
    }

    /// Print matches as structured JSON.
    pub fn print_json_output(&self) {
        println!(
            "{}",
            serde_json::to_string_pretty(&self.matches).unwrap_or_else(|_| "[]".to_string())
        );
    }

    fn selected_text(&self) -> Option<String> {
        let m = self.matches.get(self.selection.match_index)?;
        match self.selection.capture_index {
            None => Some(m.text.clone()),
            Some(ci) => m.captures.get(ci).map(|c| c.text.clone()),
        }
    }

    /// Apply a mutating editor operation to the currently focused editor panel,
    /// then trigger the appropriate recompute/rematch/rereplace.
    pub fn edit_focused(&mut self, f: impl FnOnce(&mut Editor)) {
        match self.focused_panel {
            Self::PANEL_REGEX => {
                f(&mut self.regex_editor);
                self.recompute();
            }
            Self::PANEL_TEST => {
                f(&mut self.test_editor);
                self.rematch();
            }
            Self::PANEL_REPLACE => {
                f(&mut self.replace_editor);
                self.rereplace();
            }
            _ => {}
        }
    }

    /// Apply a non-mutating cursor movement to the currently focused editor panel.
    pub fn move_focused(&mut self, f: impl FnOnce(&mut Editor)) {
        match self.focused_panel {
            Self::PANEL_REGEX => f(&mut self.regex_editor),
            Self::PANEL_TEST => f(&mut self.test_editor),
            Self::PANEL_REPLACE => f(&mut self.replace_editor),
            _ => {}
        }
    }

    pub fn run_benchmark(&mut self) {
        let pattern = self.regex_editor.content().to_string();
        let text = self.test_editor.content().to_string();
        if pattern.is_empty() || text.is_empty() {
            return;
        }

        let mut results = Vec::new();
        for kind in EngineKind::all() {
            let eng = engine::create_engine(kind);
            let compile_start = Instant::now();
            let compiled = match eng.compile(&pattern, &self.flags) {
                Ok(c) => c,
                Err(e) => {
                    results.push(BenchmarkResult {
                        engine: kind,
                        compile_time: compile_start.elapsed(),
                        match_time: Duration::ZERO,
                        match_count: 0,
                        error: Some(e.to_string()),
                    });
                    continue;
                }
            };
            let compile_time = compile_start.elapsed();
            let match_start = Instant::now();
            let (match_count, error) = match compiled.find_matches(&text) {
                Ok(matches) => (matches.len(), None),
                Err(e) => (0, Some(e.to_string())),
            };
            results.push(BenchmarkResult {
                engine: kind,
                compile_time,
                match_time: match_start.elapsed(),
                match_count,
                error,
            });
        }
        self.benchmark_results = results;
        self.overlay.benchmark = true;
    }

    /// Generate a regex101.com URL from the current state.
    pub fn regex101_url(&self) -> String {
        let pattern = self.regex_editor.content();
        let test_string = self.test_editor.content();

        let flavor = match self.engine_kind {
            #[cfg(feature = "pcre2-engine")]
            EngineKind::Pcre2 => "pcre2",
            _ => "ecmascript",
        };

        let mut flags = String::from("g");
        if self.flags.case_insensitive {
            flags.push('i');
        }
        if self.flags.multi_line {
            flags.push('m');
        }
        if self.flags.dot_matches_newline {
            flags.push('s');
        }
        if self.flags.unicode {
            flags.push('u');
        }
        if self.flags.extended {
            flags.push('x');
        }

        format!(
            "https://regex101.com/?regex={}&testString={}&flags={}&flavor={}",
            url_encode(pattern),
            url_encode(test_string),
            url_encode(&flags),
            flavor,
        )
    }

    /// Copy regex101 URL to clipboard.
    pub fn copy_regex101_url(&mut self) {
        let url = self.regex101_url();
        self.copy_to_clipboard(&url, "regex101 URL copied to clipboard");
    }

    /// Generate code for the current pattern in the given language and copy to clipboard.
    pub fn generate_code(&mut self, lang: &crate::codegen::Language) {
        let pattern = self.regex_editor.content().to_string();
        if pattern.is_empty() {
            self.status
                .set("No pattern to generate code for".to_string());
            return;
        }
        let code = crate::codegen::generate_code(lang, &pattern, &self.flags);
        self.copy_to_clipboard(&code, &format!("{} code copied to clipboard", lang));
        self.overlay.codegen = false;
    }

    #[cfg(feature = "pcre2-engine")]
    pub fn start_debug(&mut self, max_steps: usize) {
        use crate::engine::pcre2_debug::{self, DebugSession};

        let pattern = self.regex_editor.content().to_string();
        let subject = self.test_editor.content().to_string();
        if pattern.is_empty() || subject.is_empty() {
            self.status
                .set("Debugger needs both a pattern and test string".to_string());
            return;
        }

        if self.engine_kind != EngineKind::Pcre2 {
            self.switch_engine_to(EngineKind::Pcre2);
            self.recompute();
        }

        // Restore cached session if pattern and subject haven't changed,
        // preserving the user's step position and heatmap toggle.
        if let Some(ref cached) = self.debug_cache {
            if cached.pattern == pattern && cached.subject == subject {
                self.debug_session = self.debug_cache.take();
                return;
            }
        }

        let start_offset = self.selected_match_start();

        match pcre2_debug::debug_match(&pattern, &subject, &self.flags, max_steps, start_offset) {
            Ok(trace) => {
                self.debug_session = Some(DebugSession {
                    trace,
                    step: 0,
                    show_heatmap: false,
                    pattern,
                    subject,
                });
            }
            Err(e) => {
                self.status.set(format!("Debugger error: {e}"));
            }
        }
    }

    #[cfg(not(feature = "pcre2-engine"))]
    pub fn start_debug(&mut self, _max_steps: usize) {
        self.status
            .set("Debugger requires PCRE2 (build with --features pcre2-engine)".to_string());
    }

    #[cfg(feature = "pcre2-engine")]
    fn selected_match_start(&self) -> usize {
        if !self.matches.is_empty() && self.selection.match_index < self.matches.len() {
            self.matches[self.selection.match_index].start
        } else {
            0
        }
    }

    #[cfg(feature = "pcre2-engine")]
    pub fn close_debug(&mut self) {
        self.debug_cache = self.debug_session.take();
    }

    pub fn debug_step_forward(&mut self) {
        #[cfg(feature = "pcre2-engine")]
        if let Some(ref mut s) = self.debug_session {
            if s.step + 1 < s.trace.steps.len() {
                s.step += 1;
            }
        }
    }

    pub fn debug_step_back(&mut self) {
        #[cfg(feature = "pcre2-engine")]
        if let Some(ref mut s) = self.debug_session {
            s.step = s.step.saturating_sub(1);
        }
    }

    pub fn debug_jump_start(&mut self) {
        #[cfg(feature = "pcre2-engine")]
        if let Some(ref mut s) = self.debug_session {
            s.step = 0;
        }
    }

    pub fn debug_jump_end(&mut self) {
        #[cfg(feature = "pcre2-engine")]
        if let Some(ref mut s) = self.debug_session {
            if !s.trace.steps.is_empty() {
                s.step = s.trace.steps.len() - 1;
            }
        }
    }

    pub fn debug_next_match(&mut self) {
        #[cfg(feature = "pcre2-engine")]
        if let Some(ref mut s) = self.debug_session {
            let current_attempt = s
                .trace
                .steps
                .get(s.step)
                .map(|st| st.match_attempt)
                .unwrap_or(0);
            for (i, step) in s.trace.steps.iter().enumerate().skip(s.step + 1) {
                if step.match_attempt > current_attempt {
                    s.step = i;
                    return;
                }
            }
        }
    }

    pub fn debug_next_backtrack(&mut self) {
        #[cfg(feature = "pcre2-engine")]
        if let Some(ref mut s) = self.debug_session {
            for (i, step) in s.trace.steps.iter().enumerate().skip(s.step + 1) {
                if step.is_backtrack {
                    s.step = i;
                    return;
                }
            }
        }
    }

    pub fn debug_toggle_heatmap(&mut self) {
        #[cfg(feature = "pcre2-engine")]
        if let Some(ref mut s) = self.debug_session {
            s.show_heatmap = !s.show_heatmap;
        }
    }

    pub fn handle_action(&mut self, action: Action, debug_max_steps: usize) {
        match action {
            Action::Quit => {
                self.should_quit = true;
            }
            Action::OutputAndQuit => {
                self.output_on_quit = true;
                self.should_quit = true;
            }
            Action::SwitchPanel => {
                if self.focused_panel == Self::PANEL_REGEX {
                    self.commit_pattern_to_history();
                }
                self.focused_panel = (self.focused_panel + 1) % Self::PANEL_COUNT;
            }
            Action::SwitchPanelBack => {
                if self.focused_panel == Self::PANEL_REGEX {
                    self.commit_pattern_to_history();
                }
                self.focused_panel =
                    (self.focused_panel + Self::PANEL_COUNT - 1) % Self::PANEL_COUNT;
            }
            Action::SwitchEngine => {
                self.switch_engine();
            }
            Action::Undo => {
                if self.focused_panel == Self::PANEL_REGEX && self.regex_editor.undo() {
                    self.recompute();
                } else if self.focused_panel == Self::PANEL_TEST && self.test_editor.undo() {
                    self.rematch();
                } else if self.focused_panel == Self::PANEL_REPLACE && self.replace_editor.undo() {
                    self.rereplace();
                }
            }
            Action::Redo => {
                if self.focused_panel == Self::PANEL_REGEX && self.regex_editor.redo() {
                    self.recompute();
                } else if self.focused_panel == Self::PANEL_TEST && self.test_editor.redo() {
                    self.rematch();
                } else if self.focused_panel == Self::PANEL_REPLACE && self.replace_editor.redo() {
                    self.rereplace();
                }
            }
            Action::HistoryPrev => {
                if self.focused_panel == Self::PANEL_REGEX {
                    self.history_prev();
                }
            }
            Action::HistoryNext => {
                if self.focused_panel == Self::PANEL_REGEX {
                    self.history_next();
                }
            }
            Action::CopyMatch => {
                if self.focused_panel == Self::PANEL_MATCHES {
                    self.copy_selected_match();
                }
            }
            Action::ToggleWhitespace => {
                self.show_whitespace = !self.show_whitespace;
            }
            Action::ToggleCaseInsensitive => {
                self.flags.toggle_case_insensitive();
                self.recompute();
            }
            Action::ToggleMultiLine => {
                self.flags.toggle_multi_line();
                self.recompute();
            }
            Action::ToggleDotAll => {
                self.flags.toggle_dot_matches_newline();
                self.recompute();
            }
            Action::ToggleUnicode => {
                self.flags.toggle_unicode();
                self.recompute();
            }
            Action::ToggleExtended => {
                self.flags.toggle_extended();
                self.recompute();
            }
            Action::ShowHelp => {
                self.overlay.help = true;
            }
            Action::OpenRecipes => {
                self.overlay.recipes = true;
                self.overlay.recipe_index = 0;
            }
            Action::OpenGrex => {
                self.overlay.grex = Some(crate::ui::grex_overlay::GrexOverlayState::default());
            }
            Action::Benchmark => {
                self.run_benchmark();
            }
            Action::ExportRegex101 => {
                self.copy_regex101_url();
            }
            Action::GenerateCode => {
                self.overlay.codegen = true;
                self.overlay.codegen_language_index = 0;
            }
            Action::InsertChar(c) => self.edit_focused(|ed| ed.insert_char(c)),
            Action::InsertNewline => {
                if self.focused_panel == Self::PANEL_TEST {
                    self.test_editor.insert_newline();
                    self.rematch();
                }
            }
            Action::DeleteBack => self.edit_focused(Editor::delete_back),
            Action::DeleteForward => self.edit_focused(Editor::delete_forward),
            Action::MoveCursorLeft => self.move_focused(Editor::move_left),
            Action::MoveCursorRight => self.move_focused(Editor::move_right),
            Action::MoveCursorWordLeft => self.move_focused(Editor::move_word_left),
            Action::MoveCursorWordRight => self.move_focused(Editor::move_word_right),
            Action::ScrollUp => match self.focused_panel {
                Self::PANEL_TEST => self.test_editor.move_up(),
                Self::PANEL_MATCHES => self.select_match_prev(),
                Self::PANEL_EXPLAIN => self.scroll_explain_up(),
                _ => {}
            },
            Action::ScrollDown => match self.focused_panel {
                Self::PANEL_TEST => self.test_editor.move_down(),
                Self::PANEL_MATCHES => self.select_match_next(),
                Self::PANEL_EXPLAIN => self.scroll_explain_down(),
                _ => {}
            },
            Action::MoveCursorHome => self.move_focused(Editor::move_home),
            Action::MoveCursorEnd => self.move_focused(Editor::move_end),
            Action::DeleteCharAtCursor => self.edit_focused(Editor::delete_char_at_cursor),
            Action::DeleteLine => self.edit_focused(Editor::delete_line),
            Action::ChangeLine => self.edit_focused(Editor::clear_line),
            Action::OpenLineBelow => {
                if self.focused_panel == Self::PANEL_TEST {
                    self.test_editor.open_line_below();
                    self.rematch();
                } else {
                    self.vim_state.cancel_insert();
                }
            }
            Action::OpenLineAbove => {
                if self.focused_panel == Self::PANEL_TEST {
                    self.test_editor.open_line_above();
                    self.rematch();
                } else {
                    self.vim_state.cancel_insert();
                }
            }
            Action::MoveToFirstNonBlank => self.move_focused(Editor::move_to_first_non_blank),
            Action::MoveToFirstLine => self.move_focused(Editor::move_to_first_line),
            Action::MoveToLastLine => self.move_focused(Editor::move_to_last_line),
            Action::MoveCursorWordForwardEnd => self.move_focused(Editor::move_word_forward_end),
            Action::EnterInsertMode => {}
            Action::EnterInsertModeAppend => self.move_focused(Editor::move_right),
            Action::EnterInsertModeLineStart => self.move_focused(Editor::move_to_first_non_blank),
            Action::EnterInsertModeLineEnd => self.move_focused(Editor::move_end),
            Action::EnterNormalMode => self.move_focused(Editor::move_left_in_line),
            Action::PasteClipboard => {
                if let Ok(mut cb) = arboard::Clipboard::new() {
                    if let Ok(text) = cb.get_text() {
                        self.edit_focused(|ed| ed.insert_str(&text));
                    }
                }
            }
            Action::ToggleDebugger => {
                #[cfg(feature = "pcre2-engine")]
                if self.debug_session.is_some() {
                    self.close_debug();
                } else {
                    self.start_debug(debug_max_steps);
                }
                #[cfg(not(feature = "pcre2-engine"))]
                self.start_debug(debug_max_steps);
            }
            Action::SaveWorkspace | Action::None => {}
        }
    }

    /// If the grex overlay has a pending debounce deadline that has passed, spawn a
    /// blocking task to regenerate the pattern with the current options. Results are
    /// delivered via `grex_result_tx` and claimed later by `drain_grex_results`.
    pub fn maybe_run_grex_generation(&mut self) {
        let Some(overlay) = self.overlay.grex.as_mut() else {
            return;
        };
        let Some(deadline) = overlay.debounce_deadline else {
            return;
        };
        if std::time::Instant::now() < deadline {
            return;
        }
        overlay.debounce_deadline = None;
        overlay.generation_counter += 1;
        let counter = overlay.generation_counter;
        let examples: Vec<String> = overlay
            .editor
            .content()
            .lines()
            .filter(|l| !l.is_empty())
            .map(|l| l.to_string())
            .collect();
        let options = overlay.options;
        let tx = self.grex_result_tx.clone();

        tokio::task::spawn_blocking(move || {
            let pattern = crate::grex_integration::generate(&examples, options);
            let _ = tx.send((counter, pattern));
        });
    }

    /// Drain any grex generation results that arrived since the last tick, applying
    /// only the result that matches the current generation counter.
    pub fn drain_grex_results(&mut self) {
        while let Ok((counter, pattern)) = self.grex_result_rx.try_recv() {
            if let Some(overlay) = self.overlay.grex.as_mut() {
                if counter == overlay.generation_counter {
                    overlay.generated_pattern = Some(pattern);
                }
            }
        }
    }

    /// Dispatch a key event to the grex overlay. Returns true if the key was consumed.
    /// Caller should only invoke this when `self.overlay.grex.is_some()`.
    pub fn dispatch_grex_overlay_key(&mut self, key: crossterm::event::KeyEvent) -> bool {
        use crossterm::event::{KeyCode, KeyModifiers};
        const DEBOUNCE_MS: u64 = 150;
        let debounce = std::time::Duration::from_millis(DEBOUNCE_MS);

        let Some(overlay) = self.overlay.grex.as_mut() else {
            return false;
        };

        // Accept / cancel first — these take precedence regardless of other modifiers.
        match key.code {
            KeyCode::Esc => {
                self.overlay.grex = None;
                return true;
            }
            KeyCode::Tab => {
                let pattern = overlay
                    .generated_pattern
                    .as_deref()
                    .filter(|p| !p.is_empty())
                    .map(str::to_string);
                if let Some(pattern) = pattern {
                    self.set_pattern(&pattern);
                    self.overlay.grex = None;
                }
                return true;
            }
            _ => {}
        }

        // Flag toggles (Alt+d/a/c). These reset the debounce so the new flags regenerate.
        if key.modifiers.contains(KeyModifiers::ALT) {
            match key.code {
                KeyCode::Char('d') => {
                    overlay.options.digit = !overlay.options.digit;
                    overlay.debounce_deadline = Some(std::time::Instant::now() + debounce);
                    return true;
                }
                KeyCode::Char('a') => {
                    overlay.options.anchors = !overlay.options.anchors;
                    overlay.debounce_deadline = Some(std::time::Instant::now() + debounce);
                    return true;
                }
                KeyCode::Char('c') => {
                    overlay.options.case_insensitive = !overlay.options.case_insensitive;
                    overlay.debounce_deadline = Some(std::time::Instant::now() + debounce);
                    return true;
                }
                _ => {}
            }
        }

        // Editor input — dispatch a focused set of keys to the overlay editor.
        let mut consumed = true;
        match key.code {
            KeyCode::Char(c) if !key.modifiers.contains(KeyModifiers::CONTROL) => {
                overlay.editor.insert_char(c);
            }
            KeyCode::Enter => overlay.editor.insert_newline(),
            KeyCode::Backspace => overlay.editor.delete_back(),
            KeyCode::Delete => overlay.editor.delete_forward(),
            KeyCode::Left if key.modifiers.contains(KeyModifiers::CONTROL) => {
                overlay.editor.move_word_left();
            }
            KeyCode::Right if key.modifiers.contains(KeyModifiers::CONTROL) => {
                overlay.editor.move_word_right();
            }
            KeyCode::Left => overlay.editor.move_left(),
            KeyCode::Right => overlay.editor.move_right(),
            KeyCode::Up => overlay.editor.move_up(),
            KeyCode::Down => overlay.editor.move_down(),
            KeyCode::Home => overlay.editor.move_home(),
            KeyCode::End => overlay.editor.move_end(),
            _ => consumed = false,
        }

        if consumed {
            overlay.debounce_deadline = Some(std::time::Instant::now() + debounce);
        }
        consumed
    }
}

fn url_encode(s: &str) -> String {
    let mut out = String::with_capacity(s.len() * 3);
    for b in s.bytes() {
        match b {
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
                out.push(b as char);
            }
            _ => {
                out.push_str(&format!("%{b:02X}"));
            }
        }
    }
    out
}

fn print_colored_matches(text: &str, matches: &[engine::Match]) {
    let mut pos = 0;
    for m in matches {
        if m.start > pos {
            print!("{}", &text[pos..m.start]);
        }
        print!("{RED_BOLD}{}{RESET}", &text[m.start..m.end]);
        pos = m.end;
    }
    if pos < text.len() {
        print!("{}", &text[pos..]);
    }
    if !text.ends_with('\n') {
        println!();
    }
}

/// Print replacement output with replaced segments highlighted.
fn print_colored_replace(output: &str, segments: &[engine::ReplaceSegment]) {
    for seg in segments {
        let chunk = &output[seg.start..seg.end];
        if seg.is_replacement {
            print!("{GREEN_BOLD}{chunk}{RESET}");
        } else {
            print!("{chunk}");
        }
    }
    if !output.ends_with('\n') {
        println!();
    }
}