devtui

package module
v0.0.91 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2025 License: MIT Imports: 15 Imported by: 0

README

DevTUI

Project Badges

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 any) (string, error) {
				strValue := newValue.(string)
				if len(strValue) < 5 {
					return "", fmt.Errorf("username must be at least 5 characters")
				}
				return "Username updated to " + strValue, 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 any) (string, error) {
				strValue := newValue.(string)
				if len(strValue) < 5 {
					return "", fmt.Errorf("username must be at least 5 characters")
				}
				return "Username updated to " + strValue, nil
			},
		),
		*NewField(
			"Password",
			"",
			true,
			func(newValue any) (string, error) {
				strValue := newValue.(string)
				if len(strValue) < 8 {
					return "", fmt.Errorf("password must be at least 8 characters")
				}
				return "Password updated", nil
			},
		),
	)


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

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

Field Types

Editable Fields with Different Data Types
String Field
*NewField(
	"Username", 
	"defaultUser", 
	true, 
	func(value any) (string, error) {
		strValue := value.(string)
		if len(strValue) < 3 {
			return "", fmt.Errorf("username must be at least 3 characters")
		}
		return "Username updated to " + strValue, nil
	},
)
Numeric Field (Port)
*NewField(
	"Server Port", 
	"8080", 
	true, 
	func(value any) (string, error) {
		switch v := value.(type) {
		case string:
			if port, err := strconv.Atoi(v); err == nil && port > 0 && port < 65536 {
				return fmt.Sprintf("Port changed to %d", port), nil
			}
			return "", fmt.Errorf("invalid port number: %s", v)
		case int:
			if v > 0 && v < 65536 {
				return fmt.Sprintf("Port changed to %d", v), nil
			}
			return "", fmt.Errorf("port out of range: %d", v)
		default:
			return "", fmt.Errorf("unsupported type for port: %T", v)
		}
	},
)
Boolean Field
*NewField(
	"Debug Mode", 
	"false", 
	true, 
	func(value any) (string, error) {
		switch v := value.(type) {
		case bool:
			return fmt.Sprintf("Debug mode set to %t", v), nil
		case string:
			if b, err := strconv.ParseBool(v); err == nil {
				return fmt.Sprintf("Debug mode set to %t", b), nil
			}
			return "", fmt.Errorf("invalid boolean value: %s", v)
		default:
			return "", fmt.Errorf("unsupported type for boolean: %T", v)
		}
	},
)
Non-Editable Fields (Action Buttons)
*NewField(
	"Action Button", 
	"Click me", 
	false, 
	func(value any) (string, error) {
		// Execute action - value parameter is ignored for non-editable fields
		return "Action executed successfully", 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(messages ...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

NewTUI creates a new DevTUI instance.

Usage Example:

config := &TuiConfig{
    AppName: "MyApp",
    TabIndexStart: 0,
    ExitChan: make(chan bool),
    Color: nil, // or your *ColorStyle
    LogToFile: func(err any) { fmt.Println(err) },
}
tui := NewTUI(config)

// To start the TUI:
if err := tui.tea.Start(); err != nil {
    config.LogToFile(err)
}

// To close the TUI from another goroutine:
config.ExitChan <- true

You can customize fields, tabs, and handlers after creation. See the documentation for more advanced usage.

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 TuiConfig

type TuiConfig struct {
	AppName       string    // app name eg: "MyApp"
	TabIndexStart int       // is the index of the tab section to start default 0
	ExitChan      chan bool //  global chan to close app eg: make(chan bool)
	/* *ColorStyle style for the TUI
	 eg:
	type ColorStyle struct {
	 Foreground string // eg: #F4F4F4
	 Background string // eg: #000000
	 Highlight  string // eg: #FF6600
	 Lowlight   string // eg: #666666
	}*/
	Color *ColorStyle

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

Directories

Path Synopsis
cmd
test-timestamps command

Jump to

Keyboard shortcuts

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