/ interpreter / src / dialoguer_input.rs
dialoguer_input.rs
 1  // Copyright (C) 2019-2025 ADnet Contributors
 2  // This file is part of the ADL library.
 3  
 4  // The ADL library is free software: you can redistribute it and/or modify
 5  // it under the terms of the GNU General Public License as published by
 6  // the Free Software Foundation, either version 3 of the License, or
 7  // (at your option) any later version.
 8  
 9  // The ADL library is distributed in the hope that it will be useful,
10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  // GNU General Public License for more details.
13  
14  // You should have received a copy of the GNU General Public License
15  // along with the ADL library. If not, see <https://www.gnu.org/licenses/>.
16  
17  use super::ui::{Ui, UserData};
18  
19  use colored::*;
20  
21  pub struct DialoguerUi {
22      history: dialoguer::BasicHistory,
23  }
24  
25  impl DialoguerUi {
26      pub fn new() -> Self {
27          DialoguerUi { history: dialoguer::BasicHistory::new() }
28      }
29  }
30  
31  impl Ui for DialoguerUi {
32      fn display_user_data(&mut self, data: &UserData<'_>) {
33          if let Some(result) = data.result {
34              println!("{}: {}", "Result".bold(), result.bright_cyan());
35          }
36          println!("{}", data.message);
37          if let Some(highlight_span) = data.highlight {
38              let first = data.code.get(0..highlight_span.0).expect("spans should be valid");
39              let second = data.code.get(highlight_span.0..highlight_span.1).expect("spans should be valid");
40              let third = data.code.get(highlight_span.1..).expect("spans should be valid");
41              println!("{first}{}{third}", second.red());
42          } else {
43              println!("{}", data.code);
44          }
45  
46          for (i, future) in data.futures.iter().enumerate() {
47              println!("{i:>4}: {future}");
48          }
49  
50          for (i, watchpoint) in data.watchpoints.iter().enumerate() {
51              println!("{i:>4}: {watchpoint}");
52          }
53      }
54  
55      fn receive_user_input(&mut self) -> String {
56          dialoguer::Input::with_theme(&dialoguer::theme::ColorfulTheme::default())
57              .with_prompt("Command?")
58              .history_with(&mut self.history)
59              .interact_text()
60              .unwrap()
61      }
62  }