toast

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: 10 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 toast is a transient auto-dismissing notification — InkUI's "Toast": a small bordered box in a screen corner, composited via layout.Overlay the same way dialog is, that closes itself after Duration via motion.After rather than waiting for a keypress.

Example

Show opens the toast and returns the Cmd that dismisses it after Duration.

package main

import (
	"fmt"

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

func main() {
	t := toast.New("Copied")
	fmt.Println(t.Open())
	cmd := t.Show()
	fmt.Println(t.Open(), cmd != nil)
}
Output:
false
true 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 toast auto-dismisses after Duration.

type Model

type Model struct {
	Message string

	// Raw, when true, draws Message unchanged. By default it is sanitised
	// (ansi.Sanitize) so untrusted text cannot carry terminal escape
	// sequences.
	Raw     bool
	Variant widgets.Variant
	Theme   theme.Theme

	Duration time.Duration
	Position Position
	// Margin insets the toast from base's edge, in cells. Defaults to 1
	// via New so a corner toast doesn't sit exactly flush against (and
	// visually merge into) whatever border base itself might have right
	// at that edge — set to 0 for a flush-to-the-corner placement.
	Margin int

	// Mouse, when true, makes Update handle tui.MouseEvent: a left click on
	// the toast dismisses it at once and delivers DismissedMsg. Off (the
	// default) ignores the mouse.
	Mouse bool
	// Bounds is the screen rectangle of the base view the toast is rendered
	// over (its origin and size, as passed to Render); the toast's own
	// rectangle is worked out from it, Position and Margin. Events outside
	// the toast are ignored.
	Bounds hittest.Rect
	// contains filtered or unexported fields
}

Model is a corner notification. Like dialog.Model, Render composites it over a base view when open and returns base unchanged when closed, so a parent can call it unconditionally every frame.

func New

func New(message string) Model

New returns a Model that isn't shown yet — call Show (and return its Cmd) to actually display it.

func (*Model) Hide

func (m *Model) Hide()

Hide closes the toast immediately, without waiting for Duration.

func (Model) LayoutNode

func (m Model) LayoutNode() layout.Node

LayoutNode returns the toast's bordered box as an ordinary layout.Node, its message word-wrapped to the width it is given and coloured by Variant. While the toast is closed it measures as nothing and renders blank. The Model is not changed.

func (Model) Linearize

func (m Model) Linearize() string

Linearize renders the toast as plain text for accessible output (see tui.Linearizer): "Notification, <kind>: <message>", or "" while it is closed. The kind is the Variant in words, so meaning does not rely on colour.

func (Model) Open

func (m Model) Open() bool

Open reports whether the toast is currently shown.

func (Model) Rect

func (m Model) Rect() hittest.Rect

Rect is the screen rectangle the toast occupies when rendered over a base view filling Bounds: where a click lands on it. It is the zero Rect while the toast is closed.

func (Model) Render

func (m Model) Render(base string) string

Render composites the toast over base in its configured Position. If closed, it returns base unchanged.

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) Show

func (m *Model) Show() tui.Cmd

Show opens the toast and returns a Cmd that auto-dismisses it after Duration — return this from your own Update (or Init) so it actually fires.

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

func (Model) Update

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

Update closes the toast when its own auto-dismiss timer (from Show) fires, delivering DismissedMsg; a no-op for any other Msg or a stale timer from a toast Show has since replaced. With Mouse on, a left click on the toast also closes it and delivers DismissedMsg.

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 Position

type Position int

Position selects which screen corner a Toast appears in.

const (
	// BottomRight places the toast in the bottom-right corner, the zero
	// value and so the default.
	BottomRight Position = iota
	// BottomLeft places the toast in the bottom-left corner.
	BottomLeft
	// TopRight places the toast in the top-right corner.
	TopRight
	// TopLeft places the toast in the top-left corner.
	TopLeft
)

Jump to

Keyboard shortcuts

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