mut_menu

Function mut_menu 

Source
pub fn mut_menu(menu: &TerminalMenu) -> RwLockWriteGuard<'_, TerminalMenuStruct>
Expand description

Get a mutable instance of the menu. Works only if has_exited(&menu) is true.

ยงExample

use terminal_menu::{menu, numeric, string, run, has_exited, mut_menu};
let mut my_menu = menu(vec![
    numeric("Charlie", 46.5, None, Some(32332.2), None)
]);
run(&my_menu);

//stuff

{
    let mut mutable_menu = mut_menu(&my_menu);
    println!("Selected Item: {}", mutable_menu.selected_item_name());
    mutable_menu.items.push(string("new item", "def", false));
}

run(&my_menu);
Examples found in repository?
examples/cancel.rs (line 14)
5fn main() {
6    use terminal_menu::{menu, label, button, run, mut_menu};
7    let menu = menu(vec![
8        label("press the button or hit 'q' or esc!"),
9        button("button")
10    ]);
11    run(&menu);
12
13    // true if exited with 'q' or esc, false if button was pressed
14    println!("{}", mut_menu(&menu).canceled());
15}
More examples
Hide additional examples
examples/long.rs (line 14)
5fn main() {
6    use terminal_menu::{menu, button, run, mut_menu};
7    let menu = menu(
8
9        // create buttons representing numbers from 1 to 100
10        (1..100).map(|n| button(format!("{}", n))).collect()
11
12    );
13    run(&menu);
14    if mut_menu(&menu).canceled() {
15        println!("Canceled!");
16        return;
17    }
18    println!("{}", mut_menu(&menu).selected_item_name());
19}
examples/simple.rs (line 28)
5fn main() {
6    use terminal_menu::{menu, label, button, run, mut_menu};
7    let menu = menu(vec![
8
9        // label:
10        //  not selectable, useful as a title, separator, etc...
11        label("----------------------"),
12        label("terminal-menu"),
13        label("use wasd or arrow keys"),
14        label("enter to select"),
15        label("'q' or esc to exit"),
16        label("-----------------------"),
17
18        // button:
19        //  exit the menu
20        button("Alice"),
21        button("Bob"),
22        button("Charlie")
23
24    ]);
25    run(&menu);
26
27    // if the menu was exited
28    if (mut_menu(&menu).canceled()) {
29        println!("Canceled!");
30        return;
31    }
32
33    // you can get the selected buttons name like so:
34    println!("Selected: {}", mut_menu(&menu).selected_item_name());
35}
examples/selections.rs (line 29)
5fn main() {
6    use terminal_menu::{menu, label, button, list, scroll, run, mut_menu};
7    let menu = menu(vec![
8        label("lists and scrolls"),
9
10        // with list and scroll you can select a value from a group of values
11        // you can change the selected value with arrow keys, wasd, or enter
12
13        // use arrow keys or wasd
14        // enter to select
15
16        // list:
17        //  show all values
18        //  surround the selected value with brackets
19        list("li", vec!["Alice", "Bob", "Charlie"]),
20
21        // scroll:
22        //  show only the selected item
23        scroll("sc", vec!["Alice", "Bob", "Charlie"]),
24
25        button("exit")
26    ]);
27    run(&menu);
28    {
29        let mm = mut_menu(&menu);
30        if mm.canceled() {
31            println!("Canceled!");
32            return;
33        }
34        println!("{}", mm.selection_value("li"));
35        println!("{}", mm.selection_value("sc"));
36    }
37}
examples/submenus.rs (line 41)
5fn main() {
6    use terminal_menu::{menu, label, button, scroll, back_button, submenu, run, mut_menu};
7    let menu = menu(vec![
8        label("submenus"),
9
10        // submenu:
11        //  submenus are basically menus inside menus
12        submenu("sub", vec![
13            scroll("scr", vec!["Alice", "Bob", "Charlie"]),
14
15            // back button:
16            //  back buttons return to the parent menu.
17            back_button("back")
18        ]),
19
20        submenu("ret", vec![
21
22            // button:
23            //  buttons exit all the menus
24            button("Alice"),
25            button("Bob"),
26            button("Charlie"),
27
28            submenu("nested", vec![
29                button("Alice"),
30                button("Bob"),
31                button("Charlie"),
32            ])
33
34        ]),
35
36        button("exit")
37    ]);
38    run(&menu);
39
40    // name of the menu active before exiting
41    println!("{:?}", mut_menu(&menu).get_latest_menu_name());
42
43    // pull values
44    println!("{}", mut_menu(&menu).get_submenu("sub").selection_value("scr"));
45}
examples/string_and_numeric.rs (line 43)
5fn main() {
6    use terminal_menu::{menu, label, button, string, password, numeric, run, mut_menu};
7    let menu = menu(vec![
8        label("strings and numerics"),
9
10        // string:
11        //  a string of characters
12        //  the last arguments specifies if empty strings are allowed
13
14        // empty strings allowed:
15        string("ste", "default", true),
16
17        // empty strings not allowed:
18        string("stn", "default", false),
19
20        // password:
21        password("pass", "default", true),
22
23        // numeric:
24        //  a floating point number
25        numeric("num",
26            // default
27            4.5,
28
29            // step
30            Some(1.5),
31
32            // minimum
33            None,
34
35            // maximum
36            Some(150.0)
37        ),
38
39        button("exit")
40    ]);
41    run(&menu);
42    {
43        let mm = mut_menu(&menu);
44        if mm.canceled() {
45            println!("Canceled!");
46            return;
47        }
48        println!("{}", mm.selection_value("ste"));
49        println!("{}", mm.selection_value("stn"));
50        println!("{}", mm.selection_value("pass"));
51        println!("{}", mm.numeric_value("num"));
52    }
53}