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

Added option to set the calendar highlight style on a daily basis. #1254

Closed
wants to merge 1 commit into from
Closed
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
25 changes: 21 additions & 4 deletions src/Spectre.Console/Extensions/CalendarExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,39 @@ public static Calendar AddCalendarEvent(this Calendar calendar, string descripti
}

/// <summary>
/// Sets the calendar's highlight <see cref="Style"/>.
/// Adds a calendar event.
/// </summary>
/// <param name="calendar">The calendar.</param>
/// <param name="style">The highlight style.</param>
/// <param name="description">The calendar event description.</param>
/// <param name="year">The year of the calendar event.</param>
/// <param name="month">The month of the calendar event.</param>
/// <param name="day">The day of the calendar event.</param>
/// <param name="style">The style of the calendar event.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static Calendar HighlightStyle(this Calendar calendar, Style? style)
public static Calendar AddCalendarEvent(this Calendar calendar, string description, int year, int month, int day, Style style)
{
if (calendar is null)
{
throw new ArgumentNullException(nameof(calendar));
}

calendar.HighlightStyle = style ?? Style.Plain;
calendar.CalendarEvents.Add(new CalendarEvent(description, year, month, day, style));
return calendar;
}

/// <summary>
/// Adds a calendar event.
/// </summary>
/// <param name="calendar">The calendar to add the calendar event to.</param>
/// <param name="description">The calendar event description.</param>
/// <param name="date">The calendar event date.</param>
/// <param name="style">The style of the calendar event.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static Calendar AddCalendarEvent(this Calendar calendar, string description, DateTime date, Style style)
{
return AddCalendarEvent(calendar, description, date.Year, date.Month, date.Day, style);
}

/// <summary>
/// Sets the calendar's header <see cref="Style"/>.
/// </summary>
Expand Down
17 changes: 4 additions & 13 deletions src/Spectre.Console/Widgets/Calendar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public sealed class Calendar : JustInTimeRenderable, IHasCulture, IHasTableBorde
private bool _useSafeBorder;
private Style? _borderStyle;
private CultureInfo? _culture;
private Style _highlightStyle;
private bool _showHeader;
private Style? _headerStyle;
private Justify? _alignment;
Expand Down Expand Up @@ -79,15 +78,6 @@ public CultureInfo? Culture
set => MarkAsDirty(() => _culture = value);
}

/// <summary>
/// Gets or sets the calendar's highlight <see cref="Style"/>.
/// </summary>
public Style HighlightStyle
{
get => _highlightStyle;
set => MarkAsDirty(() => _highlightStyle = value);
}

/// <summary>
/// Gets or sets a value indicating whether or not the calendar header should be shown.
/// </summary>
Expand Down Expand Up @@ -153,7 +143,6 @@ public Calendar(int year, int month, int day)
_useSafeBorder = true;
_borderStyle = null;
_culture = CultureInfo.InvariantCulture;
_highlightStyle = Color.Blue;
_showHeader = true;
_calendarEvents = new ListWithCallback<CalendarEvent>(MarkAsDirty);
}
Expand Down Expand Up @@ -196,9 +185,11 @@ protected override IRenderable Build()
{
if (weekdays[currentDay - 1] == weekday)
{
if (_calendarEvents.Any(e => e.Month == Month && e.Day == currentDay))
var currentDayEvents = _calendarEvents.Where(e => e.Month == Month && e.Day == currentDay);

if (currentDayEvents is not null && currentDayEvents.Any())
{
row.Add(new Markup(currentDay.ToString(CultureInfo.InvariantCulture) + "*", _highlightStyle));
row.Add(new Markup(currentDay.ToString(CultureInfo.InvariantCulture) + "*", currentDayEvents.First().Style));
}
else
{
Expand Down
11 changes: 9 additions & 2 deletions src/Spectre.Console/Widgets/CalendarEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,19 @@ public sealed class CalendarEvent
/// </summary>
public int Day { get; }

/// <summary>
/// Gets the <see cref="Style"/> of the calendar event.
/// </summary>
public Style Style { get; }

/// <summary>
/// Initializes a new instance of the <see cref="CalendarEvent"/> class.
/// </summary>
/// <param name="year">The year of the calendar event.</param>
/// <param name="month">The month of the calendar event.</param>
/// <param name="day">The day of the calendar event.</param>
public CalendarEvent(int year, int month, int day)
: this(string.Empty, year, month, day)
: this(string.Empty, year, month, day, null)
{
}

Expand All @@ -43,11 +48,13 @@ public CalendarEvent(int year, int month, int day)
/// <param name="year">The year of the calendar event.</param>
/// <param name="month">The month of the calendar event.</param>
/// <param name="day">The day of the calendar event.</param>
public CalendarEvent(string description, int year, int month, int day)
/// <param name="style">The style of the calendar event.</param>
public CalendarEvent(string description, int year, int month, int day, Style? style = null)
{
Description = description ?? string.Empty;
Year = year;
Month = month;
Day = day;
Style = style ?? Style.Plain;
}
}