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
//  REPL.rs
//    by Lut99
//
//  Created:
//    12 Sep 2022, 16:42:47
//  Last edited:
//    08 Jan 2024, 10:23:14
//  Auto updated?
//    Yes
//
//  Description:
//!   Implements the interactive Read-Eval-Print Loop.
//

use std::borrow::Cow::{self, Borrowed, Owned};
use std::fs;
use std::io::{Stderr, Stdout};

use brane_ast::ParserOptions;
use brane_dsl::Language;
use brane_exe::FullValue;
use brane_tsk::docker::DockerOptions;
use brane_tsk::spec::AppId;
use log::warn;
use rustyline::completion::{Completer, FilenameCompleter, Pair};
use rustyline::error::ReadlineError;
use rustyline::highlight::{Highlighter, MatchingBracketHighlighter};
use rustyline::hint::{Hinter, HistoryHinter};
use rustyline::history::DefaultHistory;
use rustyline::validate::{self, MatchingBracketValidator, Validator};
use rustyline::{CompletionType, Config, Context, EditMode, Editor};
use rustyline_derive::Helper;

pub use crate::errors::ReplError as Error;
use crate::instance::InstanceInfo;
use crate::run::{
    InstanceVmState, OfflineVmState, initialize_instance_vm, initialize_offline_vm, process_instance_result, process_offline_result, run_instance_vm,
    run_offline_vm,
};
use crate::utils::{ensure_config_dir, get_history_file};


/***** HELPER FUNCTIONS *****/
/// Handles magicks in the REPL.
///
/// # Arguments
/// - `line`: The line given by the user.
///
/// # Returns
/// If a magics was triggered, returns if that trigger should break the REPL (i.e., returns `Some(true)` if so or `Some(false)` if the REPL can continue but not with this line). If the line was not a REPL magick, then `None` is returned.
fn repl_magicks(line: impl AsRef<str>) -> Option<bool> {
    let line: &str = line.as_ref();

    // Switch on the command given
    if line == "exit" || line == "quit" || line == "q" {
        Some(true)
    } else if line == "help" {
        println!("You found the secret REPL-commands!");
        println!(
            "These commands are not part of BraneScript (or whatever language you're using this REPL with), but instead provide convienience \
             functions for the REPL itself."
        );
        println!();
        println!("Supported commands:");
        println!("  `exit`, `quit` or `q`   Exits the REPL. The same can be achieved by hitting `Ctrl+C` or `Ctrl+D`.");
        println!("  `help`                  Prints this overview.");
        println!();
        println!("Any other statement that is not one of the commands above is interpreted as the language you're REPLing.");
        println!();
        Some(false)
    } else {
        None
    }
}





/***** REPL HELPER *****/
/// Implements the helper for the Repl (auto-completion and syntax highlighting and such)
#[derive(Helper)]
struct ReplHelper {
    /// The completer: we auto-complete filenames, like the standard terminal
    completer: FilenameCompleter,
    /// Highlighter: we highlight matching brackets
    highlighter: MatchingBracketHighlighter,
    /// We even validate for matching brackets
    validator: MatchingBracketValidator,
    /// We hint based on the user's history
    hinter: HistoryHinter,
    /// Does something with being a coloured prompt(?)
    colored_prompt: String,
}

impl Completer for ReplHelper {
    type Candidate = Pair;

    fn complete(&self, line: &str, pos: usize, ctx: &Context<'_>) -> Result<(usize, Vec<Pair>), ReadlineError> {
        self.completer.complete(line, pos, ctx)
    }
}

impl Hinter for ReplHelper {
    type Hint = String;

    fn hint(&self, line: &str, pos: usize, ctx: &Context<'_>) -> Option<String> {
        self.hinter.hint(line, pos, ctx).and_then(|h| h.lines().next().map(|l| l.to_string()))
    }
}

impl Highlighter for ReplHelper {
    fn highlight_prompt<'b, 's: 'b, 'p: 'b>(&'s self, prompt: &'p str, default: bool) -> Cow<'b, str> {
        if default { Borrowed(&self.colored_prompt) } else { Borrowed(prompt) }
    }

    fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> { Owned("\x1b[1m".to_owned() + hint + "\x1b[m") }

    fn highlight<'l>(&self, line: &'l str, pos: usize) -> Cow<'l, str> { self.highlighter.highlight(line, pos) }

    fn highlight_char(&self, line: &str, pos: usize) -> bool { self.highlighter.highlight_char(line, pos) }
}

impl Validator for ReplHelper {
    fn validate(&self, ctx: &mut validate::ValidationContext) -> rustyline::Result<validate::ValidationResult> { self.validator.validate(ctx) }

    fn validate_while_typing(&self) -> bool { self.validator.validate_while_typing() }
}





/***** SUBCOMMANDS *****/
/// Entrypoint to the REPL, which performs the required initialization.
///
/// # Arguments
/// - `proxy_addr`: The address to proxy any data transfers through if they occur.
/// - `remote`: Whether to use the remote Brane instance in the login file to run the on instead.
/// - `attach`: If not None, defines the session ID of an existing session to connect to.
/// - `language`: The language with which to compile the file.
/// - `clear`: Whether or not to clear the history of the REPL before beginning.
/// - `profile`: If given, prints the profile timings to stdout if available.
/// - `docker_opts`: The DockerOpts that determines how we connect to the local Docker dameon.
/// - `keep_containers`: Whether to keep containers after execution or not.
///
/// # Errors
/// This function errors if we could not properly read from/write to the terminal. Additionally, it may error if any of the given statements fails for whatever reason.
#[allow(clippy::too_many_arguments)]
pub async fn start(
    proxy_addr: Option<String>,
    remote: bool,
    attach: Option<AppId>,
    language: Language,
    clear: bool,
    profile: bool,
    docker_opts: DockerOptions,
    keep_containers: bool,
) -> Result<(), Error> {
    // Build the config for the rustyline REPL.
    let config = Config::builder().history_ignore_space(true).completion_type(CompletionType::Circular).edit_mode(EditMode::Emacs).build();

    // Build the helper for the REPL
    let repl_helper = ReplHelper {
        completer: FilenameCompleter::new(),
        highlighter: MatchingBracketHighlighter::new(),
        hinter: HistoryHinter {},
        colored_prompt: "".to_owned(),
        validator: MatchingBracketValidator::new(),
    };

    // Get the history file, clearing it if necessary
    if let Err(err) = ensure_config_dir(true) {
        return Err(Error::ConfigDirCreateError { err });
    };
    let history_file = match get_history_file() {
        Ok(file) => file,
        Err(err) => {
            return Err(Error::HistoryFileError { err });
        },
    };
    if clear && history_file.exists() {
        if let Err(err) = fs::remove_file(&history_file) {
            warn!("Could not clear REPL history: {}", err);
        };
    }

    // Create the REPL
    let mut rl = match Editor::with_config(config) {
        Ok(rl) => rl,
        Err(err) => {
            return Err(Error::EditorCreateError { err });
        },
    };
    rl.set_helper(Some(repl_helper));
    if let Err(err) = rl.load_history(&history_file) {
        warn!("Could not load REPL history from '{}': {}", history_file.display(), err);
    }

    // Prepare the parser options
    let options: ParserOptions = ParserOptions::new(language);

    // Initialization done; run the REPL
    println!("Welcome to the Brane REPL, press Ctrl+D to exit.\n");
    if remote {
        // Open the login file to find the remote location
        let info: InstanceInfo = match InstanceInfo::from_active_path() {
            Ok(info) => info,
            Err(err) => {
                return Err(Error::InstanceInfoError { err });
            },
        };

        // Run the thing
        remote_repl(&mut rl, info, proxy_addr, attach, options, profile).await?;
    } else {
        local_repl(&mut rl, options, docker_opts, keep_containers).await?;
    }

    // Try to save the history if we exited cleanly
    if let Err(reason) = rl.save_history(&history_file) {
        warn!("Could not save session history to '{}': {}", history_file.display(), reason);
    }

    // Done!
    Ok(())
}



/// Runs the given file on the remote instance.
///
/// # Arguments
/// - `rl`: The REPL interface we use to do the R-part of a REPL.
/// - `info`: An [`InstanceInfo`] that describes how to connect to the backend.
/// - `proxy_addr`: The address to proxy any data transfers through if they occur.
/// - `attach`: If given, uses the given ID to attach to an existing session instead of creating a new one.
/// - `options`: The ParseOptions that specify how to parse the incoming source.
/// - `profile`: If given, prints the profile timings to stdout if reported by the remote.
///
/// # Returns
/// Nothing, but does print results and such to stdout. Might also produce new datasets.
async fn remote_repl(
    rl: &mut Editor<ReplHelper, DefaultHistory>,
    info: InstanceInfo,
    proxy_addr: Option<String>,
    attach: Option<AppId>,
    options: ParserOptions,
    profile: bool,
) -> Result<(), Error> {
    let api_address: String = info.api.to_string();
    let drv_address: String = info.drv.to_string();

    // First we initialize the remote thing
    let mut state: InstanceVmState<Stdout, Stderr> =
        match initialize_instance_vm(&api_address, &drv_address, Some(info.user.clone()), attach, options).await {
            Ok(state) => state,
            Err(err) => {
                return Err(Error::InitializeError { what: "remote instance client", err });
            },
        };

    // Next, enter the L in REPL
    let mut count: u32 = 1;
    loop {
        // Prepare the prompt with the current iteration number
        let p = format!("{count}> ");

        // Write the prompt in a coloured way
        rl.helper_mut().expect("No helper").colored_prompt = format!("\x1b[1;32m{p}\x1b[0m");

        // Find a line to read
        match rl.readline(&p) {
            Ok(line) => {
                // The command checked out, so add it to the history
                if let Err(err) = rl.add_history_entry(&line.replace('\n', " ")) {
                    warn!("Failed to update REPL history: {err}");
                }

                // Fetch REPL magicks
                if let Some(quit) = repl_magicks(&line) {
                    if quit {
                        break;
                    } else {
                        continue;
                    }
                }

                // Next, we run the VM (one snippet only ayway)
                let res: FullValue = match run_instance_vm(&drv_address, &mut state, "<stdin>", &line, profile).await {
                    Ok(res) => res,
                    Err(_) => {
                        continue;
                    },
                };

                // Then, we collect and process the result
                if let Err(err) = process_instance_result(&api_address, &proxy_addr, res).await {
                    error!("{}", Error::ProcessError { what: "remote instance VM", err });
                    continue;
                }

                // Go to the next iteration
                count += 1;
                state.state.offset += 1 + line.chars().filter(|c| *c == '\n').count();
            },
            Err(ReadlineError::Interrupted) => {
                println!("Keyboard interrupt received, exiting...");
                break;
            },
            Err(ReadlineError::Eof) => {
                break;
            },
            Err(err) => {
                error!("Failed to get new line: {}", err);
                break;
            },
        }
    }

    // Done
    Ok(())
}



/// Runs the given file on the local machine.
///
/// # Arguments
/// - `rl`: The REPL interface we use to do the R-part of a REPL.
/// - `parse_opts`: The ParseOptions that specify how to parse the incoming source.
/// - `docker_opts`: The DockerOpts that determines how we connect to the local Docker dameon.
/// - `keep_containers`: Whether to keep containers after execution or not.
///
/// # Returns
/// Nothing, but does print results and such to stdout. Might also produce new datasets.
async fn local_repl(
    rl: &mut Editor<ReplHelper, DefaultHistory>,
    parse_opts: ParserOptions,
    docker_opts: DockerOptions,
    keep_containers: bool,
) -> Result<(), Error> {
    // First we initialize the remote thing
    let mut state: OfflineVmState = match initialize_offline_vm(parse_opts, docker_opts, keep_containers) {
        Ok(state) => state,
        Err(err) => {
            return Err(Error::InitializeError { what: "offline VM", err });
        },
    };

    // With the VM setup, enter the L in the REPL
    let mut count: u32 = 1;
    loop {
        // Prepare the prompt with the current iteration number
        let p = format!("{count}> ");

        // Write the prompt in a coloured way
        rl.helper_mut().expect("No helper").colored_prompt = format!("\x1b[1;32m{p}\x1b[0m");

        // Find a line to read
        match rl.readline(&p) {
            Ok(line) => {
                // The command checked out, so add it to the history
                if let Err(err) = rl.add_history_entry(&line.replace('\n', " ")) {
                    warn!("Failed to update REPL history: {err}");
                }

                // Fetch REPL magicks
                if let Some(quit) = repl_magicks(&line) {
                    if quit {
                        break;
                    } else {
                        continue;
                    }
                }

                // Next, we run the VM (one snippet only ayway)
                let res: FullValue = match run_offline_vm(&mut state, "<stdin>", &line).await {
                    Ok(res) => res,
                    Err(err) => {
                        return Err(Error::RunError { what: "offline VM", err });
                    },
                };

                // Then, we collect and process the result
                if let Err(err) = process_offline_result(res) {
                    error!("{}", Error::ProcessError { what: "offline VM", err });
                    continue;
                }

                // Go to the next iteration
                count += 1;
                state.state.offset += 1 + line.chars().filter(|c| *c == '\n').count();
            },
            Err(ReadlineError::Interrupted) => {
                println!("Keyboard interrupt received, exiting...");
                break;
            },
            Err(ReadlineError::Eof) => {
                break;
            },
            Err(err) => {
                error!("Failed to get new line: {}", err);
                break;
            },
        }
    }

    // Done
    Ok(())
}