Skip to main content

Task Types in Ziva

Ziva provides five main task types for creating interactive TUI applications. Each task has unique capabilities and use cases.

Task Types

Common Features

All tasks in Ziva share a common set of basic capabilities:

Task Interface

type Task interface {
    Execute() error
    GetTitle() string
    GetResult() interface{}
    IsCompleted() bool
}

Timeouts

All tasks support timeout configuration with default values:
// For YesNoTask
task.WithTimeout(5*time.Second, "Yes")

// For SingleSelectTask
task.WithTimeout(10*time.Second, "option1")

// For MultiSelectTask
task.WithTimeout(15*time.Second, []string{"item1", "item2"})

// For InputTask
task.WithTimeout(30*time.Second, "default text")

Choice Hints

For selection tasks, you can add hints:
// Using default delimiter "::"
options := []string{
    "dev::Development environment",
    "prod::Production environment",
}

// Configure custom delimiter
options := []string{
    "api||REST API server",
    "web||Web interface",
    "cli||Command line interface",
}

Creating Tasks

Factory Methods

// YesNo task
yesno := ziva.NewYesNoTask("Title", "Question?")

// Single select task
single := ziva.NewSingleSelectTask("Title", []string{"opt1", "opt2"})

// Multi select task
multi := ziva.NewMultiSelectTask("Title", []string{"item1", "item2"})

// Input task
input := ziva.NewInputTask("Title", "Prompt:")

// Function task
fn := ziva.NewFuncTask("Title", func() error {
    // perform work
    return nil
})

Method Chaining

All tasks support method chaining configuration:
task := ziva.NewSingleSelectTask("Selection", options).
    WithDefaultItem("option2").
    WithTimeout(10*time.Second, "option1").
    WithViewport(5, true)

Getting Results

After Queue Execution

// Save task references
yesnoTask := ziva.NewYesNoTask("Confirm", "Continue?")
selectTask := ziva.NewSingleSelectTask("Choose", options)

queue := ziva.NewQueue("Demo")
queue.AddTasks(yesnoTask, selectTask)
queue.Run()

// Get results
if yesnoTask.IsYes() {
    fmt.Printf("Selected: %s\n", selectTask.GetSelected())
}

Typed Results

// YesNoTask
option := yesnoTask.GetSelectedOption() // YesNoOption
isYes := yesnoTask.IsYes()              // bool
isNo := yesnoTask.IsNo()                // bool

// SingleSelectTask
selected := task.GetSelected()          // string
index := task.GetSelectedIndex()        // int

// MultiSelectTask
items := task.GetSelected()             // []string

// InputTask
value := task.GetValue()                // string

Advanced Features

Viewport for Long Lists

task := ziva.NewSingleSelectTask("Choose", longList).
    WithViewport(5, true) // show 5 items, show counters

Disabling Items

// By indices
task.WithItemsDisabled([]int{0, 2, 4})

// By values
task.WithItemsDisabled([]string{"item1", "item3"})

Default Values

// For single select
task.WithDefaultItem("option2")
task.WithDefaultItem(1) // by index

// For multi select
task.WithDefaultItems([]string{"item1", "item3"})
task.WithDefaultItems([]int{0, 2}) // by indices

Error Handling

Validation in InputTask

input := ziva.NewInputTask("Email", "Enter email:").
    WithValidator(ziva.DefaultValidators.Email())

// After execution
if input.GetValue() != "" {
    fmt.Printf("Valid email: %s\n", input.GetValue())
}

Errors in FuncTask

funcTask := ziva.NewFuncTask("Process", func() error {
    // May return error
    return errors.New("processing failed")
}).WithStopOnError(true) // stop queue on error

Best Practices

// Configuration task group
configTasks := []ziva.Task{
    ziva.NewInputTask("Host", "Enter hostname:"),
    ziva.NewInputTask("Port", "Enter port:"),
    ziva.NewYesNoTask("SSL", "Enable SSL?"),
}
queue.AddTasks(configTasks...)

2. Use Context

type Config struct {
    Host string
    Port string
    SSL  bool
}

func setupConfig() *Config {
    var config Config

    hostTask := ziva.NewInputTask("Host", "Enter hostname:")
    portTask := ziva.NewInputTask("Port", "Enter port:")
    sslTask := ziva.NewYesNoTask("SSL", "Enable SSL?")

    queue := ziva.NewQueue("Configuration")
    queue.AddTasks(hostTask, portTask, sslTask)
    queue.Run()

    config.Host = hostTask.GetValue()
    config.Port = portTask.GetValue()
    config.SSL = sslTask.IsYes()

    return &config
}

3. Validate Results

task := ziva.NewInputTask("Port", "Enter port (1-65535):").
    WithValidator(ziva.DefaultValidators.Port())

// Additional check after execution
if task.GetValue() != "" {
    port, _ := strconv.Atoi(task.GetValue())
    if port < 1024 {
        fmt.Println("Warning: Using privileged port")
    }
}

See Also

I