Documentation
¶
Overview ¶
Package colorpicker is a palette-swatch + hex-input color picker.
It is deliberately not built on picker.Model's View: round 11 of the widget-catalog gap analysis found that wrapping a separately-Render-ed swatch style in an outer cursor-highlight Render call clobbers the highlight, because ansi.Style.Render always appends a full SGR reset with no save/restore (see ansi/style.go). Model.View instead computes ONE combined ansi.Style per swatch — swatch color and cursor highlight together — and calls Render exactly once per swatch.
Example ¶
Move across the palette and press Enter to choose the color under the cursor.
package main
import (
"fmt"
"github.com/ows4444/tui"
"github.com/ows4444/tui/ansi"
"github.com/ows4444/tui/colorpicker"
)
func main() {
m := colorpicker.New(ansi.BasicColor(1), ansi.BasicColor(2), ansi.BasicColor(4))
m, _ = m.Update(tui.Key{Type: tui.KeyRight})
fmt.Println(m.Cursor())
_, cmd := m.Update(tui.Key{Type: tui.KeyEnter})
fmt.Println(cmd().(colorpicker.SelectedMsg).Color == ansi.BasicColor(2))
}
Output: 1 true
Index ¶
- type KeyMap
- type Model
- func (m Model) Bindings() []keymap.Binding
- func (m Model) Cursor() int
- func (m Model) HexFocused() bool
- func (m Model) LayoutNode() layout.Node
- func (m Model) Linearize() string
- func (m Model) SetTheme(t theme.Theme) Model
- func (m Model) Tokens() theme.Tokens
- func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)
- func (m Model) View() string
- func (m Model) WithTokens(tok theme.Tokens) Model
- type SelectedMsg
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type KeyMap ¶
type KeyMap struct {
Prev keymap.Binding // move the palette cursor left
Next keymap.Binding // move the palette cursor right
Accept keymap.Binding // confirm the swatch, or the hex value
SwitchPane keymap.Binding // toggle focus between palette and hex input
}
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 {
Palette []ansi.Color
HexInput textinput.Model
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. While the hex input has focus,
// keys other than SwitchPane and Accept go to HexInput.
KeyMap KeyMap
// Mouse, when true, makes Update handle tui.MouseEvent: a left click on a
// swatch moves the cursor to it and gives the palette focus (a click on
// the highlighted swatch confirms it, like Enter), a left click on the hex
// input focuses it, and the wheel moves the palette cursor. Off (the
// default) ignores the mouse.
Mouse bool
// Bounds is the screen rectangle where the app draws the picker (one
// row); mouse events outside it are ignored.
Bounds hittest.Rect
// contains filtered or unexported fields
}
Model is a keyboard-navigable palette of ansi.Color swatches plus an embedded hex text input, for callers who want an arbitrary RGB color rather than one of Palette's fixed entries.
func New ¶
New builds a Model from palette, with the cursor on the first entry and focus on the palette (not the hex input).
func (Model) Bindings ¶
Bindings returns the actions active for the focused pane, with descriptions, for help text: SwitchPane and Accept, then the palette movement keys, or the editing keys of HexInput while it has focus.
func (Model) HexFocused ¶
HexFocused reports whether keys are currently forwarded to HexInput rather than moving the palette cursor.
func (Model) LayoutNode ¶
LayoutNode adapts the picker to a layout.Node: the swatches keep their room and the hex input scrolls its value in whatever width is left, all cut to the allotted Size. The Model is not changed.
func (Model) Linearize ¶
Linearize renders the picker as plain text for accessible output (see tui.Linearizer): one line per palette swatch with its colour in words, its position and ", selected" on the cursor swatch (none while the hex field has focus), then the hex input's own line.
func (Model) SetTheme ¶
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 ¶
Tokens returns the colour tokens the widget renders with: its theme's roles, overridden by any theme.WithTokens(theme.ComponentColorPicker, ...) and then by WithTokens.
func (Model) Update ¶
Update moves the cursor among Palette on Left/Right (clamped at the ends, not wrapping — Home/End style navigation isn't in scope here, so clamping keeps repeated Left/Right presses inert at the boundary instead of silently jumping to the opposite end), confirms the highlighted swatch on Enter, and toggles hex-input focus on Tab. While hex-focused, keys other than Tab are forwarded to HexInput.Update, and Enter attempts to parse HexInput.Value() as a "#RRGGBB" hex color: a valid value confirms it via SelectedMsg, an invalid one is a no-op (no panic, no Cmd). With Mouse on it also handles tui.MouseEvent (see Model.Mouse).
func (Model) View ¶
View renders the palette swatches followed by the hex input. Each swatch is rendered with a single combined ansi.Style built for that swatch specifically (background color alone for a plain swatch, background + bold + inverse-text for the cursor swatch), then Render is called exactly once on the whole swatch block — never a base style Render wrapped inside a separate cursor-highlight Render.
type SelectedMsg ¶
SelectedMsg is delivered (via the Cmd Update returns) when a color is confirmed, either from the palette (Enter on a swatch) or from the hex input (Enter while hex-focused, with a valid value).