greentea

package module
v0.0.0-...-2fb1d40 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2025 License: MIT Imports: 15 Imported by: 2

README

greentea

A Wrapper for the bubbletea golang tui libary

Example

package main

import (
	"context"
	"fmt"
	"time"

	"github.com/tiemingo/greentea"
	"github.com/urfave/cli/v3"
)

func main() {

	logLeaf := greentea.NewStringLeaf()
	quitLeaf := greentea.NewLeaf[error]()
	exitLeaf := greentea.NewLeaf[func()]()

	commandError := &greentea.CommandError{
		CommandError: "",
	}

	// Set exit functions
	exitLeaf.Append(func() {
		logLeaf.Println("First exit func")
	})
	exitLeaf.Append(func() {
		logLeaf.Println("Second exit func")
	})

	go func() {
		i := 0
		for {
			logLeaf.Printlnf("%d. Print", i)
			time.Sleep(time.Second)

			if i == 10 {
				// exitLeaf.Append(nil) // exit without message
				quitLeaf.Append(fmt.Errorf("I is %d", i)) // exit with message
			}
			i++
		}
	}()

	greentea.RunTui(&greentea.GreenTeaConfig{
		RefreshDelay: 100,
		Commands: []*cli.Command{
			{
				Name: "test",
				Action: func(ctx context.Context, cmd *cli.Command) error {
					commandError.CommandError = "test for command error"
					return nil
				},
			},
		},
		LogLeaf:  logLeaf,
		QuitLeaf: quitLeaf,
		ExitLeaf: exitLeaf,
		History: &greentea.History{
			Persistent:    true,
			SavePath:      "./",
			HistoryLength: 25,
		},
		CommandError: commandError,
	})
}

Documentation

Index

Constants

View Source
const HISTORY_FILENAME = ".magichistory"

Variables

This section is empty.

Functions

func RunTui

func RunTui(config *GreenTeaConfig)

Types

type CommandError

type CommandError struct {
	CommandError string
}

type GreenTeaConfig

type GreenTeaConfig struct {
	RefreshDelay int
	Commands     []*cli.Command
	LogLeaf      *StringLeaf
	QuitLeaf     *Leaf[error] // once a print is added, the applecation quits with the added message

	ExitLeaf     *Leaf[func()] // functions to run on exit
	History      *History
	CommandError *CommandError
	// contains filtered or unexported fields
}

type History

type History struct {
	Persistent    bool
	SavePath      string
	HistoryLength int
	// contains filtered or unexported fields
}

type Leaf

type Leaf[T any] struct {
	// contains filtered or unexported fields
}

func NewLeaf

func NewLeaf[T any]() *Leaf[T]

Creates and return a new empty leaf

func (*Leaf[T]) Append

func (leaf *Leaf[T]) Append(item T)

Appends the given element to the leaf

func (*Leaf[T]) Clear

func (leaf *Leaf[T]) Clear()

Removes all remaining items for leaf

func (*Leaf[T]) GetAll

func (leaf *Leaf[T]) GetAll() []T

Return a slice of all items in leaf

func (*Leaf[T]) Harvest

func (leaf *Leaf[T]) Harvest() (T, bool)

Returns the oldest element and true, then removes it from the leaf. Return an empty var of type T and false if leaf has no elements

func (*Leaf[T]) HarvestAll

func (leaf *Leaf[T]) HarvestAll() []T

Returns all items from leaf and clears it.

type StringLeaf

type StringLeaf Leaf[string]

func NewStringLeaf

func NewStringLeaf() *StringLeaf

Creates and return a new empty string leaf

func (*StringLeaf) Clear

func (leaf *StringLeaf) Clear()

Removes all remaining items for leaf

func (*StringLeaf) GetAll

func (leaf *StringLeaf) GetAll() []string

Return a slice of all items in leaf

func (*StringLeaf) Harvest

func (leaf *StringLeaf) Harvest() (string, bool)

Returns the oldest element and true, then removes it from the leaf. Return an empty string and false if leaf has no elements

func (*StringLeaf) HarvestAll

func (leaf *StringLeaf) HarvestAll() []string

Returns all items from leaf and clears it.

func (*StringLeaf) Println

func (leaf *StringLeaf) Println(a ...any)

Implements fmt.Println()

func (*StringLeaf) Printlnf

func (leaf *StringLeaf) Printlnf(format string, a ...any)

Implements fmt.Printf() with a new line after

Jump to

Keyboard shortcuts

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