Example #1
0
        void AsyncInvoke(IAsyncResult ar)
        {
            // end; it may throw, e.g. on [CtrlC]
            try
            {
                PowerShell.EndInvoke(ar);
            }
            catch (RuntimeException)
            { }

            // write failure
            if (PowerShell.InvocationStateInfo.State == PSInvocationState.Failed)
            {
                using (PowerShell ps = PowerShell.Create())
                {
                    ps.Runspace = Runspace;
                    A.OutReason(ps, PowerShell.InvocationStateInfo.Reason);
                }
            }

            // complete output
            var writer = (EditorOutputWriter1)FarUI.PopWriter();

            EndOutput(writer);
            EndInvoke();

            // kill
            PowerShell.Dispose();
        }
Example #2
0
        internal bool Act(string code, OutputWriter writer, bool addHistory)
        {
            // result
            bool ok = true;

            // push writer
            if (writer != null)
            {
                // predefined output
                FarUI.PushWriter(writer);
            }
            else
            {
                // use own output to be shown later
                FarUI.PushWriter(new TranscriptOutputWriter());
            }

            // invoke
            try
            {
                // win7 Indeterminate
                FarUI.IsProgressStarted = false;
                Far.Api.UI.SetProgressState(TaskbarProgressBarState.Indeterminate);

                // add history
                if (addHistory)
                {
                    code = code.Trim();
                    if (code.Length > 0 && code[code.Length - 1] != '#')
                    {
                        History.AddLine(code);
                    }
                }

                // invoke command
                using (var ps = NewPowerShell())
                {
                    _myCommand = code;
                    //TODO We may need a mode with Out-Host even for console, e.g. to transcribe apps output
                    var output = FarUI.Writer is ConsoleOutputWriter ? A.OutDefaultCommand : A.OutHostCommand;
                    ps.Commands.AddScript(code).AddCommand(output);
                    ps.Invoke();
                }
            }
            catch (Exception reason)
            {
                ok = false;
                ConsoleColor color1 = ConsoleColor.Black;
                try
                {
                    // push console color
                    if (writer is ConsoleOutputWriter)
                    {
                        color1 = Far.Api.UI.ForegroundColor;
                        Far.Api.UI.ShowUserScreen();
                        Far.Api.UI.ForegroundColor = Settings.ErrorForegroundColor;
                    }

                    // write the reason
                    using (var ps = NewPowerShell())
                        A.OutReason(ps, reason);
                }
                finally
                {
                    // pop console color
                    if (color1 != ConsoleColor.Black)
                    {
                        Far.Api.UI.ForegroundColor = color1;
                    }
                }
            }
            finally
            {
                // win7 NoProgress
                FarUI.IsProgressStarted = false;
                Far.Api.UI.SetProgressState(TaskbarProgressBarState.NoProgress);

                _myCommand = null;

                // pop writer
                OutputWriter usedWriter = FarUI.PopWriter();
                if (writer == null)
                {
                    // it is the writer created locally;
                    // view its file, if any
                    var myWriter = (TranscriptOutputWriter)usedWriter;
                    myWriter.Close();
                    if (myWriter.FileName != null)
                    {
                        var viewer = Far.Api.CreateViewer();
                        viewer.Title          = code;
                        viewer.FileName       = myWriter.FileName;
                        viewer.DeleteSource   = DeleteSource.File;
                        viewer.Switching      = Switching.Enabled;
                        viewer.DisableHistory = true;
                        viewer.CodePage       = 1200;
                        viewer.Open();
                    }
                }

                // notify host
                FarHost.NotifyEndApplication();
            }

            return(ok);
        }
Example #3
0
        internal bool Run(RunArgs args)
        {
            var code = args.Code;

            // push writer
            if (args.Writer != null)
            {
                // predefined output
                FarUI.PushWriter(args.Writer);
            }
            else
            {
                // use own output to be shown later
                FarUI.PushWriter(new TranscriptOutputWriter());
            }

            // invoke
            try
            {
                // win7 Indeterminate
                FarUI.IsProgressStarted = false;
                Far.Api.UI.SetProgressState(TaskbarProgressBarState.Indeterminate);

                // add history
                if (args.AddHistory)
                {
                    code = code.Trim();
                    if (!code.EndsWith("#"))
                    {
                        History.AddLine(code);
                    }
                }

                // invoke command
                using (var ps = NewPowerShell())
                {
                    _myCommand = code;
                    var command = ps.Commands.AddScript(code, args.UseLocalScope);
                    if (args.Arguments != null)
                    {
                        foreach (var arg in args.Arguments)
                        {
                            command.AddArgument(arg);
                        }
                    }

                    var output = FarUI.Writer is ConsoleOutputWriter ? A.OutDefaultCommand : A.OutHostCommand;
                    command.AddCommand(output);
                    ps.Invoke();
                    args.Reason = ps.InvocationStateInfo.Reason;
                }

                return(true);
            }
            catch (Exception reason)
            {
                args.Reason = reason;
                if (args.NoOutReason)
                {
                    return(false);
                }

                ConsoleColor color1 = ConsoleColor.Black;
                try
                {
                    // push console color
                    if (args.Writer is ConsoleOutputWriter)
                    {
                        color1 = Far.Api.UI.ForegroundColor;
                        Far.Api.UI.ShowUserScreen();
                        Far.Api.UI.ForegroundColor = Settings.ErrorForegroundColor;
                    }

                    // write the reason
                    using (var ps = NewPowerShell())
                        A.OutReason(ps, reason);
                }
                finally
                {
                    // pop console color
                    if (color1 != ConsoleColor.Black)
                    {
                        Far.Api.UI.ForegroundColor = color1;
                    }
                }

                return(false);
            }
            finally
            {
                // win7 NoProgress
                FarUI.IsProgressStarted = false;
                Far.Api.UI.SetProgressState(TaskbarProgressBarState.NoProgress);

                _myCommand = null;

                // pop writer
                OutputWriter usedWriter = FarUI.PopWriter();
                if (args.Writer == null)
                {
                    // it is the writer created locally;
                    // view its file, if any
                    var myWriter = (TranscriptOutputWriter)usedWriter;
                    myWriter.Close();
                    if (myWriter.FileName != null)
                    {
                        var viewer = Far.Api.CreateViewer();
                        viewer.Title          = code;
                        viewer.FileName       = myWriter.FileName;
                        viewer.DeleteSource   = DeleteSource.File;
                        viewer.Switching      = Switching.Enabled;
                        viewer.DisableHistory = true;
                        viewer.CodePage       = 1200;
                        viewer.Open();
                    }
                }

                // notify host
                FarHost.NotifyEndApplication();
            }
        }