Class ui.panel.Confirm

Confirmation dialog.

Creates a modal dialog that sizes itself to the terminal: the width is derived from the ButtonBar's preferred display size, and the text area height from word-wrapping the prompt to that width.

The button items, ids, and sets fields share the same format as ui.panel.ButtonBar and cli.Confirm.

Call :run() to display the dialog and block until the user confirms or cancels. On confirmation the id of the selected button is returned. On cancellation nil, "cancelled" is returned.

This Panel class is standalone, similar to Screen, it is not intended to be embedded inside another Panel as a child.

Example:

local Confirm = require("terminal.ui.panel.confirm")
local terminal = require("terminal")

local dlg = Confirm {
  prompt   = "Delete this file?",
  buttons  = Confirm.sets.yes_no_cancel,
  border   = {
    format = terminal.draw.box_fmt.rounded,
    title  = "Confirm",
  },
}

local id, err = dlg:run()
if id == Confirm.ids.yes then
  -- proceed
end

Functions

ui.panel.Confirm (opts) Create a new Confirm dialog instance.

Tables

ui.panel.confirm.ids Response ID constants.
ui.panel.confirm.sets Preset item sets ready to pass as opts.items.

Methods

ui.panel.confirm:get_selection () Returns the id of the currently focused button.
ui.panel.confirm:run () Display the dialog and block until the user makes a selection.


Functions

ui.panel.Confirm (opts)
Create a new Confirm dialog instance.

Parameters:

  • opts Options.
    • prompt string or table The question or message shown in the text area. A plain string is used as a single line. Alternatively supply an array of strings.
    • buttons table Array of button items. Each entry must have a label (string) and may have an id (any; defaults to the item index) and cancel (boolean; marks the button selected on Escape).
    • default any Id of the initially focused button. Defaults to the first item. (optional)
    • cancellable boolean Whether ESC/Ctrl+C is allowed. Defaults to true when an item has cancel = true, false otherwise. (optional)
    • border table Border configuration for the dialog. Inherits the same structure as Panel's border option (format, attr, title, titleattr, truncationtype). (optional)
    • prefix string Passed to ButtonBar: string prepended to each button label, default: "[". (optional)
    • postfix string Passed to ButtonBar: string appended to each button label, default: "]". (optional)
    • padding number Passed to ButtonBar: spaces between adjacent buttons. (default 1)
    • button_min_width number Passed to ButtonBar. (optional)
    • button_max_width number Passed to ButtonBar. (optional)
    • attr table Text attributes applied to the dialog background and prompt text. (optional)
    • button_attr table Passed to ButtonBar: unselected button attributes. (optional)
    • selected_attr table Passed to ButtonBar: focused button attributes. (optional)
    • redraw function Called before each dialog render (including on resize) and once after :run() exits. Use it to repaint the underlying screen so the dialog always draws on a fresh background and the screen is restored on exit. (optional)

Returns:

    Confirm A new Confirm dialog instance.

Tables

ui.panel.confirm.ids
Response ID constants. Shared with ButtonBar.ids and cli.Confirm.ids.
ui.panel.confirm.sets
Preset item sets ready to pass as opts.items. Shared with ButtonBar.sets and cli.Confirm.sets.

Methods

ui.panel.confirm:get_selection ()
Returns the id of the currently focused button.

Returns:

    any The focused button id.
ui.panel.confirm:run ()
Display the dialog and block until the user makes a selection. Handles Left/Right to navigate buttons, Enter to confirm, and ESC/Ctrl+C to cancel. On each iteration the terminal size is compared to the last known size; if it changed the layout is recalculated and the dialog is re-rendered before processing the key.

Returns:

  1. any The id of the selected button.
  2. nil

Or

  1. nil On success with an explicit cancel button.
  2. string "cancelled" when ESC is pressed and no cancel button exists but cancellable is true.
generated by LDoc 1.5.0