pub struct TerminalMenuStruct {
pub items: Vec<TerminalMenuItem>,
/* private fields */
}Fields§
§items: Vec<TerminalMenuItem>Implementations§
Source§impl TerminalMenuStruct
impl TerminalMenuStruct
Sourcepub fn selected_item_name(&self) -> &str
pub fn selected_item_name(&self) -> &str
Returns the name of the selected menu item.
§Example
use terminal_menu::{menu, button, run, mut_menu};
let my_menu = menu(vec![
button("a"),
button("b"),
]);
run(&my_menu);
println!("selected item name: {}", mut_menu(&my_menu).selected_item_name()); //"a" or "b"Examples found in repository?
examples/long.rs (line 18)
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}More examples
examples/simple.rs (line 34)
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}Sourcepub fn selected_item_index(&self) -> usize
pub fn selected_item_index(&self) -> usize
Returns the selected item as an index of the items vec.
§Example
use terminal_menu::{menu, button, run, mut_menu};
let my_menu = menu(vec![
button("a"),
button("b"),
]);
run(&my_menu);
println!("selected item index: {}", mut_menu(&my_menu).selected_item_index()); // 0 or 1Sourcepub fn set_selected_item_with_name(&mut self, item: &str)
pub fn set_selected_item_with_name(&mut self, item: &str)
Set the selected item with a name.
§Example
use terminal_menu::{TerminalMenu, menu, button, mut_menu};
let my_menu: TerminalMenu = menu(vec![
button("item"),
button("other item")
]);
mut_menu(&my_menu).set_selected_item_with_name("item");Sourcepub fn set_selected_item_with_index(&mut self, item: usize)
pub fn set_selected_item_with_index(&mut self, item: usize)
Set the selected item with an index of the items vec.
§Example
use terminal_menu::{TerminalMenu, menu, button, mut_menu};
let my_menu: TerminalMenu = menu(vec![
button("item"),
button("other item")
]);
mut_menu(&my_menu).set_selected_item_with_index(1); //index 1 = other itemSourcepub fn selection_value(&self, name: &str) -> &str
pub fn selection_value(&self, name: &str) -> &str
Returns the value of the specified scroll, list, or string item.
§Example
use terminal_menu::{TerminalMenu, menu, scroll, run, mut_menu};
let my_menu: TerminalMenu = menu(vec![
scroll("item", vec!["val1", "val2"])
]);
run(&my_menu);
println!("item value: {}", mut_menu(&my_menu).selection_value("item"));Examples found in repository?
examples/selections.rs (line 34)
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}More examples
examples/submenus.rs (line 44)
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 48)
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}Sourcepub fn numeric_value(&self, name: &str) -> f64
pub fn numeric_value(&self, name: &str) -> f64
Returns the value of the specified numeric item.
§Example
use terminal_menu::{TerminalMenu, menu, scroll, run, numeric, mut_menu};
let my_menu: TerminalMenu = menu(vec![
numeric("item", 0.0, None, None, None)
]);
run(&my_menu);
println!("item value: {}", mut_menu(&my_menu).numeric_value("item"));Examples found in repository?
examples/string_and_numeric.rs (line 51)
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}Returns the specified submenu.
§Example
use terminal_menu::{TerminalMenu, menu, run, submenu, scroll, mut_menu};
let my_menu: TerminalMenu = menu(vec![
submenu("sub",vec![
scroll("item", vec!["winnie", "the", "pooh"])
])
]);
run(&my_menu);
println!("{}", mut_menu(&my_menu).get_submenu("sub").selection_value("item"));Examples found in repository?
examples/submenus.rs (line 44)
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}Returns the menu (or submenu) which was active on deactivation.
Examples found in repository?
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}Sourcepub fn canceled(&self) -> bool
pub fn canceled(&self) -> bool
Returns true if menu was exited with ‘q’ or esc
§Example
use terminal_menu::{menu, button, run, mut_menu};
let menu = menu(vec![
button("button")
]);
run(&menu);
// true if esc, false if button
println!("{}", mut_menu(&menu).canceled());Examples found in repository?
More 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 30)
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/string_and_numeric.rs (line 44)
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}Auto Trait Implementations§
impl Freeze for TerminalMenuStruct
impl RefUnwindSafe for TerminalMenuStruct
impl Send for TerminalMenuStruct
impl Sync for TerminalMenuStruct
impl Unpin for TerminalMenuStruct
impl UnsafeUnpin for TerminalMenuStruct
impl UnwindSafe for TerminalMenuStruct
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more