Ejemplo n.º 1
0
        /// <summary>
        /// Gets input from the user.
        /// </summary>
        /// <param name="prompt">The prompt to show.</param>
        /// <param name="isPassword">Whether the input should be treated as a password.</param>
        /// <returns>The value.</returns>
        public Task <string> GetInputAsync(string prompt = "", bool isPassword = false, CancellationToken cancellationToken = default)
        {
            var command          = new GetInput(prompt, isPassword, targetKernelName: VSCodeKernelName);
            var completionSource = new TaskCompletionSource <string>();
            var token            = command.GetToken();

            _kernel.KernelEvents.Where(e => e.Command.GetToken() == token).Subscribe(e =>
            {
                switch (e)
                {
                case CommandFailed ex:
                    completionSource.TrySetException(ex.Exception ?? new Exception(ex.Message));
                    break;

                case CommandSucceeded _:
                    completionSource.TrySetResult(null);
                    break;

                case InputProduced iv:
                    completionSource.TrySetResult(iv.Value);
                    break;
                }
            }, cancellationToken);
            var _ = _kernel.SendAsync(command, cancellationToken);

            return(completionSource.Task);
        }
        /// <summary>
        /// Gets input from the user.
        /// </summary>
        /// <param name="prompt">The prompt to show.</param>
        /// <param name="isPassword">Whether the input should be treated as a password.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The user input value.</returns>
        public static async Task <string> GetInputAsync(string prompt = "", bool isPassword = false, CancellationToken cancellationToken = default)
        {
            var kernel = Kernel.Root.FindKernel(VSCodeKernelName) ?? throw new ArgumentNullException($"Cannot find kernel {VSCodeKernelName}");

            var command = new GetInput(prompt, isPassword, targetKernelName: VSCodeKernelName);

            var results = await kernel.SendAsync(command, cancellationToken);

            var inputProduced = await results.KernelEvents.OfType <InputProduced>().FirstOrDefaultAsync();

            return(inputProduced?.Value);
        }
Ejemplo n.º 3
0
 public InputProduced(string value, GetInput command)
     : base(command)
 {
     Value = value;
 }