Class: Clack::Prompts::Date

Inherits:
Core::Prompt show all
Defined in:
lib/clack/prompts/date.rb

Overview

Date picker prompt with inline segmented input.

Features:

  • Three formats: :iso (YYYY-MM-DD), :us (MM/DD/YYYY), :eu (DD/MM/YYYY)

  • Arrow key navigation between segments

  • Up/down to increment/decrement values

  • Direct digit typing with auto-advance

  • Min/max date bounds validation

Examples:

Basic usage

date = Clack.date(message: "Select a date")

With bounds

date = Clack.date(
  message: "When?",
  min: Date.today,
  max: Date.today + 365,
  format: :us
)

Constant Summary collapse

FORMATS =

Supported date format configurations mapping format symbol to segment order and separator.

{
  iso: {order: [:year, :month, :day], sep: "-"},
  us: {order: [:month, :day, :year], sep: "/"},
  eu: {order: [:day, :month, :year], sep: "/"}
}.freeze
DAYS_IN_MONTH =

Non-leap-year days per month (index 1-12; index 0 unused).

[nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31].freeze
KEY_SHIFT_TAB =

ANSI escape sequence for Shift+Tab

"\e[Z"

Constants inherited from Core::Prompt

Core::Prompt::MIN_TERMINAL_WIDTH

Instance Attribute Summary

Attributes inherited from Core::Prompt

#error_message, #state, #value, #warning_message

Instance Method Summary collapse

Methods inherited from Core::Prompt

flush_resize, register, #request_redraw, #run, setup_signal_handler, unregister

Constructor Details

#initialize(message:, format: :iso, initial_value: nil, min: nil, max: nil, **opts) ⇒ Date

Returns a new instance of Date.

Parameters:

  • message (String)

    the prompt message

  • format (Symbol) (defaults to: :iso)

    date format (:iso, :us, :eu)

  • initial_value (Date, Time, String, nil) (defaults to: nil)

    initial date value

  • min (Date, nil) (defaults to: nil)

    minimum allowed date

  • max (Date, nil) (defaults to: nil)

    maximum allowed date

  • opts (Hash)

    additional options passed to Core::Prompt

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/clack/prompts/date.rb', line 45

def initialize(message:, format: :iso, initial_value: nil, min: nil, max: nil, **opts)
  super(message:, **opts)

  raise ArgumentError, "Unknown format: #{format}" unless FORMATS.key?(format)
  raise ArgumentError, "min must be before or equal to max" if min && max && min > max

  @format = format
  @min = min
  @max = max
  @segment = 0
  @input_buffer = ""

  init_date(initial_value)
end