Example #1
0
        void IInvokeInteractive.Run()
        {
            InitialisePowerShellImpl();
            ShouldExit = false;
            // Set up the control-C handler.
            var treatAsInputOld = Console.TreatControlCAsInput;

            Console.CancelKeyPress      += HandleControlC;
            Console.TreatControlCAsInput = false;
            try {
                // Read commands and run them until the ShouldExit flag is set by
                // the user calling "exit".
                var invocationSettings = new PSInvocationSettings {
                    AddToHistory = true
                };
                while (!ShouldExit)
                {
                    var prompt = GetPrompt();
                    _consoleHost.UI.Write(_consoleHost.UI.RawUI.ForegroundColor, _consoleHost.UI.RawUI.BackgroundColor, prompt);
                    var cmd = ReadLine() ?? _consoleReadLine.Read();
                    InvokeScript(cmd, invocationSettings);
                }
            }
            finally {
                Console.CancelKeyPress      -= HandleControlC;
                Console.TreatControlCAsInput = treatAsInputOld;
            }

            // Exit with the desired exit code that was set by the exit command.
            // The exit code is set in the host by the MyHost.SetShouldExit() method.
        }