devtui

package module
v0.0.48 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2025 License: MIT Imports: 15 Imported by: 0

README

DevTUI

Interactive Terminal User Interface library for Go applications development (principal tui in GoDEV App)

Features

  • Tab-based interface organization
  • Editable and non-editable fields
  • Keyboard navigation (Tab, Shift+Tab, Left/Right arrows)
  • Field validation and callbacks
  • Customizable styles and colors

devtui

Basic Usage

package main

import (
	"fmt"
	"github.com/cdvelop/devtui"
)

func main() {
	// Configuration
	config := &devtui.TuiConfig{
		AppName:  "MyApp", 
		ExitChan: make(chan bool),
		Config: devtui.Config{
			ForeGround:"#F4F4F4",
			Background:"#000000",
			Highlight: "#FF6600",
			Lowlight:  "#666666",
		},
		LogToFile: func(messageErr any) {
			// Implement your logging logic here
		},
	}

	// Create new TUI instance
	tui := devtui.NewTUI(config)

	// Create and add custom tabs (recommended way)
	mainTab := tui.NewTabSection("Main", "")
	mainTab.AddFields(
		*devtui.NewField(
			"Username",
			"",
			true,
			func(newValue string) (string, error) {
				if len(newValue) < 5 {
					return "", fmt.Errorf("username must be at least 5 characters")
				}
				return newValue, nil
			},
		),
	)
	tui.AddTabSections(mainTab)

	// Start the TUI
	if err := tui.Start(); err != nil {
		panic(err)
	}
}

Keyboard Shortcuts

Key Action
Tab Switch to next tab
Shift+Tab Switch to previous tab
Left/Right Navigate between fields in current tab
Enter Edit field or execute action
Esc Cancel editing
Ctrl+C Quit application

NewTabSection Method

// NewTabSection creates a new TabSection with the given title and footer
// Example:

	tab := tui.NewTabSection("BUILD", "Press 't' to compile")
	// Preferred way to add fields (variadic parameters)
	tab.AddFields(
		*NewField(
			"Username",
			"",
			true,
			func(newValue string) (string, error) {
				if len(newValue) < 5 {
					return "", fmt.Errorf("username must be at least 5 characters")
				}
				return newValue, nil
			},
		),
		*NewField(
			"Password",
			"",
			true,
			func(newValue string) (string, error) {
				if len(newValue) < 8 {
					return "", fmt.Errorf("password must be at least 8 characters")
				}
				return newValue, nil
			},
		),
	)


//	 Get/Set title and footer
	currentTitle := tab.Title()
	tab.SetTitle("New Title")

	currentFooter := tab.Footer() 
	tab.SetFooter("New Footer")

Field Types

Editable Fields
*NewField(
	"Field Name", 
	"initial value", 
	true, 
	func(value string) (string, error) {
		// Validate or process the new value
		return value, nil
	},
)
Non-Editable Fields (Action Buttons)
*NewField(
	"Action Button", 
	"Click me", 
	false, 
	func(value string) (string, error) {
		// Execute action
		return "Action executed", nil
	},
)

errors

  • al borrar todo un campo editable, al comenzar a escribir se copia el valor anterior al lado del cursor

Dependencies

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ColorStyle

type ColorStyle struct {
	ForeGround string // eg: #F4F4F4
	Background string // eg: #000000
	Highlight  string // eg: #FF6600
	Lowlight   string // eg: #666666
}

type DevTUI

type DevTUI struct {
	*TuiConfig
	// contains filtered or unexported fields
}

DevTUI mantiene el estado de la aplicación

func DefaultTUIForTest added in v0.0.10

func DefaultTUIForTest(LogToFile func(messageErr any)) *DevTUI

NewDefaultTUI creates a DevTUI instance with basic default configuration useful for unit tests and for quick initialization in real applications

func NewTUI

func NewTUI(c *TuiConfig) *DevTUI

func (*DevTUI) AddTabSections added in v0.0.9

func (t *DevTUI) AddTabSections(sections ...*TabSection) *DevTUI

AddTabSections adds one or more TabSections to the DevTUI If a tab with title "DEFAULT" exists, it will be replaced by the first tab section Deprecated: Use NewTabSection and append to tabSections directly

func (*DevTUI) ContentView

func (h *DevTUI) ContentView() string

ContentView renderiza los mensajes para una sección de contenido

func (*DevTUI) GetTotalTabSections added in v0.0.9

func (t *DevTUI) GetTotalTabSections() int

GetTotalTabSections returns the total number of tab sections

func (*DevTUI) HandleKeyboard added in v0.0.10

func (h *DevTUI) HandleKeyboard(msg tea.KeyMsg) (bool, tea.Cmd)

HandleKeyboard processes keyboard input and updates the model state returns whether the update function should continue processing or return early

func (*DevTUI) Init

func (h *DevTUI) Init() tea.Cmd

Init initializes the terminal UI application.

func (*DevTUI) InitTUI added in v0.0.5

func (h *DevTUI) InitTUI(args ...any)

InitTUI initializes and runs the terminal UI application.

It accepts optional variadic arguments of any type. If a *sync.WaitGroup is provided among these arguments, InitTUI will call its Done() method before returning.

The method runs the UI using the internal tea engine, and handles any errors that may occur during execution. If an error occurs, it will be displayed on the console and the application will wait for user input before exiting.

Parameters:

  • args ...any: Optional arguments. Can include a *sync.WaitGroup for synchronization.

func (*DevTUI) NewTabSection added in v0.0.43

func (t *DevTUI) NewTabSection(title, footer string) *TabSection

NewTabSection creates and initializes a new TabSection with the given title and footer Example:

tab := tui.NewTabSection("BUILD", "Press enter to compile")

func (*DevTUI) Print

func (h *DevTUI) Print(messages ...any)

Print sends a normal Label or error to the tui in current tab

func (*DevTUI) ReturnFocus

func (t *DevTUI) ReturnFocus() error

func (*DevTUI) Update

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

Update maneja las actualizaciones del estado

func (*DevTUI) View

func (h *DevTUI) View() string

type Field added in v0.0.44

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

use NewField to create a new field in the tab section Field represents a field in the TUI with a name, value, and editable state.

func NewField added in v0.0.44

func NewField(name, value string, editable bool, changeFunc func(newValue string) (string, error)) *Field

NewField creates and returns a new Field instance.

Parameters:

  • name: The name of the field.
  • value: The initial value of the field.
  • editable: A boolean indicating whether the field is editable.
  • changeFunc: A callback function that is invoked when the field's value changes. It receives the new value as a parameter and returns the updated value and an error, if any.

Returns: - A pointer to the newly created Field instance.

Example usage:

field := NewField("username", "defaultUser", true, func(newValue string) (string, error) {
    if len(newValue) == 0 {
        return "", errors.New("value cannot be empty")
    }
    return newValue, nil
})

func (*Field) Editable added in v0.0.44

func (f *Field) Editable() bool

func (*Field) Name added in v0.0.44

func (f *Field) Name() string

func (*Field) SetCursorAtEnd added in v0.0.44

func (f *Field) SetCursorAtEnd()

func (*Field) SetEditable added in v0.0.44

func (f *Field) SetEditable(editable bool)

func (*Field) SetName added in v0.0.44

func (f *Field) SetName(name string)

func (*Field) SetValue added in v0.0.44

func (f *Field) SetValue(value string)

func (*Field) Value added in v0.0.44

func (f *Field) Value() string

type TabSection

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

TabSection represents a tab section in the TUI with configurable fields and content

func (*TabSection) AddFields added in v0.0.44

func (ts *TabSection) AddFields(fields ...Field)

AddFields adds one or more Field handlers to the section This is the preferred method for adding fields in normal usage

func (*TabSection) FieldHandlers added in v0.0.7

func (ts *TabSection) FieldHandlers() []Field

FieldHandlers returns the field handlers slice

func (*TabSection) Footer added in v0.0.44

func (ts *TabSection) Footer() string

Footer returns the tab section footer text

func (*TabSection) SetFieldHandlers added in v0.0.44

func (ts *TabSection) SetFieldHandlers(handlers []Field)

SetFieldHandlers sets the field handlers slice (mainly for testing)

func (*TabSection) SetFooter added in v0.0.44

func (ts *TabSection) SetFooter(footer string)

SetFooter sets the tab section footer text

func (*TabSection) SetTitle added in v0.0.44

func (ts *TabSection) SetTitle(title string)

SetTitle sets the tab section title

func (*TabSection) Title

func (ts *TabSection) Title() string

Title returns the tab section title

func (*TabSection) Write added in v0.0.4

func (ts *TabSection) Write(p []byte) (n int, err error)

Write implementa io.Writer para capturar la salida de otros procesos

type TuiConfig

type TuiConfig struct {
	AppName       string    // app name eg: "MyApp"
	TabIndexStart int       // is the index of the tab section to start
	ExitChan      chan bool //  global chan to close app
	Color         *ColorStyle

	LogToFile func(messageErr any) // function to write log error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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