pub struct Menu<T>{ /* private fields */ }Expand description
the menu itself
Implementations§
Source§impl<T: Send + Sync> Menu<T>
impl<T: Send + Sync> Menu<T>
Sourcepub fn set_colorscheme(&mut self, cs: ColorScheme) -> &mut Self
pub fn set_colorscheme(&mut self, cs: ColorScheme) -> &mut Self
Examples found in repository?
examples/complex.rs (line 51)
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
fn main() {
let mut menu = Menu::new().unwrap_or_else(|e| {
quit_now!("Error: {}", e);
});
// build item list
let mut item_list = Vec::new();
for i in 1..=100 {
item_list.push(Item::new(format!("{}th item", i).as_str(), i));
}
// build colorscheme, you can skip this step to use the default colorscheme
// NOTE: sometimes your terminal might not support all the colors, so you may need to adjust
// the colorscheme
let mut colorscheme = termenu::ColorScheme::default();
colorscheme
.set_title_style(
FontStyle::default()
.set_shape(FontShape::Bold | FontShape::Underline)
.set_fg_color(colored::Color::Green)
.build(),
)
.set_query_style(FontStyle::default().set_shape(FontShape::Italic).build())
.set_chosen_ln_style(
FontStyle::default()
.set_shape(FontShape::Underline)
.set_fg_color(colored::Color::Black)
.set_bg_color_256((215, 255, 0))
.build(),
)
.set_more_tag_style(
FontStyle::default()
.set_fg_color(colored::Color::Magenta)
.build(),
);
// run
let selection = menu
.set_title("test selection:")
.show_end_tag(true)
.set_max_height(0.3)
.set_colorscheme(colorscheme)
.add_list(item_list)
.select() // this is the menu entry
.unwrap_or_else(|e| {
quit_now!("Error: {}", e);
});
// handle selection
if let Some(selection) = selection {
println!("You selected: {}", selection);
} else {
println!("You didn't select anything");
}
}Source§impl<T: Send + Sync> Menu<T>
impl<T: Send + Sync> Menu<T>
Sourcepub fn new() -> Result<Menu<T>>
pub fn new() -> Result<Menu<T>>
Examples found in repository?
More examples
examples/basic.rs (line 2)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
fn main() {
let mut menu = termenu::Menu::new().unwrap();
let mut item_list = Vec::new();
for i in 1..=10 {
item_list.push(termenu::Item::new(format!("{}th item", i).as_str(), i));
}
let selection = menu
.set_title("test selection:")
.add_list(item_list)
.select()
.unwrap();
if let Some(selection) = selection {
println!("You selected: {}", selection);
}
}examples/non-ascii.rs (line 2)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
fn main() {
let mut menu = termenu::Menu::new().unwrap();
let mut item_list = Vec::new();
for i in 1..=10 {
item_list.push(termenu::Item::new(
format!("{}th オプション/选项/옵션", i).as_str(),
i,
));
}
let selection = menu
.set_title("test selection:")
.add_list(item_list)
.select()
.unwrap();
if let Some(selection) = selection {
println!("You selected: {}", selection);
}
}examples/register_fn.rs (line 2)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn main() {
let mut menu = termenu::Menu::new().unwrap();
let mut item_list = Vec::new();
for i in 1..=10 {
item_list.push(termenu::Item::new(
format!("{}th item", &i).as_str(),
move || {
println!("you selected: {}", i);
},
));
}
let selection = menu
.set_title("test selection:")
.add_list(item_list)
.select()
.unwrap();
if let Some(selection) = selection {
selection();
}
}examples/complex.rs (line 11)
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
fn main() {
let mut menu = Menu::new().unwrap_or_else(|e| {
quit_now!("Error: {}", e);
});
// build item list
let mut item_list = Vec::new();
for i in 1..=100 {
item_list.push(Item::new(format!("{}th item", i).as_str(), i));
}
// build colorscheme, you can skip this step to use the default colorscheme
// NOTE: sometimes your terminal might not support all the colors, so you may need to adjust
// the colorscheme
let mut colorscheme = termenu::ColorScheme::default();
colorscheme
.set_title_style(
FontStyle::default()
.set_shape(FontShape::Bold | FontShape::Underline)
.set_fg_color(colored::Color::Green)
.build(),
)
.set_query_style(FontStyle::default().set_shape(FontShape::Italic).build())
.set_chosen_ln_style(
FontStyle::default()
.set_shape(FontShape::Underline)
.set_fg_color(colored::Color::Black)
.set_bg_color_256((215, 255, 0))
.build(),
)
.set_more_tag_style(
FontStyle::default()
.set_fg_color(colored::Color::Magenta)
.build(),
);
// run
let selection = menu
.set_title("test selection:")
.show_end_tag(true)
.set_max_height(0.3)
.set_colorscheme(colorscheme)
.add_list(item_list)
.select() // this is the menu entry
.unwrap_or_else(|e| {
quit_now!("Error: {}", e);
});
// handle selection
if let Some(selection) = selection {
println!("You selected: {}", selection);
} else {
println!("You didn't select anything");
}
}Sourcepub fn show_end_tag(&mut self, b: bool) -> &mut Self
pub fn show_end_tag(&mut self, b: bool) -> &mut Self
Set if show the ‘—end—’ tag at the end of the menu
Examples found in repository?
examples/complex.rs (line 49)
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
fn main() {
let mut menu = Menu::new().unwrap_or_else(|e| {
quit_now!("Error: {}", e);
});
// build item list
let mut item_list = Vec::new();
for i in 1..=100 {
item_list.push(Item::new(format!("{}th item", i).as_str(), i));
}
// build colorscheme, you can skip this step to use the default colorscheme
// NOTE: sometimes your terminal might not support all the colors, so you may need to adjust
// the colorscheme
let mut colorscheme = termenu::ColorScheme::default();
colorscheme
.set_title_style(
FontStyle::default()
.set_shape(FontShape::Bold | FontShape::Underline)
.set_fg_color(colored::Color::Green)
.build(),
)
.set_query_style(FontStyle::default().set_shape(FontShape::Italic).build())
.set_chosen_ln_style(
FontStyle::default()
.set_shape(FontShape::Underline)
.set_fg_color(colored::Color::Black)
.set_bg_color_256((215, 255, 0))
.build(),
)
.set_more_tag_style(
FontStyle::default()
.set_fg_color(colored::Color::Magenta)
.build(),
);
// run
let selection = menu
.set_title("test selection:")
.show_end_tag(true)
.set_max_height(0.3)
.set_colorscheme(colorscheme)
.add_list(item_list)
.select() // this is the menu entry
.unwrap_or_else(|e| {
quit_now!("Error: {}", e);
});
// handle selection
if let Some(selection) = selection {
println!("You selected: {}", selection);
} else {
println!("You didn't select anything");
}
}Sourcepub fn set_max_height(&mut self, percent: f32) -> &mut Self
pub fn set_max_height(&mut self, percent: f32) -> &mut Self
Set the max height of the menu, should be a percentage in range (0, 1], otherwise it will be ignored
Examples found in repository?
examples/complex.rs (line 50)
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
fn main() {
let mut menu = Menu::new().unwrap_or_else(|e| {
quit_now!("Error: {}", e);
});
// build item list
let mut item_list = Vec::new();
for i in 1..=100 {
item_list.push(Item::new(format!("{}th item", i).as_str(), i));
}
// build colorscheme, you can skip this step to use the default colorscheme
// NOTE: sometimes your terminal might not support all the colors, so you may need to adjust
// the colorscheme
let mut colorscheme = termenu::ColorScheme::default();
colorscheme
.set_title_style(
FontStyle::default()
.set_shape(FontShape::Bold | FontShape::Underline)
.set_fg_color(colored::Color::Green)
.build(),
)
.set_query_style(FontStyle::default().set_shape(FontShape::Italic).build())
.set_chosen_ln_style(
FontStyle::default()
.set_shape(FontShape::Underline)
.set_fg_color(colored::Color::Black)
.set_bg_color_256((215, 255, 0))
.build(),
)
.set_more_tag_style(
FontStyle::default()
.set_fg_color(colored::Color::Magenta)
.build(),
);
// run
let selection = menu
.set_title("test selection:")
.show_end_tag(true)
.set_max_height(0.3)
.set_colorscheme(colorscheme)
.add_list(item_list)
.select() // this is the menu entry
.unwrap_or_else(|e| {
quit_now!("Error: {}", e);
});
// handle selection
if let Some(selection) = selection {
println!("You selected: {}", selection);
} else {
println!("You didn't select anything");
}
}Sourcepub fn set_title(&mut self, t: &str) -> &mut Self
pub fn set_title(&mut self, t: &str) -> &mut Self
Set the title of the menu, which will be displayed at the top of the menu
Examples found in repository?
examples/basic.rs (line 8)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
fn main() {
let mut menu = termenu::Menu::new().unwrap();
let mut item_list = Vec::new();
for i in 1..=10 {
item_list.push(termenu::Item::new(format!("{}th item", i).as_str(), i));
}
let selection = menu
.set_title("test selection:")
.add_list(item_list)
.select()
.unwrap();
if let Some(selection) = selection {
println!("You selected: {}", selection);
}
}More examples
examples/non-ascii.rs (line 11)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
fn main() {
let mut menu = termenu::Menu::new().unwrap();
let mut item_list = Vec::new();
for i in 1..=10 {
item_list.push(termenu::Item::new(
format!("{}th オプション/选项/옵션", i).as_str(),
i,
));
}
let selection = menu
.set_title("test selection:")
.add_list(item_list)
.select()
.unwrap();
if let Some(selection) = selection {
println!("You selected: {}", selection);
}
}examples/register_fn.rs (line 13)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn main() {
let mut menu = termenu::Menu::new().unwrap();
let mut item_list = Vec::new();
for i in 1..=10 {
item_list.push(termenu::Item::new(
format!("{}th item", &i).as_str(),
move || {
println!("you selected: {}", i);
},
));
}
let selection = menu
.set_title("test selection:")
.add_list(item_list)
.select()
.unwrap();
if let Some(selection) = selection {
selection();
}
}examples/run_multiple_times.rs (line 15)
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
fn run_once(menu: &mut termenu::Menu<i32>) {
println!("[start running once]");
let mut item_list = Vec::new();
for i in 1..=10 {
item_list.push(termenu::Item::new(format!("{}th item", i).as_str(), i));
}
let selection = menu
.set_title("test selection:")
.add_list(item_list)
.select()
.unwrap();
if let Some(selection) = selection {
println!("You selected: {}", selection);
} else {
println!("You didn't select anything");
}
menu.reset().unwrap();
}examples/complex.rs (line 48)
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
fn main() {
let mut menu = Menu::new().unwrap_or_else(|e| {
quit_now!("Error: {}", e);
});
// build item list
let mut item_list = Vec::new();
for i in 1..=100 {
item_list.push(Item::new(format!("{}th item", i).as_str(), i));
}
// build colorscheme, you can skip this step to use the default colorscheme
// NOTE: sometimes your terminal might not support all the colors, so you may need to adjust
// the colorscheme
let mut colorscheme = termenu::ColorScheme::default();
colorscheme
.set_title_style(
FontStyle::default()
.set_shape(FontShape::Bold | FontShape::Underline)
.set_fg_color(colored::Color::Green)
.build(),
)
.set_query_style(FontStyle::default().set_shape(FontShape::Italic).build())
.set_chosen_ln_style(
FontStyle::default()
.set_shape(FontShape::Underline)
.set_fg_color(colored::Color::Black)
.set_bg_color_256((215, 255, 0))
.build(),
)
.set_more_tag_style(
FontStyle::default()
.set_fg_color(colored::Color::Magenta)
.build(),
);
// run
let selection = menu
.set_title("test selection:")
.show_end_tag(true)
.set_max_height(0.3)
.set_colorscheme(colorscheme)
.add_list(item_list)
.select() // this is the menu entry
.unwrap_or_else(|e| {
quit_now!("Error: {}", e);
});
// handle selection
if let Some(selection) = selection {
println!("You selected: {}", selection);
} else {
println!("You didn't select anything");
}
}pub fn add(&mut self, item: Item<T>) -> &mut Self
Sourcepub fn add_list(&mut self, items: Vec<Item<T>>) -> &mut Self
pub fn add_list(&mut self, items: Vec<Item<T>>) -> &mut Self
Examples found in repository?
examples/basic.rs (line 9)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
fn main() {
let mut menu = termenu::Menu::new().unwrap();
let mut item_list = Vec::new();
for i in 1..=10 {
item_list.push(termenu::Item::new(format!("{}th item", i).as_str(), i));
}
let selection = menu
.set_title("test selection:")
.add_list(item_list)
.select()
.unwrap();
if let Some(selection) = selection {
println!("You selected: {}", selection);
}
}More examples
examples/non-ascii.rs (line 12)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
fn main() {
let mut menu = termenu::Menu::new().unwrap();
let mut item_list = Vec::new();
for i in 1..=10 {
item_list.push(termenu::Item::new(
format!("{}th オプション/选项/옵션", i).as_str(),
i,
));
}
let selection = menu
.set_title("test selection:")
.add_list(item_list)
.select()
.unwrap();
if let Some(selection) = selection {
println!("You selected: {}", selection);
}
}examples/register_fn.rs (line 14)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn main() {
let mut menu = termenu::Menu::new().unwrap();
let mut item_list = Vec::new();
for i in 1..=10 {
item_list.push(termenu::Item::new(
format!("{}th item", &i).as_str(),
move || {
println!("you selected: {}", i);
},
));
}
let selection = menu
.set_title("test selection:")
.add_list(item_list)
.select()
.unwrap();
if let Some(selection) = selection {
selection();
}
}examples/run_multiple_times.rs (line 16)
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
fn run_once(menu: &mut termenu::Menu<i32>) {
println!("[start running once]");
let mut item_list = Vec::new();
for i in 1..=10 {
item_list.push(termenu::Item::new(format!("{}th item", i).as_str(), i));
}
let selection = menu
.set_title("test selection:")
.add_list(item_list)
.select()
.unwrap();
if let Some(selection) = selection {
println!("You selected: {}", selection);
} else {
println!("You didn't select anything");
}
menu.reset().unwrap();
}examples/complex.rs (line 52)
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
fn main() {
let mut menu = Menu::new().unwrap_or_else(|e| {
quit_now!("Error: {}", e);
});
// build item list
let mut item_list = Vec::new();
for i in 1..=100 {
item_list.push(Item::new(format!("{}th item", i).as_str(), i));
}
// build colorscheme, you can skip this step to use the default colorscheme
// NOTE: sometimes your terminal might not support all the colors, so you may need to adjust
// the colorscheme
let mut colorscheme = termenu::ColorScheme::default();
colorscheme
.set_title_style(
FontStyle::default()
.set_shape(FontShape::Bold | FontShape::Underline)
.set_fg_color(colored::Color::Green)
.build(),
)
.set_query_style(FontStyle::default().set_shape(FontShape::Italic).build())
.set_chosen_ln_style(
FontStyle::default()
.set_shape(FontShape::Underline)
.set_fg_color(colored::Color::Black)
.set_bg_color_256((215, 255, 0))
.build(),
)
.set_more_tag_style(
FontStyle::default()
.set_fg_color(colored::Color::Magenta)
.build(),
);
// run
let selection = menu
.set_title("test selection:")
.show_end_tag(true)
.set_max_height(0.3)
.set_colorscheme(colorscheme)
.add_list(item_list)
.select() // this is the menu entry
.unwrap_or_else(|e| {
quit_now!("Error: {}", e);
});
// handle selection
if let Some(selection) = selection {
println!("You selected: {}", selection);
} else {
println!("You didn't select anything");
}
}Sourcepub fn reset(&mut self) -> Result<()>
pub fn reset(&mut self) -> Result<()>
reset the menu state and clear all the items that have been added, this will not reset the color scheme and other reusable settings
Examples found in repository?
examples/run_multiple_times.rs (line 24)
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
fn run_once(menu: &mut termenu::Menu<i32>) {
println!("[start running once]");
let mut item_list = Vec::new();
for i in 1..=10 {
item_list.push(termenu::Item::new(format!("{}th item", i).as_str(), i));
}
let selection = menu
.set_title("test selection:")
.add_list(item_list)
.select()
.unwrap();
if let Some(selection) = selection {
println!("You selected: {}", selection);
} else {
println!("You didn't select anything");
}
menu.reset().unwrap();
}Source§impl<T: Send + Sync> Menu<T>
impl<T: Send + Sync> Menu<T>
Sourcepub fn select(&mut self) -> Result<Option<&T>>
pub fn select(&mut self) -> Result<Option<&T>>
Start the menu and return the selection
if the user presses esc or ctrl-c, None will be returned
otherwise, the selected item will be returned
Examples found in repository?
examples/basic.rs (line 10)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
fn main() {
let mut menu = termenu::Menu::new().unwrap();
let mut item_list = Vec::new();
for i in 1..=10 {
item_list.push(termenu::Item::new(format!("{}th item", i).as_str(), i));
}
let selection = menu
.set_title("test selection:")
.add_list(item_list)
.select()
.unwrap();
if let Some(selection) = selection {
println!("You selected: {}", selection);
}
}More examples
examples/non-ascii.rs (line 13)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
fn main() {
let mut menu = termenu::Menu::new().unwrap();
let mut item_list = Vec::new();
for i in 1..=10 {
item_list.push(termenu::Item::new(
format!("{}th オプション/选项/옵션", i).as_str(),
i,
));
}
let selection = menu
.set_title("test selection:")
.add_list(item_list)
.select()
.unwrap();
if let Some(selection) = selection {
println!("You selected: {}", selection);
}
}examples/register_fn.rs (line 15)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn main() {
let mut menu = termenu::Menu::new().unwrap();
let mut item_list = Vec::new();
for i in 1..=10 {
item_list.push(termenu::Item::new(
format!("{}th item", &i).as_str(),
move || {
println!("you selected: {}", i);
},
));
}
let selection = menu
.set_title("test selection:")
.add_list(item_list)
.select()
.unwrap();
if let Some(selection) = selection {
selection();
}
}examples/run_multiple_times.rs (line 17)
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
fn run_once(menu: &mut termenu::Menu<i32>) {
println!("[start running once]");
let mut item_list = Vec::new();
for i in 1..=10 {
item_list.push(termenu::Item::new(format!("{}th item", i).as_str(), i));
}
let selection = menu
.set_title("test selection:")
.add_list(item_list)
.select()
.unwrap();
if let Some(selection) = selection {
println!("You selected: {}", selection);
} else {
println!("You didn't select anything");
}
menu.reset().unwrap();
}examples/complex.rs (line 53)
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
fn main() {
let mut menu = Menu::new().unwrap_or_else(|e| {
quit_now!("Error: {}", e);
});
// build item list
let mut item_list = Vec::new();
for i in 1..=100 {
item_list.push(Item::new(format!("{}th item", i).as_str(), i));
}
// build colorscheme, you can skip this step to use the default colorscheme
// NOTE: sometimes your terminal might not support all the colors, so you may need to adjust
// the colorscheme
let mut colorscheme = termenu::ColorScheme::default();
colorscheme
.set_title_style(
FontStyle::default()
.set_shape(FontShape::Bold | FontShape::Underline)
.set_fg_color(colored::Color::Green)
.build(),
)
.set_query_style(FontStyle::default().set_shape(FontShape::Italic).build())
.set_chosen_ln_style(
FontStyle::default()
.set_shape(FontShape::Underline)
.set_fg_color(colored::Color::Black)
.set_bg_color_256((215, 255, 0))
.build(),
)
.set_more_tag_style(
FontStyle::default()
.set_fg_color(colored::Color::Magenta)
.build(),
);
// run
let selection = menu
.set_title("test selection:")
.show_end_tag(true)
.set_max_height(0.3)
.set_colorscheme(colorscheme)
.add_list(item_list)
.select() // this is the menu entry
.unwrap_or_else(|e| {
quit_now!("Error: {}", e);
});
// handle selection
if let Some(selection) = selection {
println!("You selected: {}", selection);
} else {
println!("You didn't select anything");
}
}Trait Implementations§
Auto Trait Implementations§
impl<T> !Freeze for Menu<T>
impl<T> !RefUnwindSafe for Menu<T>
impl<T> Send for Menu<T>
impl<T> Sync for Menu<T>
impl<T> Unpin for Menu<T>where
T: Unpin,
impl<T> !UnwindSafe for Menu<T>
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
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more