Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
 internal void PushWriter(OutputWriter writer)
 {
     _writers.Push(writer);
 }
Exemplo n.º 3
0
        /// <summary>
        /// Invokes PowerShell command with pipeline.
        /// </summary>
        /// <param name="code">PowerShell code.</param>
        /// <param name="writer">Output writer or null.</param>
        /// <param name="addHistory">Add command to history.</param>
        internal bool Act(string code, OutputWriter writer, bool addHistory)
        {
            // result
            bool ok = true;

            // drop history cache
            History.Cache = null;

            // 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
                Far.Api.UI.SetProgressState(TaskbarProgressBarState.Indeterminate);

                // add history
                if (addHistory)
                {
                    code = code.Trim();
                    if (code.Length > 0 && code[code.Length - 1] != '#' && code != _myLastCommand)
                        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 (RuntimeException reason)
            {
                ok = false;
                ConsoleColor color1 = ConsoleColor.Black;
                try
                {
                    // push console color
                    if (writer is ConsoleOutputWriter)
                    {
                        color1 = Far.Api.UI.ForegroundColor;
                        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
                Far.Api.UI.SetProgressState(TaskbarProgressBarState.NoProgress);

                _myLastCommand = _myCommand;
                _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;
        }
Exemplo n.º 4
0
 internal void PushWriter(OutputWriter writer)
 {
     _writers.Push(writer);
 }
Exemplo n.º 5
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();
            }
        }