arostech_cli_rust/models/
menu_options.rs

1use colored::Colorize;
2use std::io::{self, Write};
3
4use super::menu_item::MenuItem;
5
6pub struct MenuOptions {
7  options: Vec<MenuItem>
8}
9
10impl MenuOptions {
11  pub fn new() -> MenuOptions {
12    MenuOptions {
13      options: create_options()
14    }
15
16  }
17  pub fn clone(&self) -> Vec<String> {
18    let string_options: Vec<String> =  self.options.iter().map(|menu_item: &MenuItem|{
19      menu_item.name.clone()
20    }).collect();
21    return string_options;
22
23  }
24
25  pub fn run (&self, choice: String) -> bool {
26    match self.options.iter().find(|item|item.name == choice) {
27      Some(item) => {
28        // print!("{}[2J", 27 as char);
29        // println!("{}{}","Action: ".green(),item.name);
30        // println!("\n");
31        let result: (bool, String) =  (item.action)();
32        // Ensure output is flushed correctly
33        io::stdout().flush().unwrap();
34
35        // Print result message
36        println!("{}", result.1);
37        
38        // Ensure the result is flushed after printing
39        io::stdout().flush().unwrap();
40
41        // Return whether to exit
42        result.0
43        
44      },
45      None => {
46        println!("Error: Option not found"); 
47        false
48      }
49    }
50  }
51}
52
53// Helpers
54fn create_options ()-> Vec<MenuItem>{
55  vec![
56    MenuItem {
57      name: "Visit arostech.dk".to_string(),
58      action: ||{
59        let _ = open::that("https://arostech.dk");
60        
61        (true, "... Opening browser".to_string())
62      }  
63    },
64    MenuItem {
65      name: "SSH into Cloudways".to_string(),
66      action: ||{
67        (false, "SSH_CLOUDWAYS".to_string())
68      }
69    },
70    MenuItem {
71      name: "Exit".to_string(),
72      action: ||{
73        println!("Goodbye.");
74        (false, "EXIT".to_string())
75      }
76    }
77
78  ]
79}