thag_rs 0.1.1

A versatile script runner and REPL for Rust snippets, expressions and programs
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
#![allow(clippy::uninlined_format_args)]
use crate::cmd_args::{Cli, ProcFlags};
use crate::code_utils::{self, clean_up, display_dir_contents, extract_ast, extract_manifest};
use crate::debug_log;
use crate::errors::BuildRunError;
use crate::log;
use crate::logging::Verbosity;
use crate::shared::Ast;
use crate::{
    colors::{nu_resolve_style, MessageLevel},
    gen_build_run, nu_color_println,
    shared::BuildState,
};

use clap::ArgMatches;
use clap::{CommandFactory, Parser};
use lazy_static::lazy_static;
use reedline::{
    default_emacs_keybindings, ColumnarMenu, DefaultCompleter, DefaultHinter, DefaultValidator,
    EditCommand, Emacs, FileBackedHistory, KeyCode, KeyModifiers, Keybindings, MenuBuilder, Prompt,
    PromptEditMode, PromptHistorySearch, PromptHistorySearchStatus, Reedline, ReedlineEvent,
    ReedlineMenu, Signal,
};
use regex::Regex;
use std::borrow::Cow;
use std::collections::HashMap;
use std::error::Error;
use std::fs::OpenOptions;
use std::str::FromStr;
use std::time::Instant;
use strum::{EnumIter, EnumString, IntoEnumIterator, IntoStaticStr};

const HISTORY_FILE: &str = "thag_repl_hist.txt";
pub static DEFAULT_MULTILINE_INDICATOR: &str = "";

#[derive(Debug, Parser, EnumIter, EnumString, IntoStaticStr)]
#[command(
    name = "",
    disable_help_flag = true,
    disable_help_subcommand = true,
    verbatim_doc_comment
)] // Disable automatic help subcommand and flag
#[strum(serialize_all = "kebab-case")]
/// REPL mode lets you type or paste a Rust expression to be evaluated.
/// Start by choosing the eval option and entering your expression. Expressions between matching braces,
/// brackets, parens or quotes may span multiple lines.
/// If valid, the expression will be converted into a Rust program, and built and run using Cargo.
/// Dependencies will be inferred from imports if possible using a Cargo search, but the overhead
/// of doing so can be avoided by placing them in Cargo.toml format at the top of the expression in a
/// comment block of the form
/// /*[toml]
/// [dependencies]
/// ...
/// */
/// From here they will be extracted to a dedicated Cargo.toml file.
/// In this case the whole expression must be enclosed in curly braces to include the TOML in the expression.
/// At any stage before exiting the REPL, or at least as long as your TMPDIR is not cleared, you can
/// go back and edit your expression or its generated Cargo.toml file and copy or save them from the
/// editor or directly from their temporary disk locations.
/// The tab key will show command selections and complete partial matching selections."
enum ReplCommand {
    /// Show the REPL banner
    Banner,
    /// Edit the Rust expression. Edit+run can also be used as an alternative to eval for longer snippets and programs.
    Edit,
    /// Edit the generated Cargo.toml
    Toml,
    /// Attempt to build and run the Rust expression
    Run,
    /// Delete all temporary files for this eval (see list)
    Delete,
    /// List temporary files for this eval
    List,
    /// Edit history
    History,
    /// Show help information
    Help,
    /// Show key bindings
    Keys,
    /// Exit the REPL
    Quit,
}

impl ReplCommand {
    fn print_help() {
        let mut command = ReplCommand::command();
        // let mut buf = Vec::new();
        // command.write_help(&mut buf).unwrap();
        // let help_message = String::from_utf8(buf).unwrap();
        println!("{}", command.render_long_help());
    }
}

/// A struct to allow sharing of necessary context between functions
#[derive(Debug)]
pub struct Context<'a> {
    pub options: &'a mut Cli,
    pub proc_flags: &'a ProcFlags,
    pub build_state: &'a mut BuildState,
    pub start: Instant,
}

/// A struct to implement the Prompt trait.
#[allow(clippy::module_name_repetitions)]
pub struct ReplPrompt(pub &'static str);
impl Prompt for ReplPrompt {
    fn render_prompt_left(&self) -> Cow<str> {
        Cow::Owned(self.0.to_string())
    }

    fn render_prompt_right(&self) -> Cow<str> {
        Cow::Owned(String::new())
    }

    fn render_prompt_indicator(&self, _edit_mode: PromptEditMode) -> Cow<str> {
        Cow::Owned("> ".to_string())
    }

    fn render_prompt_multiline_indicator(&self) -> Cow<str> {
        Cow::Borrowed(DEFAULT_MULTILINE_INDICATOR)
    }

    fn render_prompt_history_search_indicator(
        &self,
        history_search: PromptHistorySearch,
    ) -> Cow<str> {
        let prefix = match history_search.status {
            PromptHistorySearchStatus::Passing => "",
            PromptHistorySearchStatus::Failing => "failing ",
        };

        Cow::Owned(format!(
            "({}reverse-search: {}) ",
            prefix, history_search.term
        ))
    }
}

fn add_menu_keybindings(keybindings: &mut Keybindings) {
    keybindings.add_binding(
        KeyModifiers::NONE,
        KeyCode::Tab,
        ReedlineEvent::UntilFound(vec![
            ReedlineEvent::Menu("completion_menu".to_string()),
            ReedlineEvent::MenuNext,
        ]),
    );
    keybindings.add_binding(
        KeyModifiers::ALT,
        KeyCode::Enter,
        ReedlineEvent::Edit(vec![EditCommand::InsertNewline]),
    );
}

// fn get_emacs_keybindings() {
//     println!("\n--Default Keybindings--");
//     for (mode, modifier, code, event) in get_reedline_default_keybindings() {
//         if mode == "emacs" {
//             println!("mode: {mode}, keymodifiers: {modifier}, keycode: {code}, event: {event}");
//         }
//     }
//     println!();
// }

/// Run the REPL.
/// # Errors
/// Will return `Err` if there is any error in running the REPL.
/// # Panics
/// Will panic if there is a problem configuring the `reedline` history file.
#[allow(clippy::module_name_repetitions)]
#[allow(clippy::too_many_lines)]
pub fn run_repl(
    options: &mut Cli,
    proc_flags: &ProcFlags,
    build_state: &mut BuildState,
    start: Instant,
) -> Result<(), Box<dyn Error>> {
    #[allow(unused_variables)]
    let mut context = Context {
        options,
        proc_flags,
        build_state,
        start,
    };
    // get_emacs_keybindings();
    let context: &mut Context = &mut context;
    let history_file = context.build_state.cargo_home.clone().join(HISTORY_FILE);
    let history = Box::new(
        FileBackedHistory::with_file(25, history_file)
            .expect("Error configuring history with file"),
    );

    let cmd_vec = ReplCommand::iter()
        .map(<ReplCommand as Into<&'static str>>::into)
        .map(String::from)
        .collect::<Vec<String>>();

    let completer = Box::new(DefaultCompleter::new_with_wordlen(cmd_vec.clone(), 2));

    // Use the interactive menu to select options from the completer
    let columnar_menu = ColumnarMenu::default()
        .with_name("completion_menu")
        .with_columns(4)
        .with_column_width(None)
        .with_column_padding(2);

    let completion_menu = Box::new(columnar_menu);

    let mut keybindings = default_emacs_keybindings();
    // keybindings.add_binding(
    //     KeyModifiers::CONTROL,
    //     KeyCode::Char('b'),
    //     ReedlineEvent::Edit(vec![EditCommand::SwapWords]),
    // );
    add_menu_keybindings(&mut keybindings);
    // println!("{:#?}", keybindings.get_keybindings());

    let edit_mode = Box::new(Emacs::new(keybindings.clone()));

    // let highlighter = Box::<ExampleHighlighter>::default();
    let mut line_editor = Reedline::create()
        .with_validator(Box::new(DefaultValidator))
        .with_hinter(Box::new(
            DefaultHinter::default().with_style(nu_resolve_style(MessageLevel::Ghost).italic()),
        ))
        .with_history(history)
        // .with_highlighter(highlighter)
        .with_completer(completer)
        .with_menu(ReedlineMenu::EngineCompleter(completion_menu))
        .with_edit_mode(edit_mode);

    let bindings = keybindings.get_keybindings();

    let prompt = ReplPrompt("repl");

    let cmd_list = cmd_vec.join(", ");

    disp_repl_banner(&cmd_list);
    loop {
        let sig = line_editor.read_line(&prompt)?;
        let input: &str = match sig {
            Signal::Success(ref buffer) => buffer,
            Signal::CtrlD | Signal::CtrlC => {
                break;
            }
        };

        // Process user input (line)

        let rs_source = input.trim();
        if rs_source.is_empty() {
            continue;
        }

        let (first_word, rest) = parse_line(rs_source);
        let maybe_cmd = {
            let mut matches = 0;
            let mut cmd = String::new();
            for key in &cmd_vec {
                if key.starts_with(&first_word) {
                    matches += 1;
                    // Selects last match
                    if matches == 1 {
                        cmd = key.to_string();
                    }
                    // eprintln!("key={key}, split[0]={}", split[0]);
                }
            }
            if matches == 1 {
                Some(cmd)
            } else {
                // println!("No single matching key found");
                None
            }
        };

        if let Some(cmd) = maybe_cmd {
            if let Ok(repl_command) = ReplCommand::from_str(&cmd) {
                let args = clap::Command::new("")
                    .no_binary_name(true)
                    .try_get_matches_from_mut(rest)?;
                match repl_command {
                    ReplCommand::Banner => disp_repl_banner(&cmd_list),
                    ReplCommand::Help => {
                        ReplCommand::print_help();
                    }
                    ReplCommand::Quit => {
                        break;
                    }
                    ReplCommand::Edit => {
                        edit(&args, context)?;
                    }
                    ReplCommand::Toml => {
                        toml(&args, context)?;
                    }
                    ReplCommand::Run => {
                        // &history.sync();
                        run_expr(&args, context)?;
                    }
                    ReplCommand::Delete => {
                        delete(&args, context)?;
                    }
                    ReplCommand::List => {
                        list(&args, context)?;
                    }
                    ReplCommand::History => {
                        edit_history(&args, context)?;
                    }
                    ReplCommand::Keys => {
                        // Calculate max command len for padding
                        // Can't extract this to a method because for some reason KeyCmmbination is not exposed.
                        let max_cmd_len = {
                            // Determine the length of the longest command for padding
                            let max_cmd_len = bindings
                                .values()
                                .map(|reedline_event| {
                                    if let ReedlineEvent::Edit(edit_cmds) = reedline_event {
                                        edit_cmds
                                            .iter()
                                            .map(|cmd| {
                                                let key_desc =
                                                    nu_resolve_style(MessageLevel::Subheading)
                                                        .paint(format!("{cmd:?}"));
                                                let key_desc = format!("{key_desc}");
                                                key_desc.len()
                                            })
                                            .max()
                                            .unwrap_or(0)
                                    } else if !format!("{reedline_event}").starts_with("UntilFound")
                                    {
                                        let event_desc = nu_resolve_style(MessageLevel::Subheading)
                                            .paint(format!("{reedline_event:?}"));
                                        let event_desc = format!("{event_desc}");
                                        event_desc.len()
                                    } else {
                                        0
                                    }
                                })
                                .max()
                                .unwrap_or(0);
                            // Add 2 bytes of padding
                            max_cmd_len + 2
                        };

                        // Collect and format key bindings
                        // Can't extract this to a method either, because KeyCmmbination is not exposed.
                        let mut formatted_bindings = {
                            let mut formatted_bindings = Vec::new();
                            for (key_combination, reedline_event) in bindings {
                                let key_modifiers = key_combination.modifier;
                                let key_code = key_combination.key_code;
                                let modifier = format_key_modifier(key_modifiers);
                                let key = format_key_code(key_code);
                                let key_desc = format!("{}{}", modifier, key);
                                if let ReedlineEvent::Edit(edit_cmds) = reedline_event {
                                    let cmd_desc = format_edit_commands(edit_cmds, max_cmd_len);
                                    formatted_bindings.push((key_desc.clone(), cmd_desc));
                                } else {
                                    let event_name = format!("{reedline_event:?}");
                                    if !event_name.starts_with("UntilFound") {
                                        let event_desc =
                                            format_non_edit_events(&event_name, max_cmd_len);
                                        formatted_bindings.push((key_desc.clone(), event_desc));
                                    }
                                }
                            }
                            formatted_bindings
                        };

                        // Sort the formatted bindings alphabetically by key combination description
                        formatted_bindings.sort_by(|a, b| a.0.cmp(&b.0));

                        // Determine the length of the longest key description for padding
                        let max_key_len = formatted_bindings
                            .iter()
                            .map(|(key_desc, _)| {
                                let key_desc =
                                    nu_resolve_style(MessageLevel::Heading).paint(key_desc);
                                let key_desc = format!("{key_desc}");
                                key_desc.len()
                            })
                            .max()
                            .unwrap_or(0);
                        // eprintln!("max_key_len={max_key_len}");

                        show_key_bindings(formatted_bindings, max_key_len);
                    }
                }
                continue;
            }
        }

        let rs_manifest = extract_manifest(rs_source, Instant::now())
            .map_err(|_err| BuildRunError::FromStr("Error parsing rs_source".to_string()))?;
        context.build_state.rs_manifest = Some(rs_manifest);

        let maybe_ast = extract_ast(rs_source);

        if let Ok(expr_ast) = maybe_ast {
            code_utils::process_expr(
                &expr_ast,
                context.build_state,
                rs_source,
                context.options,
                context.proc_flags,
                &context.start,
            )
            .map_err(|_err| BuildRunError::Command("Error processing expression".to_string()))?;
        } else {
            nu_color_println!(
                nu_resolve_style(MessageLevel::Error),
                "Error parsing code: {maybe_ast:#?}"
            );
        }
    }
    Ok(())
}

fn show_key_bindings(formatted_bindings: Vec<(String, String)>, max_key_len: usize) {
    println!();
    nu_color_println!(
        nu_resolve_style(crate::MessageLevel::Emphasis),
        "Key bindings - subject to your terminal settings"
    );

    // Print the formatted and sorted key bindings
    for (key_desc, cmd_desc) in formatted_bindings {
        let key_desc = nu_resolve_style(MessageLevel::Heading).paint(key_desc);
        let key_desc = format!("{key_desc}");
        println!("{:<width$}    {}", key_desc, cmd_desc, width = max_key_len);
    }
    println!();
}

// Helper function to convert KeyModifiers to string
fn format_key_modifier(modifier: KeyModifiers) -> String {
    let mut modifiers = Vec::new();
    if modifier.contains(KeyModifiers::CONTROL) {
        modifiers.push("CONTROL");
    }
    if modifier.contains(KeyModifiers::SHIFT) {
        modifiers.push("SHIFT");
    }
    if modifier.contains(KeyModifiers::ALT) {
        modifiers.push("ALT");
    }
    let mods_str = modifiers.join("+");
    if modifiers.is_empty() {
        mods_str
    } else {
        mods_str + "-"
    }
}

// Helper function to convert KeyCode to string
fn format_key_code(key_code: KeyCode) -> String {
    match key_code {
        KeyCode::Backspace => "Backspace".to_string(),
        KeyCode::Enter => "Enter".to_string(),
        KeyCode::Left => "Left".to_string(),
        KeyCode::Right => "Right".to_string(),
        KeyCode::Up => "Up".to_string(),
        KeyCode::Down => "Down".to_string(),
        KeyCode::Home => "Home".to_string(),
        KeyCode::End => "End".to_string(),
        KeyCode::PageUp => "PageUp".to_string(),
        KeyCode::PageDown => "PageDown".to_string(),
        KeyCode::Tab => "Tab".to_string(),
        KeyCode::BackTab => "BackTab".to_string(),
        KeyCode::Delete => "Delete".to_string(),
        KeyCode::Insert => "Insert".to_string(),
        KeyCode::F(num) => format!("F{}", num),
        KeyCode::Char(c) => format!("{}", c.to_uppercase()),
        KeyCode::Null => "Null".to_string(),
        KeyCode::Esc => "Esc".to_string(),
        KeyCode::CapsLock => "CapsLock".to_string(),
        KeyCode::ScrollLock => "ScrollLock".to_string(),
        KeyCode::NumLock => "NumLock".to_string(),
        KeyCode::PrintScreen => "PrintScreen".to_string(),
        KeyCode::Pause => "Pause".to_string(),
        KeyCode::Menu => "Menu".to_string(),
        KeyCode::KeypadBegin => "KeypadBegin".to_string(),
        KeyCode::Media(media) => format!("Media({:?})", media),
        KeyCode::Modifier(modifier) => format!("Modifier({:?})", modifier),
    }
}

// Helper function to format ReedlineEvents other than Edit, and their doc comments
#[allow(clippy::too_many_lines)]
fn format_non_edit_events(event_name: &str, max_cmd_len: usize) -> String {
    lazy_static! {
        pub static ref EVENT_DESC_MAP: HashMap<String, String> = {
            let mut m = HashMap::new();
            m.insert(
                "HistoryHintComplete".to_string(),
                "Complete history hint (default in full)".to_string(),
            );
            m.insert(
                "HistoryHintWordComplete".to_string(),
                "Complete a single token/word of the history hint".to_string(),
            );
            m.insert("CtrlD".to_string(), "Handle EndOfLine event".to_string());
            m.insert("CtrlC".to_string(), "Handle SIGTERM key input".to_string());
            m.insert(
                "ClearScreen".to_string(),
                "Clears the screen and sets prompt to first line".to_string(),
            );
            m.insert("ClearScrollback".to_string(),"Clears the screen and the scrollback buffer, sets the prompt back to the first line".to_string());
            m.insert("Enter".to_string(), "Handle enter event".to_string());
            m.insert(
                "Submit".to_string(),
                "Handle unconditional submit event".to_string(),
            );
            m.insert(
                "SubmitOrNewline".to_string(),
                "Submit at the end of the *complete* text, otherwise newline".to_string(),
            );
            m.insert("Esc".to_string(), "Esc event".to_string());
            m.insert("Mouse".to_string(), "Mouse".to_string());
            m.insert(
                "Resize(u16, u16)".to_string(),
                "trigger terminal resize".to_string(),
            );
            m.insert(
                "Edit(Vec<EditCommand>)".to_string(),
                "Run these commands in the editor".to_string(),
            );
            m.insert("Repaint".to_string(), "Trigger full repaint".to_string());
            m.insert(
                "PreviousHistory".to_string(),
                "Navigate to the previous historic buffer".to_string(),
            );
            m.insert(
                "Up".to_string(),
                "Move up to the previous line, if multiline, or up into the historic buffers"
                    .to_string(),
            );
            m.insert(
                "Down".to_string(),
                "Move down to the next line, if multiline, or down through the historic buffers"
                    .to_string(),
            );
            m.insert(
                "Right".to_string(),
                "Move right to the next column, completion entry, or complete hint".to_string(),
            );
            m.insert(
                "Left".to_string(),
                "Move left to the next column, or completion entry".to_string(),
            );
            m.insert(
                "NextHistory".to_string(),
                "Navigate to the next historic buffer".to_string(),
            );
            m.insert(
                "SearchHistory".to_string(),
                "Search the history for a string".to_string(),
            );
            m.insert(
                "Multiple(Vec<ReedlineEvent>)".to_string(),
                "Multiple chained (Vi)".to_string(),
            );
            m.insert(
                "UntilFound(Vec<ReedlineEvent>)".to_string(),
                "Test".to_string(),
            );
            m.insert(
                "Menu(String)".to_string(),
                "Trigger a menu event. It activates a menu with the event name".to_string(),
            );
            m.insert(
                "MenuNext".to_string(),
                "Next element in the menu".to_string(),
            );
            m.insert(
                "MenuPrevious".to_string(),
                "Previous element in the menu".to_string(),
            );
            m.insert("MenuUp".to_string(), "Moves up in the menu".to_string());
            m.insert("MenuDown".to_string(), "Moves down in the menu".to_string());
            m.insert("MenuLeft".to_string(), "Moves left in the menu".to_string());
            m.insert(
                "MenuRight".to_string(),
                "Moves right in the menu".to_string(),
            );
            m.insert(
                "MenuPageNext".to_string(),
                "Move to the next history page".to_string(),
            );
            m.insert(
                "MenuPagePrevious".to_string(),
                "Move to the previous history page".to_string(),
            );
            m.insert("OpenEditor".to_string(), "Open text editor".to_string());
            m
        };
    };

    let event_highlight = nu_resolve_style(MessageLevel::Subheading).paint(event_name);
    let event_highlight = format!("{event_highlight}");
    let event_desc = format!(
        "{:<max_cmd_len$} {}",
        event_highlight,
        EVENT_DESC_MAP.get(event_name).unwrap_or(&String::new())
    );
    event_desc
}

/// Helper function to format `EditCommand` and include its doc comments
#[allow(clippy::too_many_lines)]
fn format_edit_commands(edit_cmds: &Vec<EditCommand>, max_cmd_len: usize) -> String {
    lazy_static! {
        pub static ref CMD_DESC_MAP: HashMap<String, String> = {
            let mut m = HashMap::new();
            m.insert(
                "MoveToStart".to_string(),
                "Move to the start of the buffer".to_string(),
            );
            m.insert(
                "MoveToLineStart".to_string(),
                "Move to the start of the current line".to_string(),
            );
            m.insert(
                "MoveToEnd".to_string(),
                "Move to the end of the buffer".to_string(),
            );
            m.insert(
                "MoveToLineEnd".to_string(),
                "Move to the end of the current line".to_string(),
            );
            m.insert(
                "MoveLeft".to_string(),
                "Move one character to the left".to_string(),
            );
            m.insert(
                "MoveRight".to_string(),
                "Move one character to the right".to_string(),
            );
            m.insert(
                "MoveWordLeft".to_string(),
                "Move one word to the left".to_string(),
            );
            m.insert(
                "MoveBigWordLeft".to_string(),
                "Move one WORD to the left".to_string(),
            );
            m.insert(
                "MoveWordRight".to_string(),
                "Move one word to the right".to_string(),
            );
            m.insert(
                "MoveWordRightStart".to_string(),
                "Move one word to the right, stop at start of word".to_string(),
            );
            m.insert(
                "MoveBigWordRightStart".to_string(),
                "Move one WORD to the right, stop at start of WORD".to_string(),
            );
            m.insert(
                "MoveWordRightEnd".to_string(),
                "Move one word to the right, stop at end of word".to_string(),
            );
            m.insert(
                "MoveBigWordRightEnd".to_string(),
                "Move one WORD to the right, stop at end of WORD".to_string(),
            );
            m.insert("MoveToPosition".to_string(), "Move to position".to_string());
            m.insert(
                "InsertChar".to_string(),
                "Insert a character at the current insertion point".to_string(),
            );
            m.insert(
                "InsertString".to_string(),
                "Insert a string at the current insertion point".to_string(),
            );
            m.insert(
                "InsertNewline".to_string(),
                "Insert the system specific new line character".to_string(),
            );
            m.insert(
                "ReplaceChars".to_string(),
                "Replace characters with string".to_string(),
            );
            m.insert(
                "Backspace".to_string(),
                "Backspace delete from the current insertion point".to_string(),
            );
            m.insert(
                "Delete".to_string(),
                "Delete in-place from the current insertion point".to_string(),
            );
            m.insert(
                "CutChar".to_string(),
                "Cut the grapheme right from the current insertion point".to_string(),
            );
            m.insert(
                "BackspaceWord".to_string(),
                "Backspace delete a word from the current insertion point".to_string(),
            );
            m.insert(
                "DeleteWord".to_string(),
                "Delete in-place a word from the current insertion point".to_string(),
            );
            m.insert("Clear".to_string(), "Clear the current buffer".to_string());
            m.insert(
                "ClearToLineEnd".to_string(),
                "Clear to the end of the current line".to_string(),
            );
            m.insert("Complete".to_string(), "Insert completion: entire completion if there is only one possibility, or else up to shared prefix.".to_string());
            m.insert(
                "CutCurrentLine".to_string(),
                "Cut the current line".to_string(),
            );
            m.insert(
                "CutFromStart".to_string(),
                "Cut from the start of the buffer to the insertion point".to_string(),
            );
            m.insert(
                "CutFromLineStart".to_string(),
                "Cut from the start of the current line to the insertion point".to_string(),
            );
            m.insert(
                "CutToEnd".to_string(),
                "Cut from the insertion point to the end of the buffer".to_string(),
            );
            m.insert(
                "CutToLineEnd".to_string(),
                "Cut from the insertion point to the end of the current line".to_string(),
            );
            m.insert(
                "CutWordLeft".to_string(),
                "Cut the word left of the insertion point".to_string(),
            );
            m.insert(
                "CutBigWordLeft".to_string(),
                "Cut the WORD left of the insertion point".to_string(),
            );
            m.insert(
                "CutWordRight".to_string(),
                "Cut the word right of the insertion point".to_string(),
            );
            m.insert(
                "CutBigWordRight".to_string(),
                "Cut the WORD right of the insertion point".to_string(),
            );
            m.insert(
                "CutWordRightToNext".to_string(),
                "Cut the word right of the insertion point and any following space".to_string(),
            );
            m.insert(
                "CutBigWordRightToNext".to_string(),
                "Cut the WORD right of the insertion point and any following space".to_string(),
            );
            m.insert(
                "PasteCutBufferBefore".to_string(),
                "Paste the cut buffer in front of the insertion point (Emacs, vi P)".to_string(),
            );
            m.insert(
                "PasteCutBufferAfter".to_string(),
                "Paste the cut buffer in front of the insertion point (vi p)".to_string(),
            );
            m.insert(
                "UppercaseWord".to_string(),
                "Upper case the current word".to_string(),
            );
            m.insert(
                "LowercaseWord".to_string(),
                "Lower case the current word".to_string(),
            );
            m.insert(
                "CapitalizeChar".to_string(),
                "Capitalize the current character".to_string(),
            );
            m.insert(
                "SwitchcaseChar".to_string(),
                "Switch the case of the current character".to_string(),
            );
            m.insert(
                "SwapWords".to_string(),
                "Swap the current word with the word to the right".to_string(),
            );
            m.insert(
                "SwapGraphemes".to_string(),
                "Swap the current grapheme/character with the one to the right".to_string(),
            );
            m.insert(
                "Undo".to_string(),
                "Undo the previous edit command".to_string(),
            );
            m.insert(
                "Redo".to_string(),
                "Redo an edit command from the undo history".to_string(),
            );
            m.insert(
                "CutRightUntil".to_string(),
                "CutUntil right until char".to_string(),
            );
            m.insert(
                "CutRightBefore".to_string(),
                "CutUntil right before char".to_string(),
            );
            m.insert(
                "MoveRightUntil".to_string(),
                "MoveUntil right until char".to_string(),
            );
            m.insert(
                "MoveRightBefore".to_string(),
                "MoveUntil right before char".to_string(),
            );
            m.insert(
                "CutLeftUntil".to_string(),
                "CutUntil left until char".to_string(),
            );
            m.insert(
                "CutLeftBefore".to_string(),
                "CutUntil left before char".to_string(),
            );
            m.insert(
                "MoveLeftUntil".to_string(),
                "MoveUntil left until char".to_string(),
            );
            m.insert(
                "MoveLeftBefore".to_string(),
                "MoveUntil left before char".to_string(),
            );
            m.insert(
                "SelectAll".to_string(),
                "Select whole input buffer".to_string(),
            );
            m.insert(
                "CutSelection".to_string(),
                "Cut selection to local buffer".to_string(),
            );
            m.insert(
                "CopySelection".to_string(),
                "Copy selection to local buffer".to_string(),
            );
            m.insert(
                "Paste".to_string(),
                "Paste content from local buffer at the current cursor position".to_string(),
            );
            m
        };
    };

    let mut cmd_descriptions = Vec::new();
    // eprintln!("edit_cmds={edit_cmds:?}");

    for cmd in edit_cmds {
        let cmd_highlight = nu_resolve_style(MessageLevel::Subheading).paint(format!("{cmd:?}"));
        let cmd_highlight = format!("{cmd_highlight}");
        let cmd_desc = match cmd {
            EditCommand::MoveToStart { select }
            | EditCommand::MoveToLineStart { select }
            | EditCommand::MoveToEnd { select }
            | EditCommand::MoveToLineEnd { select }
            | EditCommand::MoveLeft { select }
            | EditCommand::MoveRight { select }
            | EditCommand::MoveWordLeft { select }
            | EditCommand::MoveBigWordLeft { select }
            | EditCommand::MoveWordRight { select }
            | EditCommand::MoveWordRightStart { select }
            | EditCommand::MoveBigWordRightStart { select }
            | EditCommand::MoveWordRightEnd { select }
            | EditCommand::MoveBigWordRightEnd { select } => format!(
                "{:<max_cmd_len$} {}{}",
                cmd_highlight,
                CMD_DESC_MAP
                    .get(format!("{cmd:?}").split_once(' ').unwrap().0)
                    .unwrap_or(&String::new()),
                if *select {
                    ". Select the text between the current cursor position and destination"
                } else {
                    ", without selecting"
                }
            ),
            EditCommand::InsertString(_)
            | EditCommand::InsertNewline
            | EditCommand::ReplaceChar(_)
            | EditCommand::ReplaceChars(_, _)
            | EditCommand::Backspace
            | EditCommand::Delete
            | EditCommand::CutChar
            | EditCommand::BackspaceWord
            | EditCommand::DeleteWord
            | EditCommand::Clear
            | EditCommand::ClearToLineEnd
            | EditCommand::Complete
            | EditCommand::CutCurrentLine
            | EditCommand::CutFromStart
            | EditCommand::CutFromLineStart
            | EditCommand::CutToEnd
            | EditCommand::CutToLineEnd
            | EditCommand::CutWordLeft
            | EditCommand::CutBigWordLeft
            | EditCommand::CutWordRight
            | EditCommand::CutBigWordRight
            | EditCommand::CutWordRightToNext
            | EditCommand::CutBigWordRightToNext
            | EditCommand::PasteCutBufferBefore
            | EditCommand::PasteCutBufferAfter
            | EditCommand::UppercaseWord
            | EditCommand::InsertChar(_)
            | EditCommand::CapitalizeChar
            | EditCommand::SwitchcaseChar
            | EditCommand::SwapWords
            | EditCommand::SwapGraphemes
            | EditCommand::Undo
            | EditCommand::Redo
            | EditCommand::CutRightUntil(_)
            | EditCommand::CutRightBefore(_)
            | EditCommand::CutLeftUntil(_)
            | EditCommand::CutLeftBefore(_)
            | EditCommand::CutSelection
            | EditCommand::CopySelection
            | EditCommand::Paste
            | EditCommand::SelectAll
            | EditCommand::LowercaseWord => format!(
                "{:<max_cmd_len$} {}",
                cmd_highlight,
                CMD_DESC_MAP
                    .get(&format!("{cmd:?}"))
                    .unwrap_or(&String::new())
            ),
            EditCommand::MoveRightUntil { c: _, select }
            | EditCommand::MoveRightBefore { c: _, select }
            | EditCommand::MoveLeftUntil { c: _, select }
            | EditCommand::MoveLeftBefore { c: _, select } => format!(
                "{:<max_cmd_len$} {}. {}",
                cmd_highlight,
                CMD_DESC_MAP
                    .get(format!("{cmd:?}").split_once(' ').unwrap().0)
                    .unwrap_or(&String::new()),
                if *select {
                    "Select the text between the current cursor position and destination"
                } else {
                    "without selecting"
                }
            ),
            EditCommand::MoveToPosition { position, select } => format!(
                "{:<max_cmd_len$} {} {} {}",
                cmd_highlight,
                CMD_DESC_MAP
                    .get(format!("{cmd:?}").split_once(' ').unwrap().0)
                    .unwrap_or(&String::new()),
                position,
                if *select {
                    "Select the text between the current cursor position and destination"
                } else {
                    "without selecting"
                }
            ),
            // Add other EditCommand variants and their descriptions here
            _ => format!("{:<width$}", cmd_highlight, width = max_cmd_len + 2),
        };
        cmd_descriptions.push(cmd_desc);
    }
    cmd_descriptions.join(", ")
}

/// Delete the temporary files used by the current REPL instance.
/// # Errors
/// Currently will not return any errors.
#[allow(clippy::unnecessary_wraps)]
pub fn delete(_args: &ArgMatches, context: &mut Context) -> Result<Option<String>, BuildRunError> {
    let build_state = &context.build_state;
    let clean_up = clean_up(&build_state.source_path, &build_state.target_dir_path);
    if clean_up.is_ok()
        || (!&build_state.source_path.exists() && !&build_state.target_dir_path.exists())
    {
        log!(Verbosity::Quieter, "Deleted");
    } else {
        log!(
            Verbosity::Quieter,
            "Failed to delete all files - enter l(ist) to list remaining files"
        );
    }
    Ok(Some(String::from("End of delete")))
}

/// Open the history file in an editor.
/// # Errors
/// Will return `Err` if there is an error editing the file.
#[allow(clippy::unnecessary_wraps)]
pub fn edit_history(
    _args: &ArgMatches,
    context: &mut Context,
) -> Result<Option<String>, BuildRunError> {
    let history_file = context.build_state.cargo_home.clone().join(HISTORY_FILE);
    println!("history_file={history_file:#?}");
    OpenOptions::new()
        .write(true)
        .create(true)
        .truncate(false)
        .open(&history_file)?;
    edit::edit_file(&history_file)?;
    Ok(Some(String::from("End of history file edit")))
}

/// Open the generated destination Rust source code file in an editor.
/// # Errors
/// Will return `Err` if there is an error editing the file.
#[allow(clippy::unnecessary_wraps)]
pub fn edit(_args: &ArgMatches, context: &mut Context) -> Result<Option<String>, BuildRunError> {
    let (build_state, _start) = (&mut context.build_state, context.start);

    edit::edit_file(&build_state.source_path)?;

    Ok(Some(String::from("End of source edit")))
}

/// Open the generated Cargo.toml file in an editor.
/// # Errors
/// Will return `Err` if there is an error editing the file.
#[allow(clippy::unnecessary_wraps)]
pub fn toml(_args: &ArgMatches, context: &mut Context) -> Result<Option<String>, BuildRunError> {
    edit::edit_file(&context.build_state.cargo_toml_path)?;
    Ok(Some(String::from("End of Cargo.toml edit")))
}

/// Run an expression.
/// # Errors
/// Currently will not return any errors.
#[allow(clippy::unnecessary_wraps)]
pub fn run_expr(
    _args: &ArgMatches,
    context: &mut Context,
) -> Result<Option<String>, BuildRunError> {
    let (options, proc_flags, build_state, start) = (
        &mut context.options,
        context.proc_flags,
        &mut context.build_state,
        context.start,
    );

    debug_log!("In run_expr: build_state={build_state:#?}");
    let result = gen_build_run(options, proc_flags, build_state, None::<Ast>, &start);
    if result.is_err() {
        log!(Verbosity::Quieter, "{result:?}");
    }
    Ok(Some(String::from("End of run")))
}

/// Parse the current line. Borrowed from clap-repl crate.
#[must_use]
pub fn parse_line(line: &str) -> (String, Vec<String>) {
    lazy_static! {
        static ref RE: Regex = Regex::new(r#"("[^"\n]+"|[\S]+)"#).unwrap();
    }
    let mut args = RE
        .captures_iter(line)
        .map(|a| a[0].to_string().replace('\"', ""))
        .collect::<Vec<String>>();
    let command: String = args.drain(..1).collect();
    (command, args)
}

/// Display the REPL banner.
pub fn disp_repl_banner(cmd_list: &str) {
    nu_color_println!(
        nu_resolve_style(MessageLevel::Heading),
        r#"Enter a Rust expression (e.g., 2 + 3 or "Hi!"), or one of: {cmd_list}."#
    );

    nu_color_println!(
        nu_resolve_style(MessageLevel::Subheading),
        r"Expressions in matching braces, brackets or quotes may span multiple lines.
Use ↑ ↓ to navigate history, →  to select current. Ctrl-U: clear. Ctrl-K: delete to end."
    );
}

/// Display a list of the temporary files used by the current REPL instance.
/// # Errors
/// This function will return an error in the following situations, but is not limited to just these cases:
/// The provided path doesn't exist.
/// The process lacks permissions to view the contents.
/// The path points at a non-directory file.
#[allow(clippy::unnecessary_wraps)]
pub fn list(_args: &ArgMatches, context: &mut Context) -> Result<Option<String>, BuildRunError> {
    let build_state = &context.build_state;
    let source_path = &build_state.source_path;
    if source_path.exists() {
        log!(Verbosity::Quieter, "File: {:?}", &source_path);
    }

    // Display directory contents
    display_dir_contents(&build_state.target_dir_path)?;

    // Check if neither file nor directory exist
    if !&source_path.exists() && !&build_state.target_dir_path.exists() {
        log!(Verbosity::Quieter, "No temporary files found");
    }
    Ok(Some(String::from("End of list")))
}