コード例 #1
0
            /// <summary>
            /// Process a input line.
            /// </summary>
            /// <param name="inputLine"></param>
            protected Tuple <bool, bool> Process(InputLine inputLine)
            {
                SnapshotSpan inputSpan = inputLine.SnapshotSpan;

                if (inputLine.Flags.HasFlag(InputLineFlag.Echo))
                {
                    WpfConsole.BeginInputLine();

                    if (inputLine.Flags.HasFlag(InputLineFlag.Execute))
                    {
                        NuGetUIThreadHelper.JoinableTaskFactory.Run(() => WpfConsole.WriteLineAsync(inputLine.Text));
                        inputSpan = WpfConsole.EndInputLine(true).Value;
                    }
                    else
                    {
                        NuGetUIThreadHelper.JoinableTaskFactory.Run(() => WpfConsole.WriteAsync(inputLine.Text));
                    }
                }

                if (inputLine.Flags.HasFlag(InputLineFlag.Execute))
                {
                    string command    = inputLine.Text;
                    bool   isExecuted = WpfConsole.Host.Execute(WpfConsole, command, null);
                    WpfConsole.InputHistory.Add(command);
                    ParentDispatcher.OnExecute(inputSpan, isExecuted);
                    return(Tuple.Create(true, isExecuted));
                }
                return(Tuple.Create(false, false));
            }
コード例 #2
0
 protected void PromptNewLine()
 {
     NuGetUIThreadHelper.JoinableTaskFactory.Run(() => WpfConsole.WriteAsync(WpfConsole.Host.Prompt + ' '));
     WpfConsole.BeginInputLine();
 }
コード例 #3
0
        public void Start()
        {
            // Only Start once
            lock (_lockObj)
            {
                if (_dispatcher == null)
                {
                    IHost host = WpfConsole.Host;

                    if (host == null)
                    {
                        throw new InvalidOperationException("Can't start Console dispatcher. Host is null.");
                    }

                    if (host is IAsyncHost)
                    {
                        _dispatcher = new AsyncHostConsoleDispatcher(this);
                    }
                    else
                    {
                        _dispatcher = new SyncHostConsoleDispatcher(this);
                    }

                    // capture the cultures to assign to the worker thread below
                    CultureInfo currentCulture   = CultureInfo.CurrentCulture;
                    CultureInfo currentUICulture = CultureInfo.CurrentUICulture;

                    // changed from Task.Factory.StartNew to Task.Run in order to run with
                    // default TaskSchedular instead of current.
                    Task.Run(
                        // gives the host a chance to do initialization works before the console starts accepting user inputs
                        () =>
                    {
                        // apply the culture of the main thread to this thread so that the PowerShell engine
                        // will have the same culture as Visual Studio.
                        Thread.CurrentThread.CurrentCulture   = currentCulture;
                        Thread.CurrentThread.CurrentUICulture = currentUICulture;

                        host.Initialize(WpfConsole);
                    }
                        ).ContinueWith(
                        task =>
                    {
                        NuGetUIThreadHelper.JoinableTaskFactory.Run(async delegate
                        {
                            await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
                            if (task.IsFaulted)
                            {
                                var exception = ExceptionUtilities.Unwrap(task.Exception);
                                if (WpfConsole != null)
                                {
                                    await WpfConsole.WriteAsync(exception.Message + Environment.NewLine, Colors.Red, null);
                                }
                            }

                            if (host.IsCommandEnabled &&
                                _dispatcher != null)
                            {
                                _dispatcher.Start();
                            }

                            RaiseEventSafe(StartCompleted);
                            IsStartCompleted = true;
                        });
                    },
                        TaskContinuationOptions.NotOnCanceled
                        );
                }
            }
        }