Example #1
0
        private PSConsoleReadLine()
        {
            _mockableMethods = this;
            _console         = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
                ? PlatformWindows.OneTimeInit(this)
                : new VirtualTerminal();
            _charMap = new DotNetCharMap();

            _buffer           = new StringBuilder(8 * 1024);
            _statusBuffer     = new StringBuilder(256);
            _savedCurrentLine = new HistoryItem();
            _queuedKeys       = new Queue <PSKeyInfo>();

            // Initialize this event handler early because it could be used by PowerShell
            // Editor Services before 'DelayedOneTimeInitialize' runs.
            _forceEventWaitHandle = new AutoResetEvent(false);

            string hostName = null;

            // This works mostly by luck - we're not doing anything to guarantee the constructor for our
            // singleton is called on a thread with a runspace, but it is happening by coincidence.
            using (var ps = System.Management.Automation.PowerShell.Create(RunspaceMode.CurrentRunspace))
            {
                try
                {
                    var    results = ps.AddScript("$Host", useLocalScope: true).Invoke <PSHost>();
                    PSHost host    = results.Count == 1 ? results[0] : null;
                    hostName = host?.Name;
                }
                catch
                {
                }
            }
            if (hostName == null)
            {
                hostName = "PSReadLine";
            }
            _options    = new PSConsoleReadLineOptions(hostName);
            _prediction = new Prediction(this);
            SetDefaultBindings(_options.EditMode);
        }
Example #2
0
        private PSConsoleReadLine()
        {
            _mockableMethods = this;
            _console         = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
                ? PlatformWindows.OneTimeInit(this)
                : new VirtualTerminal();
            _charMap = new DotNetCharMap();

            _buffer           = new StringBuilder(8 * 1024);
            _statusBuffer     = new StringBuilder(256);
            _savedCurrentLine = new HistoryItem();
            _queuedKeys       = new Queue <ConsoleKeyInfo>();

            string hostName = null;

            // This works mostly by luck - we're not doing anything to guarantee the constructor for our
            // singleton is called on a thread with a runspace, but it is happening by coincidence.
            using (var ps = System.Management.Automation.PowerShell.Create(RunspaceMode.CurrentRunspace))
            {
                try
                {
                    ps.AddCommand("Get-Variable").AddParameter("Name", "host").AddParameter("ValueOnly");
                    var     results = ps.Invoke();
                    dynamic host    = results.Count == 1 ? results[0] : null;
                    if (host != null)
                    {
                        hostName = host.Name as string;
                    }
                }
                catch
                {
                }
            }
            if (hostName == null)
            {
                hostName = "PSReadLine";
            }
            _options = new PSConsoleReadLineOptions(hostName);
            SetDefaultBindings(_options.EditMode);
        }