flextui

package module
v0.0.15 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2025 License: MIT Imports: 10 Imported by: 0

README

FlexTUI

FlexTUI is a flexible terminal user interface library inspired by CSS Flexbox. It allows you to create complex layouts with ease, using a simple and intuitive API.

Features

  • Component-Based Architecture: Create reusable UI components with properties like grow, length, and orientation.
  • Responsive Layouts: Automatically adjust component sizes and positions based on terminal dimensions.
  • Customizable Styles: Apply ANSI color codes for styling components and text.
  • Interactive Elements: Build interactive menus and components with keyboard navigation.
  • Signal Handling: Automatically handle terminal resize events and clean up on exit.

Installation

To install FlexTUI, use go get:

go get github.com/computerdane/flextui

Usage

Here's a simple example to get you started:

package main

import (
	"github.com/computerdane/flextui"
	"github.com/computerdane/flextui/components"
)

func main() {
	// Create a new bordered component
	borders := components.NewBorders()
	borders.SetTitle("My App")
	borders.SetTitleIsOnBottom(false)

	// Create a new component for content
	content := flextui.NewComponent()
	content.SetContent("Hello, FlexTUI!")
	borders.Inner.AddChild(content)

	// Add the bordered component to the screen
	flextui.Screen.AddChild(borders.Outer)

	// Update layout and render
	flextui.Screen.UpdateLayout()
	flextui.Screen.Render()

	// Listen for user input here
	select {}
}

Documentation

For more detailed documentation and examples, please refer to the GoDoc.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Documentation

Index

Constants

View Source
const BLANK_CHAR = " "
View Source
const (
	Event_LayoutUpdated = iota // Triggered at the very end of UpdateLayout()
)

Variables

This section is empty.

Functions

func Clear added in v0.0.11

func Clear()

func CursorTo added in v0.0.10

func CursorTo(row, col int)

func HandleShellSignals

func HandleShellSignals()

Handles SIGINT, SIGTERM, and SIGWINCH signals.

- SIGINT/SIGTERM : shows the cursor and exits the current process

- SIGWINCH : updates the screen layout and re-renders the whole screen

func HideCursor

func HideCursor()

func ShowCursor

func ShowCursor()

Types

type Box

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

func (*Box) Bottom

func (b *Box) Bottom() int

func (*Box) Height

func (b *Box) Height() int

func (*Box) Left

func (b *Box) Left() int

func (*Box) Right

func (b *Box) Right() int

func (*Box) ToString

func (b *Box) ToString() string

func (*Box) Top

func (b *Box) Top() int

func (*Box) Width

func (b *Box) Width() int

type Component

type Component struct {
	Scroll Scroll
	// contains filtered or unexported fields
}

A Component represents a rectangular area on the screen that can have a parent Component and children Components. Components are laid out according to simple rules inspired by CSS Flex. By default, Components lay out their children horizontally and space them evenly. When properties are changed on this Component, parent Components, or child Components, they will update each others properties in order to lay themselves out correctly. After changing any properties of a Component, the UpdateLayout() function must be called to apply them before the next Render().

For pre-built Components that are more advanced, see the github.com/computerdane/flextui/components library.

var CursorOwner *Component

The Component that is currently allowed to modify the cursor position.

var Screen *Component

Parent of all components. Fills the entire terminal window.

func NewComponent

func NewComponent() *Component

func (*Component) AddChild

func (c *Component) AddChild(child *Component)

Adds a child Component to this Component. The order in which AddChild() is called will determine the order of the child Components' layout.

func (*Component) AddEventListener added in v0.0.8

func (c *Component) AddEventListener(event int, listener *func(*Component))

Attach an event listener to this component. Use the flextui.Event_* constants to choose an event.

func (*Component) Box

func (c *Component) Box() *Box

func (*Component) Children

func (c *Component) Children() []*Component

func (*Component) Content

func (c *Component) Content() *string

func (*Component) Grow

func (c *Component) Grow() float64

func (*Component) IsVertical

func (c *Component) IsVertical() bool

func (*Component) Length added in v0.0.9

func (c *Component) Length() int

func (*Component) RemoveAllChildren

func (c *Component) RemoveAllChildren()

Removes all child Components from this Component.

func (*Component) RemoveEventListener added in v0.0.8

func (c *Component) RemoveEventListener(event int, listener *func(*Component))

Remove an event listener from this component. Use the flextui.Event_* constants to choose an event.

func (*Component) Render

func (c *Component) Render()

Render this Component's content to the screen, and render all child Components as well.

func (*Component) SetColorFunc

func (c *Component) SetColorFunc(colorFunc func(a ...any) string)

Set the Component's style using a function that can be called to add ANSI color codes before rendering the Component's content. Pairs well with the library github.com/fatih/color using a color's github.com/fatih/color.Color.SprintFunc.

func (*Component) SetContent

func (c *Component) SetContent(content string)

Set this Component's text content.

func (*Component) SetContentFunc

func (c *Component) SetContentFunc(updateFunc func(*Box) string)

Set the Component's content based on its Box's dimensions. Useful for creating responsive Components that fill their content depending on the width/height of the Component.

func (*Component) SetGrow

func (c *Component) SetGrow(grow float64)

Set the Component's grow property. All Components have a default grow of 1. If a Component's grow is larger than others, it will take up more space proportional to the total grow of its neighbors.

func (*Component) SetIsVertical

func (c *Component) SetIsVertical(isVertical bool)

Change whether child Components are laid out vertically or horizontally.

func (*Component) SetLength

func (c *Component) SetLength(length int)

Set a custom length for a Component. Overrides the grow property and disables flex-based layout for this Component only. Neighbor Components will still use their grow properties for their layouts.

func (*Component) UpdateLayout

func (c *Component) UpdateLayout()

Updates the Box positions of this Component and all child Components.

Useful for responding to layout changes triggered by screen resizing or user actions.

type Scroll added in v0.0.7

type Scroll struct {
	Top    int
	Left   int
	Right  int
	Bottom int
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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