Skip to content

Latest commit

 

History

History
137 lines (97 loc) · 4.58 KB

index.md

File metadata and controls

137 lines (97 loc) · 4.58 KB
PromptPlus

PromptPlus

Switch

NuGet License: MIT .NET

← Back to Home • Next: Switch — Methods →


Toggle a single boolean between its on and off states. The user flips it with the arrow keys or Space and confirms with Enter.

The Switch control is the fastest way to collect one yes/no, enable/disable, or active/inactive value. It shows two labeled states and lets the user flip between them; the labels can be plain text or emoji, and the confirmed value can be persisted as history. Everything is configured through a single fluent chain.

🔘 Just need to display an on/off indicator without asking for input? Use the read-only Switch widget instead — it renders the same toggle but does not wait for the user.


On this page

Sub-page What you will find
Index (this page) What it is, when to use it, a first working example, the method map
Methods Every fluent method — signature, parameters, defaults, and a snippet
Operations Keyboard, labels, history, and edge cases
Styles The SwitchStyles regions and how to recolor them

When to use it

Use Switch when… Consider instead…
You need a single on/off boolean —
You want a yes/no key-press confirmation Confirm
The user should pick one of several options Select
You only need to display a state, not collect one Switch widget

Minimal example

using PromptPlusLibrary;

var result = PromptPlus.Controls
    .Switch("Enable feature?")
    .Run();

if (!result.IsAborted)
    PromptPlus.Console.WriteLine($"Enabled: {result.Content}");
  • Switch("Enable feature?") creates the control. The first argument is the prompt; an optional second argument is a description line shown under it.
  • With no configuration the switch starts off (false) and shows the localized Yes/No labels.
  • .Run() renders the toggle and blocks until the user presses Enter (confirm) or Esc (abort).
  • The call returns a ResultPrompt<bool?>: read .Content for the state and .IsAborted to detect Esc.

💡 The value is nullable. On abort .Content is null — always check IsAborted first.


A more complete example

using PromptPlusLibrary;

var env = PromptPlus.Controls
    .Switch("Environment")
    .OnValue("Production")     // label for the on state
    .OffValue("Development")   // label for the off state
    .Default(false)            // start on Development
    .Run();

if (!env.IsAborted)
    PromptPlus.Console.WriteLine($"Environment: {(env.Content == true ? "Production" : "Development")}");

This shows the two things you most often customize: the state labels (OnValue / OffValue) and the starting value (Default). See Methods for the emoji label overloads and Operations for how flipping behaves at runtime.


Method map

Grouped by purpose. Full signatures and examples are on the Methods page.

Purpose Methods
Starting value Default
State labels OnValue, OffValue
Dynamic description ChangeDescription, ChangeDescriptionAsync
History EnableHistory
Appearance & behavior Styles, Options
Run Run

Return value

Switch returns ResultPrompt<bool?>.

Member Meaning
.Content The confirmed state (true = on, false = off; null if aborted)
.IsAborted true when the user pressed Esc / the abort key
var (state, aborted) = PromptPlus.Controls.Switch("Enable?").Run();
if (!aborted) PromptPlus.Console.WriteLine($"{state}");

See also

  • Methods — the full fluent API
  • Operations — keyboard, labels, history
  • Styles — recolor the prompt, on/off, and error regions
  • Switch widget — the read-only, display-only sibling
  • Slider — the numeric-range cousin