pub struct Form { /* private fields */ }Expand description
A form with fields and navigation.
Implementations§
Source§impl Form
impl Form
Sourcepub fn builder() -> FormBuilder
pub fn builder() -> FormBuilder
Creates a new form builder.
Examples found in repository?
examples/address_form.rs (line 27)
18fn main() -> io::Result<()> {
19 // Setup terminal
20 enable_raw_mode()?;
21 let mut stdout = io::stdout();
22 execute!(stdout, EnterAlternateScreen)?;
23 let backend = CrosstermBackend::new(stdout);
24 let mut terminal = Terminal::new(backend)?;
25
26 // Create the form
27 let mut form = Form::builder()
28 .title("Shipping Information")
29 .text("name", "Full Name")
30 .placeholder("John Doe")
31 .required()
32 .done()
33 .text("email", "Email")
34 .placeholder("john@example.com")
35 .required()
36 .validator(Box::new(ratatui_form::Email))
37 .done()
38 .text("phone", "Phone")
39 .placeholder("(555) 123-4567")
40 .done()
41 .block(AddressBlock::new("shipping").required())
42 .checkbox("newsletter", "Subscribe to newsletter")
43 .done()
44 .checkbox("terms", "I agree to the terms and conditions")
45 .required()
46 .done()
47 .build();
48
49 // Main loop
50 loop {
51 // Render
52 terminal.draw(|frame| {
53 let area = frame.area();
54 form.render(area, frame.buffer_mut());
55 })?;
56
57 // Handle input
58 if let Event::Key(key_event) = event::read()? {
59 // Quick exit with Ctrl+C
60 if key_event.code == KeyCode::Char('c')
61 && key_event.modifiers.contains(event::KeyModifiers::CONTROL)
62 {
63 break;
64 }
65
66 form.handle_input(key_event);
67
68 match form.result() {
69 FormResult::Submitted => {
70 // Write JSON and exit
71 form.write_json("shipping.json")?;
72 break;
73 }
74 FormResult::Cancelled => {
75 break;
76 }
77 FormResult::Active => {}
78 }
79 }
80 }
81
82 // Cleanup terminal
83 disable_raw_mode()?;
84 execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
85
86 // Print result
87 match form.result() {
88 FormResult::Submitted => {
89 println!("Form submitted! Data saved to shipping.json");
90 println!("\nForm data:");
91 println!("{}", serde_json::to_string_pretty(&form.to_json()).unwrap());
92 }
93 FormResult::Cancelled => {
94 println!("Form cancelled.");
95 }
96 FormResult::Active => {
97 println!("Form exited.");
98 }
99 }
100
101 Ok(())
102}Sourcepub fn result(&self) -> &FormResult
pub fn result(&self) -> &FormResult
Returns the current form result.
Examples found in repository?
examples/address_form.rs (line 68)
18fn main() -> io::Result<()> {
19 // Setup terminal
20 enable_raw_mode()?;
21 let mut stdout = io::stdout();
22 execute!(stdout, EnterAlternateScreen)?;
23 let backend = CrosstermBackend::new(stdout);
24 let mut terminal = Terminal::new(backend)?;
25
26 // Create the form
27 let mut form = Form::builder()
28 .title("Shipping Information")
29 .text("name", "Full Name")
30 .placeholder("John Doe")
31 .required()
32 .done()
33 .text("email", "Email")
34 .placeholder("john@example.com")
35 .required()
36 .validator(Box::new(ratatui_form::Email))
37 .done()
38 .text("phone", "Phone")
39 .placeholder("(555) 123-4567")
40 .done()
41 .block(AddressBlock::new("shipping").required())
42 .checkbox("newsletter", "Subscribe to newsletter")
43 .done()
44 .checkbox("terms", "I agree to the terms and conditions")
45 .required()
46 .done()
47 .build();
48
49 // Main loop
50 loop {
51 // Render
52 terminal.draw(|frame| {
53 let area = frame.area();
54 form.render(area, frame.buffer_mut());
55 })?;
56
57 // Handle input
58 if let Event::Key(key_event) = event::read()? {
59 // Quick exit with Ctrl+C
60 if key_event.code == KeyCode::Char('c')
61 && key_event.modifiers.contains(event::KeyModifiers::CONTROL)
62 {
63 break;
64 }
65
66 form.handle_input(key_event);
67
68 match form.result() {
69 FormResult::Submitted => {
70 // Write JSON and exit
71 form.write_json("shipping.json")?;
72 break;
73 }
74 FormResult::Cancelled => {
75 break;
76 }
77 FormResult::Active => {}
78 }
79 }
80 }
81
82 // Cleanup terminal
83 disable_raw_mode()?;
84 execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
85
86 // Print result
87 match form.result() {
88 FormResult::Submitted => {
89 println!("Form submitted! Data saved to shipping.json");
90 println!("\nForm data:");
91 println!("{}", serde_json::to_string_pretty(&form.to_json()).unwrap());
92 }
93 FormResult::Cancelled => {
94 println!("Form cancelled.");
95 }
96 FormResult::Active => {
97 println!("Form exited.");
98 }
99 }
100
101 Ok(())
102}Sourcepub fn handle_input(&mut self, event: KeyEvent)
pub fn handle_input(&mut self, event: KeyEvent)
Handles keyboard input.
Examples found in repository?
examples/address_form.rs (line 66)
18fn main() -> io::Result<()> {
19 // Setup terminal
20 enable_raw_mode()?;
21 let mut stdout = io::stdout();
22 execute!(stdout, EnterAlternateScreen)?;
23 let backend = CrosstermBackend::new(stdout);
24 let mut terminal = Terminal::new(backend)?;
25
26 // Create the form
27 let mut form = Form::builder()
28 .title("Shipping Information")
29 .text("name", "Full Name")
30 .placeholder("John Doe")
31 .required()
32 .done()
33 .text("email", "Email")
34 .placeholder("john@example.com")
35 .required()
36 .validator(Box::new(ratatui_form::Email))
37 .done()
38 .text("phone", "Phone")
39 .placeholder("(555) 123-4567")
40 .done()
41 .block(AddressBlock::new("shipping").required())
42 .checkbox("newsletter", "Subscribe to newsletter")
43 .done()
44 .checkbox("terms", "I agree to the terms and conditions")
45 .required()
46 .done()
47 .build();
48
49 // Main loop
50 loop {
51 // Render
52 terminal.draw(|frame| {
53 let area = frame.area();
54 form.render(area, frame.buffer_mut());
55 })?;
56
57 // Handle input
58 if let Event::Key(key_event) = event::read()? {
59 // Quick exit with Ctrl+C
60 if key_event.code == KeyCode::Char('c')
61 && key_event.modifiers.contains(event::KeyModifiers::CONTROL)
62 {
63 break;
64 }
65
66 form.handle_input(key_event);
67
68 match form.result() {
69 FormResult::Submitted => {
70 // Write JSON and exit
71 form.write_json("shipping.json")?;
72 break;
73 }
74 FormResult::Cancelled => {
75 break;
76 }
77 FormResult::Active => {}
78 }
79 }
80 }
81
82 // Cleanup terminal
83 disable_raw_mode()?;
84 execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
85
86 // Print result
87 match form.result() {
88 FormResult::Submitted => {
89 println!("Form submitted! Data saved to shipping.json");
90 println!("\nForm data:");
91 println!("{}", serde_json::to_string_pretty(&form.to_json()).unwrap());
92 }
93 FormResult::Cancelled => {
94 println!("Form cancelled.");
95 }
96 FormResult::Active => {
97 println!("Form exited.");
98 }
99 }
100
101 Ok(())
102}Sourcepub fn to_json(&self) -> Value
pub fn to_json(&self) -> Value
Returns the form data as a JSON object.
Examples found in repository?
examples/address_form.rs (line 91)
18fn main() -> io::Result<()> {
19 // Setup terminal
20 enable_raw_mode()?;
21 let mut stdout = io::stdout();
22 execute!(stdout, EnterAlternateScreen)?;
23 let backend = CrosstermBackend::new(stdout);
24 let mut terminal = Terminal::new(backend)?;
25
26 // Create the form
27 let mut form = Form::builder()
28 .title("Shipping Information")
29 .text("name", "Full Name")
30 .placeholder("John Doe")
31 .required()
32 .done()
33 .text("email", "Email")
34 .placeholder("john@example.com")
35 .required()
36 .validator(Box::new(ratatui_form::Email))
37 .done()
38 .text("phone", "Phone")
39 .placeholder("(555) 123-4567")
40 .done()
41 .block(AddressBlock::new("shipping").required())
42 .checkbox("newsletter", "Subscribe to newsletter")
43 .done()
44 .checkbox("terms", "I agree to the terms and conditions")
45 .required()
46 .done()
47 .build();
48
49 // Main loop
50 loop {
51 // Render
52 terminal.draw(|frame| {
53 let area = frame.area();
54 form.render(area, frame.buffer_mut());
55 })?;
56
57 // Handle input
58 if let Event::Key(key_event) = event::read()? {
59 // Quick exit with Ctrl+C
60 if key_event.code == KeyCode::Char('c')
61 && key_event.modifiers.contains(event::KeyModifiers::CONTROL)
62 {
63 break;
64 }
65
66 form.handle_input(key_event);
67
68 match form.result() {
69 FormResult::Submitted => {
70 // Write JSON and exit
71 form.write_json("shipping.json")?;
72 break;
73 }
74 FormResult::Cancelled => {
75 break;
76 }
77 FormResult::Active => {}
78 }
79 }
80 }
81
82 // Cleanup terminal
83 disable_raw_mode()?;
84 execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
85
86 // Print result
87 match form.result() {
88 FormResult::Submitted => {
89 println!("Form submitted! Data saved to shipping.json");
90 println!("\nForm data:");
91 println!("{}", serde_json::to_string_pretty(&form.to_json()).unwrap());
92 }
93 FormResult::Cancelled => {
94 println!("Form cancelled.");
95 }
96 FormResult::Active => {
97 println!("Form exited.");
98 }
99 }
100
101 Ok(())
102}Sourcepub fn write_json(&self, path: impl AsRef<Path>) -> Result<()>
pub fn write_json(&self, path: impl AsRef<Path>) -> Result<()>
Writes the form data to a JSON file.
Examples found in repository?
examples/address_form.rs (line 71)
18fn main() -> io::Result<()> {
19 // Setup terminal
20 enable_raw_mode()?;
21 let mut stdout = io::stdout();
22 execute!(stdout, EnterAlternateScreen)?;
23 let backend = CrosstermBackend::new(stdout);
24 let mut terminal = Terminal::new(backend)?;
25
26 // Create the form
27 let mut form = Form::builder()
28 .title("Shipping Information")
29 .text("name", "Full Name")
30 .placeholder("John Doe")
31 .required()
32 .done()
33 .text("email", "Email")
34 .placeholder("john@example.com")
35 .required()
36 .validator(Box::new(ratatui_form::Email))
37 .done()
38 .text("phone", "Phone")
39 .placeholder("(555) 123-4567")
40 .done()
41 .block(AddressBlock::new("shipping").required())
42 .checkbox("newsletter", "Subscribe to newsletter")
43 .done()
44 .checkbox("terms", "I agree to the terms and conditions")
45 .required()
46 .done()
47 .build();
48
49 // Main loop
50 loop {
51 // Render
52 terminal.draw(|frame| {
53 let area = frame.area();
54 form.render(area, frame.buffer_mut());
55 })?;
56
57 // Handle input
58 if let Event::Key(key_event) = event::read()? {
59 // Quick exit with Ctrl+C
60 if key_event.code == KeyCode::Char('c')
61 && key_event.modifiers.contains(event::KeyModifiers::CONTROL)
62 {
63 break;
64 }
65
66 form.handle_input(key_event);
67
68 match form.result() {
69 FormResult::Submitted => {
70 // Write JSON and exit
71 form.write_json("shipping.json")?;
72 break;
73 }
74 FormResult::Cancelled => {
75 break;
76 }
77 FormResult::Active => {}
78 }
79 }
80 }
81
82 // Cleanup terminal
83 disable_raw_mode()?;
84 execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
85
86 // Print result
87 match form.result() {
88 FormResult::Submitted => {
89 println!("Form submitted! Data saved to shipping.json");
90 println!("\nForm data:");
91 println!("{}", serde_json::to_string_pretty(&form.to_json()).unwrap());
92 }
93 FormResult::Cancelled => {
94 println!("Form cancelled.");
95 }
96 FormResult::Active => {
97 println!("Form exited.");
98 }
99 }
100
101 Ok(())
102}Sourcepub fn validation_errors(&self) -> &[ValidationError]
pub fn validation_errors(&self) -> &[ValidationError]
Returns validation errors.
Sourcepub fn render(&self, area: Rect, buf: &mut Buffer)
pub fn render(&self, area: Rect, buf: &mut Buffer)
Renders the form to a buffer.
Examples found in repository?
examples/address_form.rs (line 54)
18fn main() -> io::Result<()> {
19 // Setup terminal
20 enable_raw_mode()?;
21 let mut stdout = io::stdout();
22 execute!(stdout, EnterAlternateScreen)?;
23 let backend = CrosstermBackend::new(stdout);
24 let mut terminal = Terminal::new(backend)?;
25
26 // Create the form
27 let mut form = Form::builder()
28 .title("Shipping Information")
29 .text("name", "Full Name")
30 .placeholder("John Doe")
31 .required()
32 .done()
33 .text("email", "Email")
34 .placeholder("john@example.com")
35 .required()
36 .validator(Box::new(ratatui_form::Email))
37 .done()
38 .text("phone", "Phone")
39 .placeholder("(555) 123-4567")
40 .done()
41 .block(AddressBlock::new("shipping").required())
42 .checkbox("newsletter", "Subscribe to newsletter")
43 .done()
44 .checkbox("terms", "I agree to the terms and conditions")
45 .required()
46 .done()
47 .build();
48
49 // Main loop
50 loop {
51 // Render
52 terminal.draw(|frame| {
53 let area = frame.area();
54 form.render(area, frame.buffer_mut());
55 })?;
56
57 // Handle input
58 if let Event::Key(key_event) = event::read()? {
59 // Quick exit with Ctrl+C
60 if key_event.code == KeyCode::Char('c')
61 && key_event.modifiers.contains(event::KeyModifiers::CONTROL)
62 {
63 break;
64 }
65
66 form.handle_input(key_event);
67
68 match form.result() {
69 FormResult::Submitted => {
70 // Write JSON and exit
71 form.write_json("shipping.json")?;
72 break;
73 }
74 FormResult::Cancelled => {
75 break;
76 }
77 FormResult::Active => {}
78 }
79 }
80 }
81
82 // Cleanup terminal
83 disable_raw_mode()?;
84 execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
85
86 // Print result
87 match form.result() {
88 FormResult::Submitted => {
89 println!("Form submitted! Data saved to shipping.json");
90 println!("\nForm data:");
91 println!("{}", serde_json::to_string_pretty(&form.to_json()).unwrap());
92 }
93 FormResult::Cancelled => {
94 println!("Form cancelled.");
95 }
96 FormResult::Active => {
97 println!("Form exited.");
98 }
99 }
100
101 Ok(())
102}Auto Trait Implementations§
impl Freeze for Form
impl !RefUnwindSafe for Form
impl Send for Form
impl Sync for Form
impl Unpin for Form
impl !UnwindSafe for Form
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