modelstack

package module
v0.0.0-...-1878c99 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

README

Modelstack

Cleanly nested Model objects for Bubbletea

Go Reference

Documentation

Overview

Package modelstack impliments a tea.Model (from Bubbletea) that listens for PushModel and PopModel tea.Msg-s to switch control between several tea.Model-s, including some initialization maintinance.

This allows easier re-use of tea.Model-s 'within' each other. However it breaks some assumptions about how your model will be used by bubbletea. The noteable behavioral differences are listed here:

For the most part, things should "just work", but keep a few things in mind when designing tea.Model-s you want to be compatible with this package:

  • Be sure to initialize ticks in .Init()
  • Wait for tea.Cmd-s you want to resolve before passing control. tea.Sequence can be useful for this.
  • Be able to handle tea.Msg-s going missing if it's possible for control to be lost mid-processing of a tea.Cmd.
  • Remember the model on the stack is the same model, not a re-initialized one (though .Init() is called again).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Pop

func Pop(msgs ...tea.Msg) tea.Cmd

Pop returns a tea.Cmd used to send a PopModel tea.Msg. Optionally you can give it tea.Msg-s to pass to the parent tea.Model.

func Push

func Push(m tea.Model) tea.Cmd

Push returns s a tea.Cmd used to send a PushModel tea.Msg

Types

type ModelStack

type ModelStack struct {
	// contains filtered or unexported fields
}

ModelStack is a tea.Model that listens for PushModel and PopModel tea.Msg's to switch control between several tea.Model's

func New

func New(m tea.Model, opts ...Option) ModelStack

New creates a new Modelstack with an initial tea.Model

func (ModelStack) Init

func (m ModelStack) Init() tea.Cmd

Init fulfills the tea.Model interface, currently it just calls Init() on the current tea.Model on the stack

func (ModelStack) Update

func (m ModelStack) Update(msg tea.Msg) (tea.Model, tea.Cmd)

Update fulfills the tea.Model interface, intercepting PushModel and PopModel tea.Msg's, as well as information needed to initialize models as they switch control (e.g. the most recent tea.WindowSizeMsg)

func (ModelStack) View

func (m ModelStack) View() string

View fulfills the tea.Model interface, rendering the currently active tea.Model on its stack

type Option

type Option func(*ModelStack)

Option is used to set options when initializing a ModelStack

func WithSlogger

func WithSlogger(l *slog.Logger) Option

WithSlogger attaches a *slog.Logger to a ModelStack

type PopModel

type PopModel struct {
	// Msgs are tea.Msg-s that will be passed to the parent model synchronously
	// when it regains control (before its first call to .View(), after calling
	// .Init())
	Msgs []tea.Msg
}

PopModel is a tea.Msg that instructs a ModelStack to remove the current tea.Model from the stack and pass control to the previous tea.Model

type PushModel

type PushModel struct {
	Model tea.Model
}

PushModel is a tea.Msg that instructs a ModelStack to add a new tea.Model to the stack and pass control to it

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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