Function terminal_menu::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/long.rs (line 14)
5
6
7
8
9
10
11
12
13
14
15
fn main() {
    use terminal_menu::{menu, button, run, mut_menu};
    let menu = menu(

        // create buttons representing numbers from 1 to 100
        (1..100).map(|n| button(format!("{}", n))).collect()

    );
    run(&menu);
    println!("{}", mut_menu(&menu).selected_item_name());
}
More examples
Hide additional examples
examples/cancel.rs (line 14)
5
6
7
8
9
10
11
12
13
14
15
fn main() {
    use terminal_menu::{menu, label, button, run, mut_menu};
    let menu = menu(vec![
        label("press the button or hit 'q' or esc!"),
        button("button")
    ]);
    run(&menu);

    // true if exited with 'q' or esc, false if button was pressed
    println!("{}", mut_menu(&menu).canceled());
}
examples/simple.rs (line 28)
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
fn main() {
    use terminal_menu::{menu, label, button, run, mut_menu};
    let menu = menu(vec![

        // label:
        //  not selectable, useful as a title, separator, etc...
        label("----------------------"),
        label("terminal-menu"),
        label("use wasd or arrow keys"),
        label("enter to select"),
        label("'q' or esc to exit"),
        label("-----------------------"),

        // button:
        //  exit the menu
        button("Alice"),
        button("Bob"),
        button("Charlie")

    ]);
    run(&menu);

    // you can get the selected buttons name like so:
    println!("Selected: {}", mut_menu(&menu).selected_item_name());
}
examples/selections.rs (line 29)
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
fn main() {
    use terminal_menu::{menu, label, button, list, scroll, run, mut_menu};
    let menu = menu(vec![
        label("lists and scrolls"),

        // with list and scroll you can select a value from a group of values
        // you can change the selected value with arrow keys, wasd, or enter

        // use arrow keys or wasd
        // enter to select

        // list:
        //  show all values
        //  surround the selected value with brackets
        list("li", vec!["Alice", "Bob", "Charlie"]),

        // scroll:
        //  show only the selected item
        scroll("sc", vec!["Alice", "Bob", "Charlie"]),

        button("exit")
    ]);
    run(&menu);
    {
        let mm = mut_menu(&menu);
        println!("{}", mm.selection_value("li"));
        println!("{}", mm.selection_value("sc"));
    }
}
examples/submenus.rs (line 35)
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
fn main() {
    use terminal_menu::{menu, label, button, scroll, back_button, submenu, run, mut_menu};
    let menu = menu(vec![
        label("submenus"),

        // submenu:
        //  submenus are basically menus inside menus
        submenu("sub", vec![
            scroll("scr", vec!["Alice", "Bob", "Charlie"]),

            // back button:
            //  back buttons return to the parent menu.
            back_button("back")
        ]),

        submenu("ret", vec![

            // button:
            //  buttons exit all the menus
            button("Alice"),
            button("Bob"),
            button("Charlie"),

        ]),

        button("exit")
    ]);
    run(&menu);

    // name of the menu active before exiting
    println!("{:?}", mut_menu(&menu).get_latest_menu_name());

    // pull values
    println!("{}", mut_menu(&menu).get_submenu("sub").selection_value("scr"));
}
examples/string_and_numeric.rs (line 40)
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
fn main() {
    use terminal_menu::{menu, label, button, string, numeric, run, mut_menu};
    let menu = menu(vec![
        label("strings and numerics"),

        // string:
        //  a string of characters
        //  the last arguments specifies if empty strings are allowed

        // empty strings allowed:
        string("ste", "default", true),

        // empty strings not allowed:
        string("stn", "default", false),

        // numeric:
        //  a floating point number
        numeric("num",
            // default
            4.5,

            // step
            Some(1.5),

            // minimum
            None,

            // maximum
            Some(150.0)
        ),

        button("exit")
    ]);
    run(&menu);
    {
        let mm = mut_menu(&menu);
        println!("{}", mm.selection_value("ste"));
        println!("{}", mm.selection_value("stn"));
        println!("{}", mm.numeric_value("num"));
    }
}