private static Command who_and_whos()
        {
            var command = new Command("#!whos", "Display the names of the current top-level variables and their values.")
            {
                Handler = CommandHandler.Create((ParseResult parseResult, KernelInvocationContext context) =>
                {
                    var alias = parseResult.CommandResult.Token.Value;

                    var detailed = alias == "#!whos";

                    Display(context, detailed);

                    return(Task.CompletedTask);
                })
            };

            // TODO: (who_and_whos) this should be a separate command with separate help
            command.AddAlias("#!who");

            return(command);

            void Display(KernelInvocationContext context, bool detailed)
            {
                if (context.Command is SubmitCode &&
                    context.HandlingKernel is CSharpKernel kernel)
                {
                    var variables = kernel.ScriptState?.Variables.Select(v => new CurrentVariable(v.Name, v.Type, v.Value)) ??
                                    Enumerable.Empty <CurrentVariable>();

                    var currentVariables = new CurrentVariables(
                        variables,
                        detailed);

                    var html = currentVariables
                               .ToDisplayString(HtmlFormatter.MimeType);

                    context.Publish(
                        new DisplayedValueProduced(
                            html,
                            context.Command,
                            new[]
                    {
                        new FormattedValue(
                            HtmlFormatter.MimeType,
                            html)
                    }));
                }
            }
        }
Example #2
0
        private static Command who_and_whos()
        {
            var command = new Command("%whos")
            {
                Handler = CommandHandler.Create((ParseResult parseResult, KernelInvocationContext context) =>
                {
                    var alias = parseResult.CommandResult.Token.Value;

                    var detailed = alias == "%whos";

                    if (context.Command is SubmitCode &&
                        context.HandlingKernel is CSharpKernel kernel)
                    {
                        var variables = kernel.ScriptState.Variables;

                        var currentVariables = new CurrentVariables(
                            variables,
                            detailed);

                        var html = currentVariables
                                   .ToDisplayString(HtmlFormatter.MimeType);

                        context.Publish(
                            new DisplayedValueProduced(
                                html,
                                context.Command,
                                new[]
                        {
                            new FormattedValue(
                                HtmlFormatter.MimeType,
                                html)
                        }));
                    }

                    return(Task.CompletedTask);
                })
            };

            command.AddAlias("%who");

            return(command);
        }