Struct terminal_menu::TerminalMenuStruct
source · 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?
More examples
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());
}
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 1
sourcepub 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 item
sourcepub 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 30)
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"));
}
}
More examples
examples/submenus.rs (line 38)
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 41)
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"));
}
}
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 43)
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"));
}
}
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 38)
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"));
}
Returns the menu (or submenu) which was active on deactivation.
Examples found in repository?
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"));
}
Auto Trait Implementations§
impl RefUnwindSafe for TerminalMenuStruct
impl Send for TerminalMenuStruct
impl Sync for TerminalMenuStruct
impl Unpin 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