PromptPlus

PromptPlus # PromptPlus ## **Calendar — Methods** [![NuGet](https://img.shields.io/badge/NuGet-PromptPlus-blue)](https://www.nuget.org/packages/PromptPlus) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![.NET](https://img.shields.io/badge/.NET-8%20%7C%209%20%7C%2010-512BD4)](https://dotnet.microsoft.com/)

← Back to HomeNext: Calendar — Operations →


Every fluent method on ICalendarControl. Each returns the same control instance, so calls chain in any order. Call Run last.

The factory is PromptPlus.Controls.Calendar(string prompt = "", string? description = null), which returns ICalendarControl.

Quick jump: Layout · Culture · FirstDayOfWeek · Range · DisableDates · DisabledWeekend · AddNote · AddNotes · PageSize · Highlights · Interaction · InteractionAsync · Default · PredicateSelected · PredicateSelectedAsync · ChangeDescription · ChangeDescriptionAsync · EnableHistory · Styles · Options · Run


Layout & culture

Layout

ICalendarControl Layout(CalendarLayout layout = CalendarLayout.SingleGrid)

Sets how the grid and its lines are drawn.

CalendarLayout Renders
SingleGrid Single-line box drawing (default)
DoubleGrid Double-line box drawing
AsciiSingleGrid ASCII single-line grid (portable, no Unicode)
AsciiDoubleGrid ASCII double-line grid
PromptPlus.Controls.Calendar("Date")
    .Layout(CalendarLayout.AsciiSingleGrid)
    .Run();

Culture

ICalendarControl Culture(CultureInfo culture)
ICalendarControl Culture(string cultureName)

Sets the culture used to display month names, day names, and date formatting — and to parse and validate dates. The default is the current PromptPlus culture. The string overload is shorthand for Culture(new CultureInfo(cultureName)).

using PromptPlusLibrary;
using System.Globalization;

PromptPlus.Controls.Calendar("Data")
    .Culture("pt-BR")
    .Run();

PromptPlus.Controls.Calendar("Date")
    .Culture(new CultureInfo("en-US"))
    .Run();

Culture(CultureInfo) throws ArgumentNullException if culture is null; Culture(string) throws ArgumentException if cultureName is null or empty.


FirstDayOfWeek

ICalendarControl FirstDayOfWeek(DayOfWeek firstDayOfWeek)

Sets which weekday appears in the first column. When not set, defaults to PromptPlus.Config.FirstDayOfWeek (DayOfWeek.Sunday unless you’ve changed it globally) — not the active culture’s own first-day-of-week convention.

using PromptPlusLibrary;
using System;

PromptPlus.Controls.Calendar("Date")
    .FirstDayOfWeek(DayOfWeek.Monday)
    .Run();

Restricting dates

Range

ICalendarControl Range(DateTime minValue, DateTime maxValue)

Defines an inclusive window of selectable dates. Days outside it are shown but cannot be confirmed.

using PromptPlusLibrary;
using System;

var today = DateTime.Now.Date;
PromptPlus.Controls.Calendar("Date")
    .Range(today.AddDays(-3), today.AddDays(3))
    .Run();

Throws ArgumentOutOfRangeException if minValue is greater than maxValue. A Default outside the range is ignored.


DisableDates

ICalendarControl DisableDates(params DateTime[] dates)

Marks specific dates as non-selectable (rendered with the Disabled style).

using PromptPlusLibrary;
using System;

var today = DateTime.Now.Date;
PromptPlus.Controls.Calendar("Date")
    .DisableDates(today.AddDays(1), today.AddDays(2))
    .Run();

Throws ArgumentNullException if dates is null.


DisabledWeekend

ICalendarControl DisabledWeekend(bool value = true)

Blocks Saturday and Sunday from selection. Default true when called.

PromptPlus.Controls.Calendar("Business date")
    .DisabledWeekend()
    .Run();

Notes

AddNote

ICalendarControl AddNote(DateTime value, string? note = null)

Attaches a note to a single date. A null note becomes an empty string. Notes for the highlighted day are shown when the user presses F2 (see Operations).

using PromptPlusLibrary;
using System;

PromptPlus.Controls.Calendar("Date", "Press [F2] to read notes")
    .AddNote(DateTime.Now.Date, "Team standup at 09:00")
    .Run();

AddNotes

ICalendarControl AddNotes((DateTime, string?)[] notes)

Adds several notes at once as (date, note) tuples. A null note becomes an empty string.

using PromptPlusLibrary;
using System;

var today = DateTime.Now.Date;
PromptPlus.Controls.Calendar("Date")
    .AddNotes(
    [
        (today.AddDays(1), "Tomorrow note"),
        (today.AddDays(2), "Day+2 note")
    ])
    .Run();

PageSize

ICalendarControl PageSize(byte value)

Maximum number of notes shown per page (valid range 0–255). 0 (default) auto-computes from terminal height, reserving lines for header, footer, and pagination. Values above the available height are clamped.

PromptPlus.Controls.Calendar("Date").PageSize(3).Run();

Highlights

Highlights

ICalendarControl Highlights(params DateTime[] dates)

Marks one or more dates so they stand out (rendered with the CalendarHighlight style). Highlighted dates remain selectable — the marking is purely visual.

using PromptPlusLibrary;
using System;

var today = DateTime.Now.Date;
PromptPlus.Controls.Calendar("Date")
    .Highlights(today, today.AddDays(3))
    .Run();

Throws ArgumentNullException if dates is null.


Loading from a source

Interaction

ICalendarControl Interaction<T>(IEnumerable<T> items, Action<T, ICalendarControl> interactionAction)

Iterates a source collection and lets you configure the calendar programmatically per item — useful for generating notes from data.

using PromptPlusLibrary;
using System;

var today = DateTime.Now.Date;
PromptPlus.Controls.Calendar("Date")
    .Interaction(myEvents, (evt, ctrl) => ctrl.AddNote(evt.Date, evt.Title))
    .Run();

Throws ArgumentNullException if items or interactionAction is null.


InteractionAsync

ICalendarControl InteractionAsync<T>(IEnumerable<T> items, Func<T, ICalendarControl, Task> interactionAction)

Asynchronous version of Interaction, for sources that need awaiting per item.

using PromptPlusLibrary;
using System;

var today = DateTime.Now.Date;
PromptPlus.Controls.Calendar("Date")
    .InteractionAsync([8, 9], async (offset, ctrl) =>
    {
        await Task.Delay(1).ConfigureAwait(false);
        ctrl.AddNote(today.AddDays(offset), $"Async note {offset}");
    })
    .Run();

Throws ArgumentNullException if items or interactionAction is null.


Initial value

Default

ICalendarControl Default(DateTime value, bool useDefaultHistory = true)

Opens the grid on value (default is today). When useDefaultHistory is true and history is enabled, the last history value is preferred. A value outside the Range is ignored.

using PromptPlusLibrary;
using System;

PromptPlus.Controls.Calendar("Date")
    .Default(DateTime.Now)
    .Run();

Validating the confirmed date

Validation runs on Enter. On failure the grid stays open and shows an error.

PredicateSelected

ICalendarControl PredicateSelected(Func<DateTime?, bool> isValidSelection)
ICalendarControl PredicateSelected(Func<DateTime?, (bool, string?)> validateSelection)
Overload Return Behavior
Func<DateTime?, bool> true = valid Generic error on failure
Func<DateTime?, (bool, string?)> (isValid, message) Custom message on failure
using PromptPlusLibrary;
using System;

// bool overload — only odd days allowed
PromptPlus.Controls.Calendar("Select odd day")
    .PredicateSelected(date => date.HasValue && date.Value.Day % 2 == 1)
    .Run();

// message overload — custom error text
PromptPlus.Controls.Calendar("Select day <= 28")
    .PredicateSelected(date =>
    {
        if (!date.HasValue)
            return (false, "Date is required");
        return date.Value.Day <= 28
            ? (true, (string?)null)
            : (false, "Only days up to 28 are allowed");
    })
    .Run();

PredicateSelectedAsync

ICalendarControl PredicateSelectedAsync(Func<DateTime?, Task<bool>> isValidSelection)
ICalendarControl PredicateSelectedAsync(Func<DateTime?, Task<(bool, string?)>> validateSelection)

Asynchronous counterparts of PredicateSelected.

using PromptPlusLibrary;
using System;

var today = DateTime.Now.Date;
PromptPlus.Controls.Calendar("Select future date")
    .PredicateSelectedAsync(async date =>
    {
        await Task.Delay(1).ConfigureAwait(false);
        return date.HasValue && date.Value.Date >= today;
    })
    .Run();

⚠️ The async predicate is awaited synchronously (blocking) on the UI thread — keep it fast.


Dynamic description

ChangeDescription

ICalendarControl ChangeDescription(Func<DateTime?, string> value)

Recomputes the description from the currently highlighted date as the user navigates.

using PromptPlusLibrary;
using System;

PromptPlus.Controls.Calendar("Date")
    .ChangeDescription(date => $"Selected day: {date:yyyy-MM-dd}")
    .Run();

Throws ArgumentNullException if value is null.


ChangeDescriptionAsync

ICalendarControl ChangeDescriptionAsync(Func<DateTime?, Task<string>> value)

Asynchronous version of ChangeDescription.

using PromptPlusLibrary;
using System;

PromptPlus.Controls.Calendar("Date")
    .ChangeDescriptionAsync(date => Task.FromResult($"Async: {date:dddd, dd MMM yyyy}"))
    .Run();

Throws ArgumentNullException if value is null.


History

EnableHistory

ICalendarControl EnableHistory(string filename, Action<IHistoryOptions>? options = null)

Persists confirmed dates to filename and can pre-select the last one (via Default(..., useDefaultHistory: true)). The IHistoryOptions builder is identical to the one documented for Input → EnableHistory (MinPrefixLength, MaxItems, ExpirationTime, FilterType, PageSize).

using PromptPlusLibrary;
using System;

PromptPlus.Controls.Calendar("Date")
    .Default(DateTime.Now, useDefaultHistory: true)
    .EnableHistory("calendar-history", opt => opt.MaxItems(5))
    .Run();

Throws ArgumentNullException if filename is null.


Appearance & behavior

Styles

ICalendarControl Styles(CalendarStyles styleType, Style style)

Recolors one visual region of this control. See the region list and examples on the Styles page.

using PromptPlusLibrary;
using ConsolePlusLibrary;   // Color, Style live here

PromptPlus.Controls.Calendar("Date")
    .Styles(CalendarStyles.Selected, new Style(Color.Green, Color.Default))
    .Run();

Throws ArgumentNullException if style is null.


Options

ICalendarControl Options(Action<IControlOptions> options)

Overrides global behaviors for this one control (prompt/description text, abort key, tooltip, hide-after-finish). See Global Behaviors → Per-Control Override.

PromptPlus.Controls.Calendar("Date")
    .Options(opt =>
    {
        opt.Description("Custom options sample");
        opt.ShowTooltip(true);
        opt.EnabledAbortKey(true);
    })
    .Run();

Throws ArgumentNullException if options is null.


Running the control

Run

ResultPrompt<DateTime?> Run(CancellationToken token = default)

Renders the grid and blocks until the user confirms (Enter) or aborts (Esc). Returns ResultPrompt<DateTime?> — the .Content is null when aborted.

var result = PromptPlus.Controls.Calendar("Date").Run();
if (!result.IsAborted && result.Content.HasValue)
    PromptPlus.Console.WriteLine(result.Content.Value.ToString("d"));

See also