soda

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2023 License: Unlicense Imports: 15 Imported by: 5

README

🥤 Soda

Bubbletea Framework

It automatically handles state management and general layout, so that you can focus on app functionality rather than fighting with TTY 🤕🥊🖥️

[!NOTE]
Work in progress

Features

  • Easy state management with history traversal
  • Predefined layout with help, title, notifications and pizza
  • Something else?

Install

go get github.com/metafates/soda

Examples

Take a look at examples folder

Why?

I write TUI apps a lot. And after some trial and error I came up with the structure that suits me the best. I've been copy-pasting it for my projects again and again, so I decided to create a separate package for it so that others can benefit from it too.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Back

func Back() tea.Msg

Back pops one state back

func BackN

func BackN(steps int) tea.Cmd

BackN pops n states back

func Error

func Error(err error) tea.Cmd

Error sends error to the Model

func Notify

func Notify(message string, duration time.Duration) tea.Cmd

Notify sends a notification to the Model

func PushState

func PushState(state State, save bool) tea.Cmd

func ReplaceState

func ReplaceState(state State) tea.Cmd

func SendCmd added in v0.2.0

func SendCmd(cmdSupplier func() (tea.Cmd, error)) tea.Cmd

func StartSpinner

func StartSpinner() tea.Msg

StartSpinner starts the Model's spinner

func StopSpinner

func StopSpinner() tea.Msg

StopSpinner stops the Model's spinner

Types

type KeyMap

type KeyMap struct {
	Quit key.Binding
	Back key.Binding
	Help key.Binding
	// contains filtered or unexported fields
}

func DefaultKeyMap

func DefaultKeyMap() KeyMap

func (KeyMap) FullHelp

func (k KeyMap) FullHelp() [][]key.Binding

func (KeyMap) ShortHelp

func (k KeyMap) ShortHelp() []key.Binding

type Model

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

func New

func New(state State, options ...Option) *Model

New creates a new soda model with initial state

func (*Model) Context

func (m *Model) Context() context.Context

func (*Model) Init

func (m *Model) Init() tea.Cmd

func (*Model) SpinnerActive

func (m *Model) SpinnerActive() bool

func (*Model) StateSize

func (m *Model) StateSize() Size

func (*Model) Update

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

func (*Model) View

func (m *Model) View() string

type ModelHandler

type ModelHandler interface {
	StateSize() Size
	Context() context.Context
	SpinnerActive() bool
}

type OnErrorFunc

type OnErrorFunc func(err error) tea.Cmd

type Option

type Option func(model *Model)

func WithAutoSize

func WithAutoSize() Option

WithAutoSize gets terminal dimensions and sets Model's size. It will also make the Model listen for incoming resize messages from temrinal.

func WithKeyMap

func WithKeyMap(keyMap KeyMap) Option

func WithOnError

func WithOnError(onError OnErrorFunc) Option

func WithSaveInitialStateToHistory added in v0.2.0

func WithSaveInitialStateToHistory(save bool) Option

func WithSize

func WithSize(size Size) Option

WithSize sets a fixed size for the Model.

func WithSpinner

func WithSpinner(s spinner.Spinner) Option

WithSpinner sets a custom spinner.Spinner for Model

func WithStyleMap

func WithStyleMap(styleMap StyleMap) Option

type Size

type Size struct {
	Width, Height int
}

func (Size) SplitHorizotnal added in v0.2.0

func (s Size) SplitHorizotnal(size Size, leftRatio float64) (Size, Size)

type State

type State interface {
	// Destroy is called before the state is popped out from history
	Destroy()

	// Backable whether the state can be popped from history
	Backable() bool

	// Resize the state
	Resize(size Size) tea.Cmd

	// Title of the state
	Title() title.Title

	// Subtitle is displayed under the title.
	// If empty string is given subtitle won't be rendered
	Subtitle() string

	// Status is displayed next to the title
	Status() string

	// KeyMap of the state
	KeyMap() help.KeyMap

	// Init is the first function that will be called. It returns an optional
	// initial command. To not perform an initial command return nil.
	Init(mh ModelHandler) tea.Cmd

	// Update is called when a message is received. Use it to inspect messages
	// and, in response, update the model and/or send a command.
	Update(mh ModelHandler, msg tea.Msg) tea.Cmd

	// View renders the state's UI, which is just a string. The view is
	// rendered after every Update.
	View(mh ModelHandler) string
}

State that model operates

type StyleMap

type StyleMap struct {
	Global       lipgloss.Style
	Header       lipgloss.Style
	Subtitle     lipgloss.Style
	Status       lipgloss.Style
	Spinner      lipgloss.Style
	Notification lipgloss.Style
	HelpBar      lipgloss.Style
}

func DefaultStyleMap

func DefaultStyleMap() StyleMap

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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