Skip to main content

YesNoTask

YesNoTask provides an interface for choosing between two options: “Yes” and “No”. This is the simplest task type in Ziva, perfect for confirmations and binary decisions.

Creating a Task

task := ziva.NewYesNoTask("Title", "Question for user?")

Main Methods

Creating with Settings

task := ziva.NewYesNoTask("Confirmation", "Continue installation?").
    WithDefaultItem("Yes").                          // default value
    WithTimeout(10*time.Second, "No").               // timeout with fallback
    WithCustomLabels("Continue", "Cancel")           // custom labels

Getting Results

// After task execution
option := task.GetSelectedOption() // ziva.YesNoOption
isYes := task.IsYes()              // bool
isNo := task.IsNo()                // bool

Options and Constants

// Available options
const (
    YesOption = ziva.YesOption  // "Yes"
    NoOption  = ziva.NoOption   // "No"
)

// Check specific option
if task.GetSelectedOption() == ziva.YesOption {
    fmt.Println("User chose 'Yes'")
}

Default Configuration

Default Selection

// By string
task.WithDefaultItem("Yes")
task.WithDefaultItem("No")

// By constant
task.WithDefaultItem(ziva.YesOption)
task.WithDefaultItem(ziva.NoOption)

// By bool (true = Yes, false = No)
task.WithDefaultItem(true)
task.WithDefaultItem(false)

Timeouts

// Timeout with automatic "Yes" selection
task.WithTimeout(5*time.Second, "Yes")

// Timeout with automatic "No" selection
task.WithTimeout(3*time.Second, ziva.NoOption)

// Timeout with bool value
task.WithTimeout(10*time.Second, true) // true = Yes

Custom Labels

By default “Yes” and “No” are used, but you can configure custom labels:
// Custom labels
task.WithCustomLabels("Continue", "Cancel")
task.WithCustomLabels("Accept", "Decline")
task.WithCustomLabels("Enable", "Disable")
task.WithCustomLabels("✓ Yes", "✗ No")

Result Line Output

When a task finishes, Ziva shows an extra line with the chosen answer. Hide it when you need a tighter layout:
task.WithoutResultLine() // suppress the trailing “Yes/No” line

Treating “No” as Success

By default a “No” answer is recorded as a soft error so the queue summary can highlight the refusal. If you prefer to treat “No” as a successful outcome, enable the helper:
task.WithNoAsError() // “No” no longer contributes an error to the summary

if task.IsNo() {
    // handle the choice while keeping the queue statistics positive
}

Usage Examples

Simple Confirmation

func confirmAction() bool {
    task := ziva.NewYesNoTask(
        "Confirmation",
        "Are you sure you want to delete the file?",
    )

    queue := ziva.NewQueue("File Deletion")
    queue.AddTasks(task)
    queue.Run()

    return task.IsYes()
}

Confirmation with Timeout

func confirmWithTimeout() bool {
    task := ziva.NewYesNoTask(
        "Auto-continue",
        "Continue in 5 seconds?",
    ).WithTimeout(5*time.Second, "Yes")

    queue := ziva.NewQueue("Demo")
    queue.AddTasks(task)
    queue.Run()

    return task.IsYes()
}

Custom Options

func enableFeature() bool {
    task := ziva.NewYesNoTask(
        "Feature Setup",
        "Enable experimental feature?",
    ).WithCustomLabels("Enable", "Keep disabled").
      WithDefaultItem("Keep disabled")

    queue := ziva.NewQueue("Configuration")
    queue.AddTasks(task)
    queue.Run()

    return task.IsYes()
}

See Also

I