foam

package module
v0.0.0-...-766dd70 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2024 License: MIT Imports: 3 Imported by: 2

README

SugarFoam

SugarFoam is a library written in Go, based on the delightful Bubble Tea, aiming to provide a higher-level API compared to the latter.

With SugarFoam, it is possible to develop TUI applications that present organized components in layouts. Compared to BubbleTea, it tries to save a bit of boilerplate code, especially if you want to develop applications that present articulated components such as tabgroup, statusbar, etc.

Go Reference Build Status Go Report Card

Demo

Starwars characters browser

Project Status

SugarFoam is in a pre-alpha development stage. APIs are subject to change and not recommended for production use. We welcome feedback and contributions for early development.

Features

  • High-level API based on Bubble Tea for building TUI applications.
  • Idiomatic approach to building applications that preserves the architecture inspired by Elm.
  • Customizable layouts to easily define layouts for rendering components.

Quickstart

To get started with SugarFoam, follow these steps:

  1. Clone the Repository:
git clone github.com/remogatto/sugarfoam
  1. Explore the examples/ folder:
cd sugarfoam/examples/tabgroup
go run .

Components and layout

SugarFoam includes a small library of components that serve as containers for Bubbles models. These components can be organized into groups, and within these groups, a layout can be defined to distribute the components in space.

API example

The following snippet demonstrates the creation of two groups of UI elements and organizes them into a tab group.

  • Group Creation: Two groups (group1 and group2) are created, each containing specific UI elements (textinput, viewport, table1, and table2). These elements are arranged within each group using a layout system, with group1 utilizing a tiled layout for viewport and table1.
  • Layout and Styling: Each group is styled with padding around the container, applied using lipgloss.NewStyle().Padding(1, 1), indicating a padding of 1 unit on the top and bottom, and 0 units on the left and right.
  • Tab Group Organization: A tab group (tabGroup) is created to organize the two groups into tabs. The first tab contains group1 and is titled "Tiled layout", set as active by default. The second tab contains group2 and is titled "Single layout".
table1 := table.New()
viewport := viewport.New()

// Create a new group with text input, viewport, and table1 elements.
group1 := group.New(
	group.WithItems(textinput, viewport, table1),
	group.WithLayout(
		layout.New(
			layout.WithStyles(&layout.Styles{Container: lipgloss.NewStyle().Padding(1, 1)}),
			layout.WithItem(tiled.New(viewport, table1)),
		),
	),
)

// Create another group, this time with only table2 element.
group2 := group.New(
	group.WithItems(table2),
	group.WithLayout(
		layout.New(
			layout.WithStyles(&layout.Styles{Container: lipgloss.NewStyle().Padding(1, 1)}),
			layout.WithItem(table2),
		),
	),
)

// Organize the groups into a tab group with two tabs.
tabGroup := tabgroup.New(
	tabgroup.WithItems(
		tabitem.New(group1, tabitem.WithTitle("Tiled layout"), tabitem.WithActive(true)),
		tabitem.New(group2, tabitem.WithTitle("Single layout")),
	),
)

License

Copyright © 2024 Andrea Fazzi

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Common

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

Common encapsulates common properties for UI components, including width, height, and styles.

func (*Common) GetHeight

func (c *Common) GetHeight() int

GetHeight returns the current height of the UI component.

func (*Common) GetStyles

func (c *Common) GetStyles() *Styles

GetStyles returns the current styles of the UI component.

func (*Common) GetWidth

func (c *Common) GetWidth() int

GetWidth returns the current width of the UI component.

func (*Common) SetHeight

func (c *Common) SetHeight(height int)

SetHeight sets the height of the UI component.

func (*Common) SetSize

func (c *Common) SetSize(width int, height int)

SetSize sets the width and height of the UI component, adjusting the focused style width to account for the border size.

func (*Common) SetStyles

func (c *Common) SetStyles(styles *Styles)

SetStyles sets the styles of the UI component.

func (*Common) SetWidth

func (c *Common) SetWidth(width int)

SetWidth sets the width of the UI component.

type Focusable

type Focusable interface {
	tea.Model
	layout.Placeable

	// Focus returns a command that focuses the element.
	Focus() tea.Cmd

	// Blur removes focus from the element.
	Blur()
}

Focusable is an interface that extends the tea.Model and layout.Placeable interfaces, adding methods for managing focus state.

Implementations of Focusable are expected to define how an element can be focused and blurred.

type Groupable

type Groupable interface {
	Focusable

	// Current returns the currently focused element within the group.
	Current() Focusable
}

Groupable extends the Focusable interface, adding methods for managing a group of focusable elements.

Implementations of Groupable are expected to define the current focused element within the group.

type Styles

type Styles struct {
	Focused  lipgloss.Style
	Blurred  lipgloss.Style
	NoBorder lipgloss.Style
}

Styles defines the styles for focused, blurred, and no-border states of UI elements.

func DefaultStyles

func DefaultStyles() *Styles

DefaultStyles returns a new Styles struct with default styles for focused, blurred, and no-border states, leveraging Lipgloss for styling.

type Tabbable

type Tabbable interface {
	Groupable

	// Title returns the title of the tab.
	Title() string

	// Active returns true if the tab is currently active.
	Active() bool
}

Tabbable extends the Groupable interface, adding methods for managing tab navigation and active state.

Implementations of Tabbable are expected to define a title for the tab, and whether the tab is currently active.

Directories

Path Synopsis
components
examples

Jump to

Keyboard shortcuts

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