Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to specify dates with special styling #181

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- Add strict clippy lints to improve code consistency and readability.
- Expand workflow clippy task to lint all-features in workspace.
- Add docs badge to readme.
- Add date select option to apply style to a list of given days [#181](https://github.com/mikaelmello/inquire/pull/181).

### Fixes

Expand Down
13 changes: 13 additions & 0 deletions inquire/src/prompts/dateselect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ pub struct DateSelect<'a> {
/// Max date allowed to be selected.
pub max_date: Option<NaiveDate>,

/// Dates to mark with special styling.
pub marked_dates: &'a [NaiveDate],

/// Help message to be presented to the user.
pub help_message: Option<&'a str>,

Expand Down Expand Up @@ -133,13 +136,17 @@ impl<'a> DateSelect<'a> {
/// Default max date.
pub const DEFAULT_MAX_DATE: Option<NaiveDate> = None;

/// Default marked dates.
pub const DEFAULT_MARKED_DATES: &'a [NaiveDate] = &[];

/// Creates a [DateSelect] with the provided message, along with default configuration values.
pub fn new(message: &'a str) -> Self {
Self {
message,
starting_date: get_current_date(),
min_date: Self::DEFAULT_MIN_DATE,
max_date: Self::DEFAULT_MAX_DATE,
marked_dates: Self::DEFAULT_MARKED_DATES,
help_message: Self::DEFAULT_HELP_MESSAGE,
vim_mode: Self::DEFAULT_VIM_MODE,
formatter: Self::DEFAULT_FORMATTER,
Expand Down Expand Up @@ -184,6 +191,12 @@ impl<'a> DateSelect<'a> {
self
}

/// Sets the marked dates.
pub fn with_marked_dates(mut self, marked_dates: &'a [NaiveDate]) -> Self {
self.marked_dates = marked_dates;
self
}

/// Sets the starting date. Equivalent to [DateSelect::with_default](DateSelect::with_default).
pub fn with_starting_date(mut self, starting_date: NaiveDate) -> Self {
self.starting_date = starting_date;
Expand Down
3 changes: 3 additions & 0 deletions inquire/src/prompts/dateselect/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct DateSelectPrompt<'a> {
help_message: Option<&'a str>,
formatter: DateFormatter<'a>,
validators: Vec<Box<dyn DateValidator>>,
marked_dates: &'a [NaiveDate],
error: Option<ErrorMessage>,
}

Expand Down Expand Up @@ -51,6 +52,7 @@ impl<'a> DateSelectPrompt<'a> {
help_message: so.help_message,
formatter: so.formatter,
validators: so.validators,
marked_dates: so.marked_dates,
error: None,
})
}
Expand Down Expand Up @@ -180,6 +182,7 @@ where
self.current_date,
self.config.min_date,
self.config.max_date,
self.marked_dates,
)?;

if let Some(help_message) = self.help_message {
Expand Down
4 changes: 4 additions & 0 deletions inquire/src/ui/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ pub mod date {
selected_date: chrono::NaiveDate,
min_date: Option<chrono::NaiveDate>,
max_date: Option<chrono::NaiveDate>,
marked_dates: &[chrono::NaiveDate],
) -> Result<()>;
}

Expand All @@ -617,6 +618,7 @@ pub mod date {
selected_date: chrono::NaiveDate,
min_date: Option<chrono::NaiveDate>,
max_date: Option<chrono::NaiveDate>,
marked_dates: &[chrono::NaiveDate],
) -> Result<()> {
macro_rules! write_prefix {
() => {{
Expand Down Expand Up @@ -695,6 +697,8 @@ pub mod date {
}
} else if date_it == today {
style_sheet = self.render_config.calendar.today_date;
} else if marked_dates.contains(&date_it) {
style_sheet = self.render_config.calendar.marked_date;
} else if date_it.month() != month.number_from_month() {
style_sheet = self.render_config.calendar.different_month_date;
}
Expand Down
9 changes: 9 additions & 0 deletions inquire/src/ui/render_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,8 @@ impl<'a> ErrorMessageRenderConfig<'a> {
pub mod calendar {
//! Module containing additional render config for date prompts.

use crate::ui::Attributes;

use super::{Color, StyleSheet, Styled};

/// Calendar configuration for error messages.
Expand Down Expand Up @@ -467,6 +469,9 @@ pub mod calendar {
/// Style sheet for dates that can not be selected due to the
/// min/max settings.
pub unavailable_date: StyleSheet,

/// Style sheet for marked dates
pub marked_date: StyleSheet,
}

impl<'a> CalendarRenderConfig<'a> {
Expand All @@ -480,6 +485,7 @@ pub mod calendar {
today_date: StyleSheet::empty(),
different_month_date: StyleSheet::empty(),
unavailable_date: StyleSheet::empty(),
marked_date: StyleSheet::empty(),
}
}

Expand All @@ -497,6 +503,9 @@ pub mod calendar {
today_date: StyleSheet::empty().with_fg(Color::LightGreen),
different_month_date: StyleSheet::empty().with_fg(Color::DarkGrey),
unavailable_date: StyleSheet::empty().with_fg(Color::DarkGrey),
marked_date: StyleSheet::empty()
.with_attr(Attributes::BOLD)
.with_fg(Color::DarkGreen),
}
}

Expand Down