EZMenu
Fast designing menus for your Rust CLI programs.
This crate provides a derive(Menu) procedural macro to easily build menus.
It includes type-checking from the user input, and a formatting customization.
Documentation
You can find all the crate documentation on Docs.rs. You can also check the examples to learn with a practical way.
Example
Here is an example of how to use it:
use Write;
use ;
To display the menu, you instantiate the struct by calling its from_menu method:
let MyMenu = from_menu;
println!;
This sample code prints the standard menu like above:
Hello there!
* author: Ahmad
* Give a number: 1000
ok!
values provided: author=Ahmad, number=1000
Format it as you wish
You can apply several formatting rules on a menu and on a field specifically. You can edit:
- the chip:
*by default. - the prefix:
:by default. - insert a new line before prefix and user input:
falseby default. - display default values or not:
trueby default.
Example
For a custom format on a field and a main formatting rule on a menu, you can build this with:
The custom >> will be applied on every field except those with custom formatting rules.
In this case, it will format the text like above:
- author: ...
>> date: ...
Skip fields with default values
You can provide default values to a field like above:
If the user provided an incorrect input, the program will not re-ask a value to the user, but will directly return the default value instead.
By default, the default value is visible. If you want to hide it, you can do so:
on the struct or on a field.
Custom I/O types
If you are not using std::io::Stdin and std::io::Stdout types, you can provide your own
types by enabling the custom_io feature in your Cargo.toml file:
[]
= { = "0.2.3", = ["custom_io"] }
Then you can instantiate your struct with:
use stdout;
let input = b"Ahmad\n1000\n" as &;
let values = from_io;
Use custom value types
If the user has to provide a value which corresponds to your specific type,
you can use the ezmenu::parsed on this type.
For example, in the case of a mk-license program, the menu can be built like above:
This will restrict the user to enter "MIT", "BSD" or "GPL" inputs ignoring the case.
Derive feature
The derive(Menu) is available with the derive feature, enabled by default.
You can disable it in your Cargo.toml file:
[]
= { = "0.2.3", = false }
You can still use the provided library to build your menus.
Example
To ask a simple value, you can use StructField::build method by giving the Stdin
and Stdout types.
use ;
use StructField;
let age: u8 = from
.build.unwrap;
If you want to build a menu with all the previous features (default values, formatting rules...), you can refer to this code below:
use ;
let mut menu = default
.title
.fmt
.with_field
.with_field
.with_field;
let name: String = menu.next_map.unwrap;
let proj_name: String = menu.next.unwrap;
let proj_year: i64 = menu.next.unwrap;
WIP
This project is still in development. You can check the EZMenu project to see all the next features.