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
// Copyright (c) 2022 MASSA LABS <info@massa.net>

use crate::ask_password;
use crate::cmds::Command;
use crate::massa_fancy_ascii_art_logo;
use crate::settings::SETTINGS;
use anyhow::Result;
use console::style;
use massa_sdk::Client;
use massa_wallet::Wallet;
use rustyline::completion::{Completer, FilenameCompleter, Pair};
use rustyline::error::ReadlineError;
use rustyline::validate::MatchingBracketValidator;
use rustyline::{CompletionType, Config, Editor};
use rustyline_derive::{Completer, Helper, Highlighter, Hinter, Validator};
use std::env;
use std::path::Path;
use strum::IntoEnumIterator;
use strum::ParseError;

fn group_parameters(parameters: Vec<String>) -> Vec<String> {
    let mut new_parameters = Vec::new();
    let mut has_opening_simple_quote = false;
    let mut temp_simple_quote = String::new();
    let mut has_opening_double_quote = false;
    let mut temp_double_quote = String::new();
    for param in parameters {
        let mut chars = param.chars();
        match chars.next() {
            Some('\'') if !has_opening_double_quote => {
                has_opening_simple_quote = true;
                temp_simple_quote = param.clone();
                temp_simple_quote.remove(0);
            }
            Some('"') if !has_opening_simple_quote => {
                has_opening_double_quote = true;
                temp_double_quote = param.clone();
                temp_double_quote.remove(0);
            }
            Some(_) if has_opening_simple_quote => {
                temp_simple_quote.push(' ');
                temp_simple_quote.push_str(&param);
            }
            Some(_) if has_opening_double_quote => {
                temp_double_quote.push(' ');
                temp_double_quote.push_str(&param);
            }
            Some(_) => new_parameters.push(param.clone()),
            None => continue,
        };
        match chars.last() {
            Some('\'') if has_opening_simple_quote => {
                has_opening_simple_quote = false;
                let mut to_add = temp_simple_quote.clone();
                to_add.pop();
                new_parameters.push(to_add);
            }
            Some('"') if has_opening_double_quote => {
                has_opening_double_quote = false;
                let mut to_add = temp_double_quote.clone();
                to_add.pop();
                new_parameters.push(to_add);
            }
            Some(_) => continue,
            None => continue,
        }
    }
    new_parameters
}

#[derive(Helper, Completer, Hinter, Validator, Highlighter)]
struct MyHelper {
    #[rustyline(Completer)]
    completer: MassaCompleter,
    #[rustyline(Validator)]
    validator: MatchingBracketValidator,
}

pub(crate) async fn run(
    client: &mut Client,
    wallet_path: &Path,
    args_password: Option<String>,
) -> Result<()> {
    massa_fancy_ascii_art_logo!();
    println!("Use 'exit' or 'CTRL+D or CTRL+C' to quit the prompt");
    println!("Use the Up/Down arrows to scroll through history");
    println!("Use the Right arrow or Tab to complete your command");
    println!("Use the Enter key to execute your command");
    crate::cmds::help();
    let h = MyHelper {
        completer: MassaCompleter::new(),
        validator: MatchingBracketValidator::new(),
    };
    let config = Config::builder()
        .auto_add_history(true)
        .completion_prompt_limit(100)
        .completion_type(CompletionType::List)
        .max_history_size(10000)?
        .build();

    let mut rl = Editor::with_config(config)?;
    rl.set_helper(Some(h));
    if rl.load_history(&SETTINGS.history_file_path).is_err() {
        println!("No previous history.");
    }

    let mut wallet_opt = None;

    loop {
        let readline = rl.readline("command > ");
        match readline {
            Ok(line) => {
                if line.is_empty() {
                    continue;
                }
                if let Err(e) = rl.add_history_entry(line.as_str()) {
                    println!("Failed to append commands history {}", e);
                }
                if let Err(e) = rl.append_history(&SETTINGS.history_file_path) {
                    println!("Failed to append commands file history: {}", e);
                }
                let input: Vec<String> =
                    group_parameters(line.split_whitespace().map(|x| x.to_string()).collect());
                let cmd: Result<Command, ParseError> = input[0].parse();
                let parameters = input[1..].to_vec();
                // Print result of evaluated command
                match cmd {
                    Ok(command) => {
                        // Check if we need to prompt the user for their wallet password
                        if command.is_pwd_needed() && wallet_opt.is_none() {
                            let password =
                                match (args_password.clone(), env::var("MASSA_CLIENT_PASSWORD")) {
                                    (Some(pwd), _) => pwd,
                                    (_, Ok(pwd)) => pwd,
                                    _ => ask_password(wallet_path),
                                };

                            let wallet = match Wallet::new(
                                wallet_path.to_path_buf(),
                                password,
                                client.chain_id,
                            ) {
                                Ok(wallet) => wallet,
                                Err(e) => {
                                    println!("Could not open wallet: {}", e);
                                    continue;
                                }
                            };
                            wallet_opt = Some(wallet);
                        }

                        match command
                            .run(client, &mut wallet_opt, &parameters, false)
                            .await
                        {
                            Ok(output) => output.pretty_print(),
                            Err(e) => println!("{}", style(format!("Error: {}", e)).red()),
                        }
                    }
                    Err(_) => {
                        println!("Command not found!\ntype \"help\" to get the list of commands")
                    }
                }
            }
            Err(ReadlineError::Interrupted) => {
                break;
            }
            Err(ReadlineError::Eof) => {
                break;
            }
            Err(err) => {
                println!("Error: {err:?}");
                break;
            }
        }
    }
    Ok(())
}

struct MassaCompleter {
    file_completer: FilenameCompleter,
}

impl MassaCompleter {
    fn new() -> Self {
        Self {
            file_completer: FilenameCompleter::new(),
        }
    }
}

impl Completer for MassaCompleter {
    type Candidate = Pair;
    fn complete(
        &self,
        line: &str,
        pos: usize,
        ctx: &rustyline::Context<'_>,
    ) -> rustyline::Result<(usize, Vec<Self::Candidate>)> {
        if line.contains(' ') {
            self.file_completer.complete(line, pos, ctx)
        } else {
            let mut candidates = Vec::new();
            for cmd in Command::iter() {
                if cmd.to_string().starts_with(line) {
                    candidates.push(Pair {
                        display: cmd.to_string(),
                        replacement: cmd.to_string(),
                    });
                }
            }
            Ok((0, candidates))
        }
    }
}