ToastManager
ToastManager must be overlaid on top of your main layout using a tview.Pages or similar. It draws itself at a configurable screen corner and requires a *tview.Application for QueueUpdateDraw:
toasts := components.NewToastManager(app.TviewApp())// Add to your root pages so it renders above all contentroot.AddPage("toasts", toasts, true, true)Showing toasts
Section titled “Showing toasts”// Auto-dismiss after default duration (3s)toasts.Show("Saved successfully", components.ToastSuccess)
// Custom durationtoasts.ShowWithDuration("Rate limited", components.ToastWarning, 10*time.Second)
// Persistent (must be dismissed manually)toasts.ShowWithDuration("Connection lost", components.ToastError, 0)Levels
Section titled “Levels”| Constant | Color | Icon |
|---|---|---|
ToastInfo | Blue | ℹ |
ToastSuccess | Green | ✓ |
ToastWarning | Yellow | ⚠ |
ToastError | Red | ✗ |
Positions
Section titled “Positions”Six positions: ToastTopRight (default), ToastTopLeft, ToastBottomRight, ToastBottomLeft, ToastTopCenter, ToastBottomCenter.
Defaults
Section titled “Defaults”| Setting | Default |
|---|---|
| Position | ToastTopRight |
| Max visible | 5 |
| Max width | 40 chars |
| Default duration | 3 seconds |
Usage
toasts := components.NewToastManager(tview.NewApplication())toasts.SetPosition(components.ToastTopRight)toasts.SetMaxVisible(5)
toasts.Show("Saved successfully", components.ToastSuccess)toasts.ShowPersistent("Connection lost", components.ToastError)
fmt.Println(toasts != nil)Methods
| Method | Signature | Description |
|---|---|---|
Dismiss | func (m *ToastManager) Dismiss(id string) | Dismiss removes a specific toast |
DismissAll | func (m *ToastManager) DismissAll() | DismissAll removes all toasts |
Error | func (m *ToastManager) Error(message string) *Toast | Error shows an error toast |
GetActive | func (m *ToastManager) GetActive() []*Toast | GetActive returns all currently visible toasts |
HandleAction | func (m *ToastManager) HandleAction(actionIndex int) bool | HandleAction triggers an action by index for the most recent toast |
HasActive | func (m *ToastManager) HasActive() bool | HasActive returns true if any toasts are visible |
Info | func (m *ToastManager) Info(message string) *Toast | Info shows an info toast |
SetDefaultDuration | func (m *ToastManager) SetDefaultDuration(d time.Duration) *ToastManager | SetDefaultDuration sets default auto-dismiss time |
SetMaxVisible | func (m *ToastManager) SetMaxVisible(max int) *ToastManager | SetMaxVisible limits how many toasts show at once |
SetMaxWidth | func (m *ToastManager) SetMaxWidth(width int) *ToastManager | SetMaxWidth sets maximum toast width |
SetOnDismiss | func (m *ToastManager) SetOnDismiss(fn func(toast *Toast)) *ToastManager | SetOnDismiss is called when a toast is dismissed |
SetOnShow | func (m *ToastManager) SetOnShow(fn func(toast *Toast)) *ToastManager | SetOnShow is called when a toast is displayed |
SetPosition | func (m *ToastManager) SetPosition(pos ToastPosition) *ToastManager | SetPosition sets where toasts appear |
Show | func (m *ToastManager) Show(message string, level ToastLevel) *Toast | Show displays a simple toast message |
ShowPersistent | func (m *ToastManager) ShowPersistent(message string, level ToastLevel) *Toast | ShowPersistent displays a toast that must be manually dismissed |
ShowWithAction | func (m *ToastManager) ShowWithAction(message string, level ToastLevel, actions ...ToastAction) *Toast | ShowWithAction displays a toast with action buttons |
ShowWithDuration | func (m *ToastManager) ShowWithDuration(message string, level ToastLevel, duration time.Duration) *Toast | ShowWithDuration displays a toast with custom duration |
ShowWithUndo | func (m *ToastManager) ShowWithUndo(message string, undoFn func()) *Toast | ShowWithUndo displays a success toast with an Undo action |
Success | func (m *ToastManager) Success(message string) *Toast | Success shows a success toast |
Warning | func (m *ToastManager) Warning(message string) *Toast | Warning shows a warning toast |
Types
type Toast struct Toast is a single notification managed by ToastManager. Duration 0 makes
it persistent — it stays visible until explicitly dismissed. Actions add
inline buttons (e.g., "Undo"); see ShowWithAction and ShowWithUndo. | Field | Type | Description |
|---|---|---|
ID | string | |
Message | string | |
Level | ToastLevel | |
Duration | time.Duration | 0 = persistent (must dismiss manually) |
Actions | []ToastAction | |
CreatedAt | time.Time |
type ToastAction struct ToastAction represents a clickable action in a toast | Field | Type | Description |
|---|---|---|
Label | string | |
Handler | func() |
type ToastLevel ToastLevel indicates the severity/type of notification | Constant | Description |
|---|---|
ToastInfo | |
ToastSuccess | |
ToastWarning | |
ToastError |
type ToastPosition ToastPosition determines which screen corner the toast stack appears in. | Constant | Description |
|---|---|
ToastTopRight | |
ToastTopLeft | |
ToastBottomRight | |
ToastBottomLeft | |
ToastTopCenter | |
ToastBottomCenter |