arostech_cli_rust/models/
menu_options.rs

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
use colored::Colorize;
use std::io::{self, Write};

use super::menu_item::MenuItem;

pub struct MenuOptions {
  options: Vec<MenuItem>
}

impl MenuOptions {
  pub fn new() -> MenuOptions {
    MenuOptions {
      options: create_options()
    }

  }
  pub fn clone(&self) -> Vec<String> {
    let string_options: Vec<String> =  self.options.iter().map(|menu_item: &MenuItem|{
      menu_item.name.clone()
    }).collect();
    return string_options;

  }

  pub fn run (&self, choice: String) -> bool {
    match self.options.iter().find(|item|item.name == choice) {
      Some(item) => {
        print!("{}[2J", 27 as char);
        println!("{}{}","Action: ".green(),item.name);
        println!("\n");
        let should_exit_after: (bool, String) =  (item.action)();
        println!("{}", should_exit_after.1);
        should_exit_after.0
        
      },
      None => {
        println!("Error: Option not found"); 
        false
      }
    }
  }
}

// Helpers
fn create_options ()-> Vec<MenuItem>{
  vec![
    MenuItem {
      name: "Visit arostech.dk".to_string(),
      action: ||{
        println!("... Opening browser");
        let _ = open::that("https://arostech.dk");
        
        (true, "... Opening browser".to_string())
      }  
    },
    MenuItem {
      name: "SSH into Cloudways".to_string(),
      action: ||{
        (false, "SSH_CLOUDWAYS".to_string())
      }
    },
    MenuItem {
      name: "Exit".to_string(),
      action: ||{
        println!("Goodbye.");
        (false, "EXIT".to_string())
      }
    }

  ]
}