API Functions
Ziva provides a rich set of global functions for configuring library behavior, optimizing performance, and advanced usage.
These functions complement the core library components and allow fine-tuning Ziva behavior for your specific application requirements.
Auto-configuration
Automatically configures Ziva for optimal performance on the current system.
func main() {
// Recommended to call at the beginning of the program
ziva.AutoConfigure()
// Your application code
queue := ziva.NewQueue("Application Name")
}
This function:
- Analyzes available system memory
- Detects processor architecture
- Optimizes settings for embedded systems
- Configures caching and buffering
DisableCompletionDelay()
Turns off the default completion delay that keeps the last task on screen for a short moment.
func init() {
// For CI pipelines or scripted runs we don't need the pause
ziva.DisableCompletionDelay()
}
Use it when you prefer immediate transitions between tasks or when running automated tests where every extra millisecond matters.
Is64Bit()
Determines system architecture for optimal algorithm selection.
if ziva.Is64Bit() {
// Use algorithms for 64-bit systems
fmt.Println("64-bit architecture")
} else {
// Optimizations for 32-bit systems
fmt.Println("32-bit architecture")
}
RepeatEfficient(s string, count int) string
Creates a string by efficiently repeating the base string.
// Create a separator of 50 "-" characters
separator := ziva.RepeatEfficient("-", 50)
fmt.Println(separator)
// Indentation for formatting
indent := ziva.RepeatEfficient(" ", 4)
JoinEfficient(parts []string, separator string) string
Joins strings with minimal memory allocations.
items := []string{"apple", "banana", "cherry"}
result := ziva.JoinEfficient(items, ", ")
// Result: "apple, banana, cherry"
FastConcat(parts …string) string
Fast concatenation of arbitrary number of strings.
message := ziva.FastConcat(
"User ", username,
" performed ", action,
" at ", timestamp,
)
CleanWhitespaceEfficient(s string) string
Cleans string from excessive whitespace characters.
text := " Too many\t\tspaces\n\nand\nlines "
clean := ziva.CleanWhitespaceEfficient(text)
// Result: "Too many spaces and lines"
Color Management
SetErrorColor(errorsColor, statusColor lipgloss.TerminalColor)
Configures color scheme for error display.
import "github.com/charmbracelet/lipgloss"
// Red errors with yellow statuses
ziva.SetErrorColor(
lipgloss.Color("#FF0000"), // red for error text
lipgloss.Color("#FFFF00"), // yellow for statuses
)
ResetErrorColors()
Resets error colors to default values.
// After experimenting with colors
ziva.ResetErrorColors()
Changes the style used to render localized “Exit” menu items in list tasks.
exitStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("#888")).Bold(true)
ziva.SetExitMenuItemStyle(exitStyle)
Changes the style used to render localized “Back” menu items in list tasks.
backStyle := lipgloss.NewStyle().Italic(true)
ziva.SetBackMenuItemStyle(backStyle)
EnableEmbeddedMode()
Enables embedded system mode with simplified color palette.
// For routers, IoT devices
ziva.EnableEmbeddedMode()
EnableASCIIMode()
Switches to ASCII mode for legacy terminals.
// For terminals without Unicode support
ziva.EnableASCIIMode()
IsEmbeddedColorMode() bool
Checks if embedded system mode is enabled.
if ziva.IsEmbeddedColorMode() {
fmt.Println("Running in embedded system mode")
}
Advanced Validators
NewPasswordValidator(minLength int) Validator
Creates validator for password complexity checking.
// Password minimum 8 characters
passwordValidator := ziva.NewPasswordValidator(8)
input := ziva.NewInputTask("Password", "Enter password:").
WithInputType(ziva.InputTypePassword).
WithValidator(passwordValidator)
NewEmailValidator() Validator
Email address validator according to RFC standards.
emailValidator := ziva.NewEmailValidator()
email := ziva.NewInputTask("Email", "Enter email:").
WithInputType(ziva.InputTypeEmail).
WithValidator(emailValidator)
NewIPValidator(allowIPv4, allowIPv6 bool) Validator
Configurable IP address validator.
// IPv4 only
ipv4Validator := ziva.NewIPValidator(true, false)
// IPv6 only
ipv6Validator := ziva.NewIPValidator(false, true)
// Any IP
anyIPValidator := ziva.NewIPValidator(true, true)
NewIPv4Validator() / NewIPv6Validator() Validator
Specialized validators for specific IP versions.
ipv4 := ziva.NewIPv4Validator()
ipv6 := ziva.NewIPv6Validator()
NewDomainValidator() Validator
Domain name validator.
domainValidator := ziva.NewDomainValidator()
domain := ziva.NewInputTask("Domain", "Enter domain:").
WithValidator(domainValidator)
NewTextValidator(minLen, maxLen int) Validator
Text validator with length constraints.
// 3 to 50 characters
textValidator := ziva.NewTextValidator(3, 50)
// Minimum only (no maximum limit)
minValidator := ziva.NewTextValidator(5, 0)
// Maximum only (no minimum limit)
maxValidator := ziva.NewTextValidator(0, 100)
Memory Optimization
InternString(s string) string
String interning for memory efficiency.
// For frequently repeated strings in UI
label1 := ziva.InternString("Done")
label2 := ziva.InternString("Done") // References same object
GetCacheStats() (size, capacity int)
Get statistics of interned string cache.
size, capacity := ziva.GetCacheStats()
fmt.Printf("Cache: %d/%d strings\n", size, capacity)
ClearInternCache()
Clear interned string cache.
// Periodic cleanup in long-running applications
ziva.ClearInternCache()
Layout Utilities
CalculateLayoutWidth(screenWidth int) int
Calculates optimal layout width.
screenWidth := 120
layoutWidth := ziva.CalculateLayoutWidth(screenWidth)
// Use in custom UI
queue := ziva.NewQueue("Application")
// layout will be optimized for calculated width
Combined Examples
Embedded System Setup
func setupForEmbedded() {
// Auto-configuration for optimization
ziva.AutoConfigure()
// Embedded system mode
ziva.EnableEmbeddedMode()
// For very old terminals
if needASCII {
ziva.EnableASCIIMode()
}
// Memory monitoring
size, capacity := ziva.GetCacheStats()
if size > capacity*0.8 {
ziva.ClearInternCache()
}
}
func createRegistrationForm() {
queue := ziva.NewQueue("Registration")
// Email with validation
email := ziva.NewInputTask("Email", "Your email:").
WithValidator(ziva.NewEmailValidator())
// Password with security requirements
password := ziva.NewInputTask("Password", "Password (min. 8 chars):").
WithInputType(ziva.InputTypePassword).
WithValidator(ziva.NewPasswordValidator(8))
// Username with constraints
username := ziva.NewInputTask("Username", "Username:").
WithValidator(ziva.NewTextValidator(3, 20))
queue.AddTasks(email, password, username)
}
func processLargeDataset(items []string) string {
// Use efficient functions for string operations
var results []string
for _, item := range items {
// Data cleaning
cleaned := ziva.CleanWhitespaceEfficient(item)
// Interning for memory efficiency
interned := ziva.InternString(cleaned)
results = append(results, interned)
}
// Efficient result joining
return ziva.JoinEfficient(results, "\n")
}
All these functions are designed for maximum performance and can be used in high-load applications without performance degradation.
Functions EnableEmbeddedMode()
and EnableASCIIMode()
change global library state. It’s recommended to call them only once at the beginning of the program.