datepicker

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package datepicker is a keyboard-navigable calendar widget operating directly on time.Time (stdlib time only, no external date library).

Example

Arrow keys move the cursor by a day or a week; Enter selects the date.

package main

import (
	"fmt"
	"time"

	"github.com/ows4444/tui"
	"github.com/ows4444/tui/datepicker"
)

func main() {
	m := datepicker.New(time.Date(2026, time.March, 10, 0, 0, 0, 0, time.UTC))
	m, _ = m.Update(tui.Key{Type: tui.KeyRight})
	m, _ = m.Update(tui.Key{Type: tui.KeyDown})
	fmt.Println(m.Cursor().Format("2006-01-02"))
	_, cmd := m.Update(tui.Key{Type: tui.KeyEnter})
	fmt.Println(cmd().(datepicker.SelectedMsg).Date.Format("Mon 2 Jan"))
}
Output:
2026-03-18
Wed 18 Mar

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type KeyMap

type KeyMap struct {
	PrevDay    keymap.Binding // move the cursor back one day
	NextDay    keymap.Binding // move the cursor forward one day
	PrevWeek   keymap.Binding // move the cursor back one week
	NextWeek   keymap.Binding // move the cursor forward one week
	MonthStart keymap.Binding // move the cursor to the first of the month
	MonthEnd   keymap.Binding // move the cursor to the last of the month
	Select     keymap.Binding // confirm the highlighted date
}

KeyMap names the keys of each action of a Model.

func DefaultKeyMap

func DefaultKeyMap() KeyMap

DefaultKeyMap returns the keys a Model used before KeyMap existed.

type Model

type Model struct {
	Value   time.Time
	MinDate time.Time
	MaxDate time.Time
	Theme   theme.Theme

	// KeyMap holds the keys for each action. New fills it with
	// DefaultKeyMap; a Model built as a struct literal with a zero KeyMap
	// behaves as if it held DefaultKeyMap.
	KeyMap KeyMap

	// Mouse, when true, makes Update handle tui.MouseEvent: a left click on a
	// day moves the cursor to it (a click on the highlighted day confirms it,
	// like Enter) and the wheel moves the cursor by a month. Days outside
	// [MinDate, MaxDate] ignore clicks. Off (the default) ignores the mouse.
	Mouse bool
	// Bounds is the screen rectangle where the app draws the calendar (its
	// first row is the weekday header); mouse events outside it are ignored.
	Bounds hittest.Rect
	// contains filtered or unexported fields
}

Model is a single-month calendar grid. Value is the last confirmed date (set by New and left untouched by navigation — only Enter updates it, via the caller applying SelectedMsg). MinDate and MaxDate, when non-zero, bound both cursor movement and which days View renders as enabled.

func New

func New(value time.Time) Model

New builds a Model with the cursor (and Value) on value, or today if value is the zero time.Time.

func (Model) Bindings

func (m Model) Bindings() []keymap.Binding

Bindings returns the actions the picker currently honours, with descriptions, for help text.

func (Model) Cursor

func (m Model) Cursor() time.Time

Cursor returns the day currently highlighted.

func (Model) LayoutNode

func (m Model) LayoutNode() layout.Node

LayoutNode adapts the calendar to a layout.Node. Measure reports its natural size (the weekday row plus one row per week of the cursor's month). Render fits the allotted Size: the weekday row stays, and when there are more weeks than rows it shows a window of weeks containing the cursor's week; columns are cut to the width. The Model is not changed.

func (Model) Linearize

func (m Model) Linearize() string

Linearize renders the calendar as plain text for accessible output (see tui.Linearizer): the date the cursor is on (", unavailable" if it is outside MinDate and MaxDate), the confirmed value if there is one, and the allowed range if one is set. The month grid is not spoken.

func (Model) SetTheme

func (m Model) SetTheme(t theme.Theme) Model

SetTheme returns m with t applied. It makes Model a tui.ThemeSetter, so a root model can forward the Program's theme (see tui.WithTheme).

func (Model) Tokens

func (m Model) Tokens() theme.Tokens

Tokens returns the colour tokens the widget renders with: its theme's roles, overridden by any theme.WithTokens(theme.ComponentDatePicker, ...) and then by WithTokens.

func (Model) Update

func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)

Update moves the cursor by day (Left/Right), by week (Up/Down), to the start/end of its month (Home/End), and confirms it on Enter, returning a Cmd that delivers SelectedMsg. Movement is clamped to [MinDate, MaxDate] whenever those are set (non-zero). With Mouse on it also handles tui.MouseEvent (see Model.Mouse). Any other Msg is a no-op.

func (Model) View

func (m Model) View() string

View renders a 7-column calendar grid for the cursor's month: a weekday header row, then one row per week. The cursor day is highlighted (reverse+bold); any day outside [MinDate, MaxDate] (when set) is dimmed (Faint).

func (Model) WithTokens

func (m Model) WithTokens(tok theme.Tokens) Model

WithTokens returns m with tok as its per-instance colour override. Nil fields inherit from the theme, so only the roles tok names change; the theme and every other widget are untouched. A second call replaces the first.

type SelectedMsg

type SelectedMsg struct {
	Date time.Time
}

SelectedMsg is delivered (via the Cmd Update returns) when the highlighted date is confirmed with Enter.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL