inquire/prompts/dateselect/mod.rs
1mod action;
2mod config;
3mod prompt;
4#[cfg(test)]
5#[cfg(feature = "crossterm")]
6mod test;
7
8pub use action::*;
9
10use chrono::NaiveDate;
11
12use crate::{
13 config::get_configuration,
14 date_utils::get_current_date,
15 error::{InquireError, InquireResult},
16 formatter::{self, DateFormatter},
17 prompts::prompt::Prompt,
18 terminal::get_default_terminal,
19 ui::{date::DateSelectBackend, Backend, RenderConfig},
20 validator::DateValidator,
21};
22
23use self::prompt::DateSelectPrompt;
24
25/// Prompt that allows user to select a date (time not supported) from an interactive calendar. Available via the `date` feature.
26///
27/// By default, the initial selected date is the current date. The user can navigate through the calendar by pressing the keyboard arrows. If the user also presses the control key along with the arrows, the user will be able to "fast-forward" to previous or next months or years.
28///
29/// More specifically:
30/// - Left arrow moves to the day previous to the one selected, and to the month previous to the one selected when pressed with `ctrl`.
31/// - Analogously, right arrow does the same, but moving to the next day or month.
32/// - Up arrow moves to the day above to the one selected, basically a week before the selected date. When pressed with `ctrl`, it moves to the previous year.
33/// - Analogously, the down arrow moves to a week later or a year later.
34///
35/// Finally, the user selects a date by pressing the space or enter keys.
36///
37/// `DateSelect` prompts provide several options of configuration:
38///
39/// - **Prompt message**: Required when creating the prompt.
40/// - **Default value**: Default value selected when the calendar is displayed and the one select if the user submits without any previous actions. Current date by default.
41/// - **Help message**: Message displayed at the line below the prompt.
42/// - **Formatter**: Custom formatter in case you need to pre-process the user input before showing it as the final answer.
43/// - Formats to "Month Day, Year" by default.
44/// - **Validators**: Custom validators to the user's selected date, displaying an error message if the date does not pass the requirements.
45/// - **Week start**: Which day of the week should be displayed in the first column of the calendar, Sunday by default.
46/// - **Min and max date**: Inclusive boundaries of allowed dates in the interactive calendar. If any boundary is set, the user will not be able to move past them, consequently not being able to select any dates out of the allowed range.
47///
48/// # Example
49///
50/// ```no_run
51/// use chrono::{NaiveDate, Weekday};
52/// use inquire::DateSelect;
53///
54/// let date = DateSelect::new("When do you want to travel?")
55/// .with_starting_date(NaiveDate::from_ymd(2021, 8, 1))
56/// .with_min_date(NaiveDate::from_ymd(2021, 8, 1))
57/// .with_max_date(NaiveDate::from_ymd(2021, 12, 31))
58/// .with_week_start(Weekday::Mon)
59/// .with_help_message("Possible flights will be displayed according to the selected date")
60/// .prompt();
61///
62/// match date {
63/// Ok(_) => println!("No flights available for this date."),
64/// Err(_) => println!("There was an error in the system."),
65/// }
66/// ```
67#[derive(Clone)]
68pub struct DateSelect<'a> {
69 /// Message to be presented to the user.
70 pub message: &'a str,
71
72 /// First day of the week when displaying week rows.
73 pub week_start: chrono::Weekday,
74
75 /// Starting date to be selected.
76 pub starting_date: NaiveDate,
77
78 /// Min date allowed to be selected.
79 pub min_date: Option<NaiveDate>,
80
81 /// Max date allowed to be selected.
82 pub max_date: Option<NaiveDate>,
83
84 /// Help message to be presented to the user.
85 pub help_message: Option<&'a str>,
86
87 /// Function that formats the user input and presents it to the user as the final rendering of the prompt.
88 pub formatter: DateFormatter<'a>,
89
90 /// Collection of validators to apply to the user input.
91 ///
92 /// Validators are executed in the order they are stored, stopping at and displaying to the user
93 /// only the first validation error that might appear.
94 ///
95 /// The possible error is displayed to the user one line above the prompt.
96 pub validators: Vec<Box<dyn DateValidator>>,
97
98 /// RenderConfig to apply to the rendered interface.
99 ///
100 /// Note: The default render config considers if the NO_COLOR environment variable
101 /// is set to decide whether to render the colored config or the empty one.
102 ///
103 /// When overriding the config in a prompt, NO_COLOR is no longer considered and your
104 /// config is treated as the only source of truth. If you want to customize colors
105 /// and still support NO_COLOR, you will have to do this on your end.
106 pub render_config: RenderConfig<'a>,
107}
108
109impl<'a> DateSelect<'a> {
110 /// Default formatter, set to [DEFAULT_DATE_FORMATTER](crate::formatter::DEFAULT_DATE_FORMATTER)
111 pub const DEFAULT_FORMATTER: DateFormatter<'a> = formatter::DEFAULT_DATE_FORMATTER;
112
113 /// Default value of vim mode. It is true because there is no typing functionality to be lost here.
114 pub const DEFAULT_VIM_MODE: bool = true;
115
116 /// Default help message.
117 pub const DEFAULT_HELP_MESSAGE: Option<&'a str> =
118 Some("arrows to move, []{} move months and years, enter to select");
119
120 /// Default validators added to the [DateSelect] prompt, none.
121 pub const DEFAULT_VALIDATORS: Vec<Box<dyn DateValidator>> = vec![];
122
123 /// Default week start.
124 pub const DEFAULT_WEEK_START: chrono::Weekday = chrono::Weekday::Sun;
125
126 /// Default min date.
127 pub const DEFAULT_MIN_DATE: Option<NaiveDate> = None;
128
129 /// Default max date.
130 pub const DEFAULT_MAX_DATE: Option<NaiveDate> = None;
131
132 /// Creates a [DateSelect] with the provided message, along with default configuration values.
133 pub fn new(message: &'a str) -> Self {
134 Self {
135 message,
136 starting_date: get_current_date(),
137 min_date: Self::DEFAULT_MIN_DATE,
138 max_date: Self::DEFAULT_MAX_DATE,
139 help_message: Self::DEFAULT_HELP_MESSAGE,
140 formatter: Self::DEFAULT_FORMATTER,
141 validators: Self::DEFAULT_VALIDATORS,
142 week_start: Self::DEFAULT_WEEK_START,
143 render_config: get_configuration(),
144 }
145 }
146
147 /// Sets the help message of the prompt.
148 pub fn with_help_message(mut self, message: &'a str) -> Self {
149 self.help_message = Some(message);
150 self
151 }
152
153 /// Removes the set help message.
154 pub fn without_help_message(mut self) -> Self {
155 self.help_message = None;
156 self
157 }
158
159 /// Sets the default date of the prompt. Equivalent to [DateSelect::with_starting_date](DateSelect::with_starting_date).
160 pub fn with_default(self, default: NaiveDate) -> Self {
161 self.with_starting_date(default)
162 }
163
164 /// Sets the week start.
165 pub fn with_week_start(mut self, week_start: chrono::Weekday) -> Self {
166 self.week_start = week_start;
167 self
168 }
169
170 /// Sets the min date.
171 pub fn with_min_date(mut self, min_date: NaiveDate) -> Self {
172 self.min_date = Some(min_date);
173 self
174 }
175
176 /// Sets the max date.
177 pub fn with_max_date(mut self, max_date: NaiveDate) -> Self {
178 self.max_date = Some(max_date);
179 self
180 }
181
182 /// Sets the starting date. Equivalent to [DateSelect::with_default](DateSelect::with_default).
183 pub fn with_starting_date(mut self, starting_date: NaiveDate) -> Self {
184 self.starting_date = starting_date;
185 self
186 }
187
188 /// Adds a validator to the collection of validators. You might want to use this feature
189 /// in case you need to limit the user to specific choices, such as not allowing weekends.
190 ///
191 /// Validators are executed in the order they are stored, stopping at and displaying to the user
192 /// only the first validation error that might appear.
193 ///
194 /// The possible error is displayed to the user one line above the prompt.
195 pub fn with_validator<V>(mut self, validator: V) -> Self
196 where
197 V: DateValidator + 'static,
198 {
199 self.validators.push(Box::new(validator));
200 self
201 }
202
203 /// Adds the validators to the collection of validators in the order they are given.
204 /// You might want to use this feature in case you need to limit the user to specific
205 /// choices, such as not allowing weekends.
206 ///
207 /// Validators are executed in the order they are stored, stopping at and displaying to the user
208 /// only the first validation error that might appear.
209 ///
210 /// The possible error is displayed to the user one line above the prompt.
211 pub fn with_validators(mut self, validators: &[Box<dyn DateValidator>]) -> Self {
212 for validator in validators {
213 self.validators.push(validator.clone());
214 }
215 self
216 }
217
218 /// Sets the formatter.
219 pub fn with_formatter(mut self, formatter: DateFormatter<'a>) -> Self {
220 self.formatter = formatter;
221 self
222 }
223
224 /// Sets the provided color theme to this prompt.
225 ///
226 /// Note: The default render config considers if the NO_COLOR environment variable
227 /// is set to decide whether to render the colored config or the empty one.
228 ///
229 /// When overriding the config in a prompt, NO_COLOR is no longer considered and your
230 /// config is treated as the only source of truth. If you want to customize colors
231 /// and still support NO_COLOR, you will have to do this on your end.
232 pub fn with_render_config(mut self, render_config: RenderConfig<'a>) -> Self {
233 self.render_config = render_config;
234 self
235 }
236
237 /// Parses the provided behavioral and rendering options and prompts
238 /// the CLI user for input according to the defined rules.
239 ///
240 /// This method is intended for flows where the user skipping/cancelling
241 /// the prompt - by pressing ESC - is considered normal behavior. In this case,
242 /// it does not return `Err(InquireError::OperationCanceled)`, but `Ok(None)`.
243 ///
244 /// Meanwhile, if the user does submit an answer, the method wraps the return
245 /// type with `Some`.
246 pub fn prompt_skippable(self) -> InquireResult<Option<NaiveDate>> {
247 match self.prompt() {
248 Ok(answer) => Ok(Some(answer)),
249 Err(InquireError::OperationCanceled) => Ok(None),
250 Err(err) => Err(err),
251 }
252 }
253
254 /// Parses the provided behavioral and rendering options and prompts
255 /// the CLI user for input according to the defined rules.
256 pub fn prompt(self) -> InquireResult<NaiveDate> {
257 let (input_reader, terminal) = get_default_terminal()?;
258 let mut backend = Backend::new(input_reader, terminal, self.render_config)?;
259 self.prompt_with_backend(&mut backend)
260 }
261
262 pub(crate) fn prompt_with_backend<B: DateSelectBackend>(
263 self,
264 backend: &mut B,
265 ) -> InquireResult<NaiveDate> {
266 DateSelectPrompt::new(self)?.prompt(backend)
267 }
268}