run method

int run({
  1. required void render(
    1. FrameContext ctx,
    2. int value,
    3. int maxValue
    ),
  2. KeyBindings? extraBindings,
})

Runs the discrete value prompt.

Implementation

int run({
  required void Function(FrameContext ctx, int value, int maxValue) render,
  KeyBindings? extraBindings,
}) {
  _initState();

  _bindings = KeyBindings.horizontalNavigation(
        onLeft: () => _value = (_value - 1).clamp(1, maxValue),
        onRight: () => _value = (_value + 1).clamp(1, maxValue),
      ) +
      KeyBindings.numbers(
        onNumber: (n) {
          if (n >= 1 && n <= maxValue) _value = n;
        },
        max: maxValue,
        hintLabel: '1–$maxValue',
        hintDescription: 'set exact',
      ) +
      KeyBindings.prompt(onCancel: () => _cancelled = true);

  if (extraBindings != null) {
    _bindings = _bindings + extraBindings;
  }

  final frame = FrameView(
    title: title,
    theme: theme,
    bindings: _bindings,
  );

  void renderFrame(RenderOutput out) {
    frame.render(out, (ctx) {
      render(ctx, _value, maxValue);
    });
  }

  final runner = PromptRunner(hideCursor: true);
  final result = runner.runWithBindings(
    render: renderFrame,
    bindings: _bindings,
  );

  return (result == PromptResult.cancelled || _cancelled) ? initial : _value;
}