Skip to content

Commit

Permalink
Add Go-style documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
andrea committed Feb 25, 2024
1 parent 8b01c5d commit b6eb01a
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 17 deletions.
21 changes: 16 additions & 5 deletions components/textinput/textinput.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
package textinput

// Package textinput provides a wrapper around the Bubble Tea
// textinput component, enhancing it with additional styling options
// and customization. It integrates with the Sugarfoam UI framework
// for a more flexible text input experience.
import (
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
foam "github.com/remogatto/sugarfoam"
)

// Option is a type for functions that modify a text input model.
type Option func(*Model)

// Model wraps the Bubble Tea textinput model with additional
// functionality and styling options provided by the Sugarfoam
// framework.
type Model struct {
foam.Common

*textinput.Model
}

// New creates a new text input model with optional configurations.
func New(opts ...Option) *Model {
t := textinput.New()
t.Placeholder = "Text here..."
Expand All @@ -32,36 +40,39 @@ func New(opts ...Option) *Model {
return ti
}

// WithPlaceholder sets the placeholder text for the text input.
func WithPlaceholder(placeholder string) Option {
return func(ti *Model) {
ti.Model.Placeholder = placeholder
}
}

// WithStyles sets the styles for the text input using the provided
// styles.
func WithStyles(styles *foam.Styles) Option {
return func(ti *Model) {
ti.SetStyles(styles)
}
}

// Init initializes the text input model with a blink command.
func (ti *Model) Init() tea.Cmd {
return textinput.Blink
}

// Update updates the text input model based on the received message.
func (ti *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
t, cmd := ti.Model.Update(msg)
ti.Model = &t

return ti, cmd
}

// View renders the text input model, applying the appropriate style
// based on focus state.
func (ti *Model) View() string {
if ti.Focused() {
return ti.GetStyles().Focused.Render(ti.Model.View())
}
return ti.GetStyles().Blurred.Render(ti.Model.View())
}

func (m *Model) KeyBindings() (map[string]key.Binding, error) {
return nil, nil
}
32 changes: 21 additions & 11 deletions components/viewport/viewport.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
package viewport

// Package viewport provides a viewport model for managing scrollable
// content within a UI. It integrates with the Bubble Tea framework
// and the SugarFoam UI framework to offer a flexible and customizable
// scrolling experience. The package supports styling and key bindings
// to enhance the user interaction with the viewport.
import (
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
foam "github.com/remogatto/sugarfoam"
"github.com/remogatto/sugarfoam/keys"
)

// DefaultWidth and DefaultHeight define the default dimensions for
// the viewport.
var (
DefaultWidth = 80
DefaultHeight = 25
)

// Option is a type for functions that modify a viewport model.
type Option func(*Model)

// Model wraps the Bubble Tea viewport model with additional
// functionality and styling options provided by the Sugarfoam
// framework.
type Model struct {
foam.Common
*viewport.Model

focused bool
}

// New creates a new viewport model with optional configurations.
func New(opts ...Option) *Model {
vp := viewport.New(DefaultWidth, DefaultHeight)

Expand All @@ -36,48 +46,48 @@ func New(opts ...Option) *Model {
}

return v

}

// WithStyles sets the styles for the viewport using the provided
// styles.
func WithStyles(styles *foam.Styles) Option {
return func(m *Model) {
m.SetStyles(styles)
}
}

// Init initializes the viewport model.
func (m *Model) Init() tea.Cmd {
return nil
}

// Update updates the viewport model based on the received message.
func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
t, cmd := m.Model.Update(msg)
m.Model = &t

return m, cmd
}

// Focused returns the focus state of the viewport.
func (m *Model) Focused() bool {
return m.focused
}

// Blur removes focus from the viewport.
func (m *Model) Blur() {
m.focused = false
}

// Focus sets the viewport to be focused.
func (m *Model) Focus() tea.Cmd {
m.focused = true

return nil
}

func (m *Model) KeyBindings() (map[string]key.Binding, error) {
kb, err := keys.KeyMapToMap("viewport", m.KeyMap)
if err != nil {
return nil, err
}
return kb, nil
}

// View renders the viewport model, applying the appropriate style
// based on focus state.
func (m *Model) View() string {
if m.Focused() {
return m.GetStyles().Focused.Render(m.Model.View())
Expand Down
20 changes: 20 additions & 0 deletions layout/layout.go
Original file line number Diff line number Diff line change
@@ -1,50 +1,66 @@
package layout

// Package layout provides utilities for managing the layout of UI components.
// It offers a flexible way to organize components into a container, with support
// for styling and resizing. The package is designed to work with the Lipgloss library
// for styling, providing a visually appealing interface.
import (
"github.com/charmbracelet/lipgloss"
)

// DefaultWidth and DefaultHeight define the default dimensions for a layout.
var (
DefaultWidth = 80
DefaultHeight = 25
)

// Option is a type for functions that modify a Layout.
type Option func(*Layout)

// WithItem adds a Placeable item to the layout.
func WithItem(i Placeable) Option {
return func(l *Layout) {
l.items = append(l.items, i)
}
}

// WithStyles sets the styles for the layout container.
func WithStyles(styles *Styles) Option {
return func(l *Layout) {
l.styles = styles
}
}

// Sizeable is an interface for components that can be resized.
type Sizeable interface {
SetSize(width int, height int)
GetWidth() int
GetHeight() int
}

// Placeable is an interface for components that can be placed within a layout.
// It extends the Sizeable interface, indicating that placeable items must
// be resizable.
type Placeable interface {
Sizeable

// View returns the string representation of the component.
View() string
}

// Styles defines the styles for the layout container.
type Styles struct {
Container lipgloss.Style
}

// Layout represents a layout container for UI components.
type Layout struct {
width, height int
items []Placeable
styles *Styles
}

// New creates a new Layout with optional configurations.
func New(opts ...Option) *Layout {
l := &Layout{width: DefaultWidth, height: DefaultHeight}

Expand All @@ -55,22 +71,26 @@ func New(opts ...Option) *Layout {
return l
}

// AddItem adds a Placeable item to the layout.
func (l *Layout) AddItem(i Placeable) *Layout {
l.items = append(l.items, i)

return l
}

// Items returns the items currently in the layout.
func (l *Layout) Items() []Placeable {
return l.items
}

// SetSize resizes the layout and its items, adjusting for container styling.
func (l *Layout) SetSize(width int, height int) {
for _, item := range l.items {
item.SetSize(width-l.styles.Container.GetHorizontalFrameSize(), height-l.styles.Container.GetVerticalFrameSize())
}
}

// View returns the string representation of the layout, rendered with its container styles.
func (l *Layout) View() string {
views := []string{}

Expand Down
23 changes: 22 additions & 1 deletion layout/tiled/tiled.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
package tiled

// Package tiled provides utilities for creating and managing
// horizontal tiles in a UI. It leverages the Lipgloss library for
// styling and the Sugarfoam layout management system for positioning
// and sizing components. The package is designed to facilitate the
// creation of horizontally aligned layouts with a focus on simplicity
// and ease of use.
import (
"github.com/charmbracelet/lipgloss"
"github.com/remogatto/sugarfoam/layout"
)

// DefaultWidth and DefaultHeight define the default dimensions for a
// horizontal tiled layout.
var (
DefaultWidth = 80
DefaultHeight = 25
)

// HorizontalTile represents a container for horizontally aligned
// placeable items.
type HorizontalTile struct {
width, height int
items []layout.Placeable
}

// View returns the string representation of the horizontal tile, with
// all its items rendered horizontally. The width of each item is
// evenly distributed, with the last item potentially taking up the
// remaining space if the total width is not evenly divisible by the
// number of items.
func (ht *HorizontalTile) View() string {
w := ht.width / len(ht.items)
dw := ht.width - w*len(ht.items)
Expand All @@ -33,14 +48,20 @@ func (ht *HorizontalTile) View() string {
return lipgloss.JoinHorizontal(lipgloss.Top, strs...)
}

// SetSize sets the dimensions of the horizontal tile.
func (ht *HorizontalTile) SetSize(width int, height int) {
ht.width = width
ht.height = height
}

func (ht *HorizontalTile) GetWidth() int { return ht.width }
// GetWidth returns the current width of the horizontal tile.
func (ht *HorizontalTile) GetWidth() int { return ht.width }

// GetHeight returns the current height of the horizontal tile.
func (ht *HorizontalTile) GetHeight() int { return ht.height }

// New creates a new HorizontalTile with the specified items, using
// the default dimensions defined by DefaultWidth and DefaultHeight.
func New(items ...layout.Placeable) *HorizontalTile {
return &HorizontalTile{DefaultWidth, DefaultHeight, items}
}

0 comments on commit b6eb01a

Please sign in to comment.