showSingleSelectDialog function

Future<int?> showSingleSelectDialog(
  1. List<String> options,
  2. Stream<List<int>> inputStream, {
  3. int maxVisibleItems = 5,
})

Shows a scrollable terminal selection dialog and returns the selected index.

Only standard ASCII characters should be used in the options, as other characters may break the width calculations.

Temporarily disables stdin line and echo modes, and restores them before returning. Also intercepts ProcessSignal.sigint to cancel the dialog.

The cursor is hidden for the duration of the dialog and then re-shown. If you want the cursor in a certain state after this dialog you will have to restore it.

The inputStream is a list of input events (typically originating from stdin or Win32AnsiStdin). See the example/select_dialog.dart for recommended patterns.

The maxVisibleItems parameter controls how many items are visible in the dialog at once.

Returns null if the user aborts the dialog (e.g. by pressing Ctrl+C or escape), there is no terminal attached to stdout, or the terminal is too small to display the dialog.

Implementation

Future<int?> showSingleSelectDialog(
  List<String> options,
  Stream<List<int>> inputStream, {
  int maxVisibleItems = 5,
}) async {
  final selectedIndices = await _runDialog(
    options,
    maxVisibleItems,
    inputStream,
    multiSelect: false,
  );
  if (selectedIndices == null || selectedIndices.isEmpty) {
    return null;
  }
  return selectedIndices.single;
}