errorretry

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package errorretry is a small interactive widget for showing an error with a retry/dismiss keybinding — Enter or 'r' retries (up to MaxRetries), Esc always dismisses.

Stability: experimental. Its API may change in any minor release.

Example

Enter asks for a retry until the allowed number is used up.

package main

import (
	"fmt"

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

func main() {
	m := errorretry.New("request failed", 1)
	m, cmd := m.Update(tui.Key{Type: tui.KeyEnter})
	fmt.Printf("%T %d %v\n", cmd(), m.RetryCount(), m.Exhausted())
}
Output:
errorretry.RetryMsg 1 true

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DismissedMsg

type DismissedMsg struct{}

DismissedMsg is delivered (via the Cmd Update returns) when the user dismisses the error with Esc.

type KeyMap

type KeyMap struct {
	Retry   keymap.Binding // request a retry while retries remain
	Dismiss keymap.Binding // dismiss the error
}

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 {
	Message    string
	MaxRetries int
	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. The hint line View draws names the
	// default keys.
	KeyMap KeyMap

	// Mouse, when true, makes Update handle tui.MouseEvent: a left click on the
	// "retry" part of the hint line retries and on the "dismiss" part dismisses,
	// as the keys would. Off (the default) ignores the mouse.
	Mouse bool
	// Bounds is the screen rectangle where the app draws the widget (its first
	// row is the first line of Message); clicks outside it are ignored.
	Bounds hittest.Rect
	// contains filtered or unexported fields
}

Model shows an error Message and lets the user retry (Enter or 'r', up to MaxRetries times) or dismiss (Esc). Retrying past MaxRetries is a no-op; Esc always works regardless of retryCount.

func New

func New(message string, maxRetries int) Model

New returns a Model for message, allowing up to maxRetries retries.

func (Model) Bindings

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

Bindings returns the actions the widget currently honours, with descriptions, for help text. Retry is left out once retries are exhausted.

func (Model) Exhausted

func (m Model) Exhausted() bool

Exhausted reports whether retries have been used up — further Enter/'r' presses are a no-op once true.

func (Model) LayoutNode

func (m Model) LayoutNode() layout.Node

LayoutNode adapts the error to a layout.Node: the message and the hint beneath it each word-wrap to the allotted width, keeping their styles. The Model is not changed.

func (Model) Linearize

func (m Model) Linearize() string

Linearize renders the error as plain text for accessible output (see tui.Linearizer): the message, how many retries have been used, and the keys that are available, e.g. "Error: connection lost", "Retries used: 1 of 3", "Press Enter or r to retry, Escape to dismiss".

func (Model) RetryCount

func (m Model) RetryCount() int

RetryCount reports how many retries have been requested so far.

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.ComponentErrorRetry, ...) and then by WithTokens.

func (Model) Update

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

Update handles Enter/'r' (retry, while retryCount < MaxRetries) and Esc (dismiss, always). Any other Msg, or Enter/'r' once retries are exhausted, is a no-op. With Mouse on, a left click on the retry or dismiss part of the hint line does the same as the key.

func (Model) View

func (m Model) View() string

View renders the error message and a hint line. The hint reflects whether retries remain: "press Enter/r to retry" while they do, and a visibly distinct "no retries left" (dimmed, no retry mention) once exhausted — Esc to dismiss is always offered either way.

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 RetryMsg

type RetryMsg struct{}

RetryMsg is delivered (via the Cmd Update returns) when the user requests a retry while retries remain.

Jump to

Keyboard shortcuts

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