Esempio n. 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SelectionPrompt{T}"/> class.
 /// </summary>
 public SelectionPrompt()
 {
     _tree = new ListPromptTree <T>(EqualityComparer <T> .Default);
 }
Esempio n. 2
0
    public async Task <ListPromptState <T> > Show(
        ListPromptTree <T> tree,
        CancellationToken cancellationToken,
        int requestedPageSize = 15)
    {
        if (tree is null)
        {
            throw new ArgumentNullException(nameof(tree));
        }

        if (!_console.Profile.Capabilities.Interactive)
        {
            throw new NotSupportedException(
                      "Cannot show selection prompt since the current " +
                      "terminal isn't interactive.");
        }

        if (!_console.Profile.Capabilities.Ansi)
        {
            throw new NotSupportedException(
                      "Cannot show selection prompt since the current " +
                      "terminal does not support ANSI escape sequences.");
        }

        var nodes = tree.Traverse().ToList();
        var state = new ListPromptState <T>(nodes, _strategy.CalculatePageSize(_console, nodes.Count, requestedPageSize));
        var hook  = new ListPromptRenderHook <T>(_console, () => BuildRenderable(state));

        using (new RenderHookScope(_console, hook))
        {
            _console.Cursor.Hide();
            hook.Refresh();

            while (true)
            {
                var rawKey = await _console.Input.ReadKeyAsync(true, cancellationToken).ConfigureAwait(false);

                if (rawKey == null)
                {
                    continue;
                }

                var key    = rawKey.Value;
                var result = _strategy.HandleInput(key, state);
                if (result == ListPromptInputResult.Submit)
                {
                    break;
                }

                if (state.Update(key.Key) || result == ListPromptInputResult.Refresh)
                {
                    hook.Refresh();
                }
            }
        }

        hook.Clear();
        _console.Cursor.Show();

        return(state);
    }
 /// <summary>
 /// Initializes a new instance of the <see cref="MultiSelectionPrompt{T}"/> class.
 /// </summary>
 /// <param name="comparer">
 /// The <see cref="IEqualityComparer{T}"/> implementation to use when comparing items,
 /// or <c>null</c> to use the default <see cref="IEqualityComparer{T}"/> for the type of the item.
 /// </param>
 public MultiSelectionPrompt(IEqualityComparer <T>?comparer = null)
 {
     Tree = new ListPromptTree <T>(comparer ?? EqualityComparer <T> .Default);
 }