summaryrefslogtreecommitdiff
path: root/vendor/dialoguer/examples/history.rs
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2024-01-08 00:21:28 +0300
committerValentin Popov <valentin@popov.link>2024-01-08 00:21:28 +0300
commit1b6a04ca5504955c571d1c97504fb45ea0befee4 (patch)
tree7579f518b23313e8a9748a88ab6173d5e030b227 /vendor/dialoguer/examples/history.rs
parent5ecd8cf2cba827454317368b68571df0d13d7842 (diff)
downloadfparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.tar.xz
fparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.zip
Initial vendor packages
Signed-off-by: Valentin Popov <valentin@popov.link>
Diffstat (limited to 'vendor/dialoguer/examples/history.rs')
-rw-r--r--vendor/dialoguer/examples/history.rs51
1 files changed, 51 insertions, 0 deletions
diff --git a/vendor/dialoguer/examples/history.rs b/vendor/dialoguer/examples/history.rs
new file mode 100644
index 0000000..0d69b27
--- /dev/null
+++ b/vendor/dialoguer/examples/history.rs
@@ -0,0 +1,51 @@
+use dialoguer::{theme::ColorfulTheme, History, Input};
+use std::{collections::VecDeque, process};
+
+fn main() {
+ println!("Use 'exit' to quit the prompt");
+ println!("In this example, history is limited to 4 entries");
+ println!("Use the Up/Down arrows to scroll through history");
+ println!();
+
+ let mut history = MyHistory::default();
+
+ loop {
+ if let Ok(cmd) = Input::<String>::with_theme(&ColorfulTheme::default())
+ .with_prompt("dialoguer")
+ .history_with(&mut history)
+ .interact_text()
+ {
+ if cmd == "exit" {
+ process::exit(0);
+ }
+ println!("Entered {}", cmd);
+ }
+ }
+}
+
+struct MyHistory {
+ max: usize,
+ history: VecDeque<String>,
+}
+
+impl Default for MyHistory {
+ fn default() -> Self {
+ MyHistory {
+ max: 4,
+ history: VecDeque::new(),
+ }
+ }
+}
+
+impl<T: ToString> History<T> for MyHistory {
+ fn read(&self, pos: usize) -> Option<String> {
+ self.history.get(pos).cloned()
+ }
+
+ fn write(&mut self, val: &T) {
+ if self.history.len() == self.max {
+ self.history.pop_back();
+ }
+ self.history.push_front(val.to_string());
+ }
+}