> ## Documentation Index
> Fetch the complete documentation index at: https://ziva.zeleza.ru/llms.txt
> Use this file to discover all available pages before exploring further.

# Task Queue

> Task sequence management system in Ziva

# Task Queue

`Queue` is the central component of Ziva that manages sequential task execution and provides a unified interface for user interaction.

## Creating a Queue

```go theme={null}
queue := ziva.NewQueue("Queue title")
```

<img src="https://mintcdn.com/terem/7tfdR1JkDhuUAiXc/images/pic_5.png?fit=max&auto=format&n=7tfdR1JkDhuUAiXc&q=85&s=c969bea304b96b8f8b766f68d3c2ae09" alt="Screenshot: queue summary view" width="966" height="620" data-path="images/pic_5.png" />

## Main Methods

### Adding Tasks

```go theme={null}
// Add single task
queue.AddTasks(task1)

// Add multiple tasks
queue.AddTasks(task1, task2, task3)

// Chained addition
queue.AddTasks(task1).AddTasks(task2)
```

### Running the Queue

```go theme={null}
if err := queue.Run(); err != nil {
    log.Fatal(err)
}
```

## Display Configuration

### Application Name

```go theme={null}
queue.WithAppName("MyApplication")
```

### Name and Title Colors

```go theme={null}
// Application name color
queue.WithAppNameColor(ziva.BlueBright, true) // color, bold

// Queue title color
queue.WithTitleColor(ziva.GreenBright, false) // color, normal
```

### Task Numbering

```go theme={null}
// Enable numbering with format
queue.WithTasksNumbered(true, "[%02d]") // [01], [02], [03]...

// Alternative formats
queue.WithTasksNumbered(true, "(%d)")   // (1), (2), (3)...
queue.WithTasksNumbered(true, "%d.")    // 1., 2., 3....

// Keep first character of format
queue.WithTasksNumbered(true, "[%02d]") // preserve '['
```

### Summary Management

```go theme={null}
// Disable completion summary
queue.WithOutSummary()

// Disable result separator lines
queue.WithOutResultLine()
```

### Screen Clearing

```go theme={null}
// Clear screen before running
queue.WithClearScreen(true)
```

## Error Management

### Error Color Configuration

```go theme={null}
// Import error types
import "github.com/qzeleza/ziva/internal/query"

// Set error color
queue.SetErrorColor(query.ErrorColorRed)    // Red
queue.SetErrorColor(query.ErrorColorYellow) // Yellow
queue.SetErrorColor(query.ErrorColorNone)   // No color
```

## Configuration Examples

### Minimal Configuration

```go theme={null}
queue := ziva.NewQueue("Simple application")
queue.AddTasks(task1, task2)
queue.Run()
```

### Full Configuration

```go theme={null}
queue := ziva.NewQueue("Advanced TUI Application")
queue.WithAppName("AdvancedApp").
      WithAppNameColor(ziva.BlueBright, true).
      WithTitleColor(ziva.GreenBright, false).
      WithTasksNumbered(true, "[%02d]").
      WithClearScreen(true).
      WithOutResultLine().
      SetErrorColor(query.ErrorColorRed)

queue.AddTasks(
    confirmTask,
    selectTask,
    inputTask,
    functionTask,
)

if err := queue.Run(); err != nil {
    log.Fatalf("Execution error: %v", err)
}
```

### Embedded Systems Configuration

```go theme={null}
queue := ziva.NewQueue("IoT Configuration")
queue.WithAppName("RouterSetup").
      WithOutSummary().        // Space saving
      WithOutResultLine().         // Simplified output
      WithTasksNumbered(false, ""). // No numbering
      WithClearScreen(false)       // Preserve context

// Use simplified colors
queue.WithAppNameColor(ziva.WhiteBright, false).
      WithTitleColor(ziva.GrayBright, false)
```

## Queue Lifecycle

1. **Creation** - `NewQueue(title)`
2. **Configuration** - `With*` method calls
3. **Task Addition** - `AddTasks(...)`
4. **Execution** - `Run()`

## State Handling

### Successful Execution

When all tasks complete successfully, `Run()` returns `nil`.

### User Cancellation

If the user cancels execution (Ctrl+C), the queue terminates gracefully.

### Execution Errors

```go theme={null}
if err := queue.Run(); err != nil {
    switch err {
    case context.Canceled:
        fmt.Println("Execution canceled by user")
    default:
        fmt.Printf("Error: %v\n", err)
    }
}
```

## Performance Optimization

### For Large Queues

```go theme={null}
// Limit completed task history
os.Setenv("ZIVA_MAX_COMPLETED_TASKS", "5")

// Memory monitoring
os.Setenv("ZIVA_MEMORY_PRESSURE_THRESHOLD", "100M")
```

### For Slow Terminals

```go theme={null}
// Simplified display mode
queue.WithOutResultLine().
      WithOutSummary().
      WithTasksNumbered(false, "")
```

<Info>
  **Tip:** Experiment with different settings to achieve optimal user experience in your specific environment.
</Info>

## See Also

* [Task Types](/en/components/tasks) - overview of all available tasks
* [Styling](/en/customization/styling) - appearance customization
* [Embedded Systems](/en/customization/embedded-systems) - IoT optimization
